| 1 |
package ezvcard.util; |
| 2 |
|
| 3 |
import java.time.DateTimeException; |
| 4 |
import java.time.Instant; |
| 5 |
import java.time.LocalDate; |
| 6 |
import java.time.LocalDateTime; |
| 7 |
import java.time.LocalTime; |
| 8 |
import java.time.OffsetDateTime; |
| 9 |
import java.time.ZoneOffset; |
| 10 |
import java.time.format.DateTimeFormatter; |
| 11 |
import java.time.temporal.ChronoField; |
| 12 |
import java.time.temporal.Temporal; |
| 13 |
import java.time.temporal.TemporalAccessor; |
| 14 |
import java.util.Locale; |
| 15 |
import java.util.concurrent.TimeUnit; |
| 16 |
import java.util.regex.Matcher; |
| 17 |
import java.util.regex.Pattern; |
| 18 |
|
| 19 |
import ezvcard.Messages; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public enum VCardDateFormat { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
EXTENDED { |
| 72 |
@Override |
| 73 |
String getPattern(TemporalAccessor temporal) { |
| 74 |
if (temporal instanceof ZoneOffset) { |
| 75 |
return "xxx"; |
| 76 |
} |
| 77 |
if (temporal instanceof Instant) { |
| 78 |
return "yyyy-MM-dd'T'HH:mm:ssX"; |
| 79 |
} |
| 80 |
if (hasOffset(temporal)) { |
| 81 |
return "yyyy-MM-dd'T'HH:mm:ssxxx"; |
| 82 |
} |
| 83 |
if (hasTime(temporal)) { |
| 84 |
return "yyyy-MM-dd'T'HH:mm:ss"; |
| 85 |
} |
| 86 |
return "yyyy-MM-dd"; |
| 87 |
} |
| 88 |
}, |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
BASIC { |
| 106 |
@Override |
| 107 |
String getPattern(TemporalAccessor temporal) { |
| 108 |
if (temporal instanceof ZoneOffset) { |
| 109 |
return "xx"; |
| 110 |
} |
| 111 |
if (temporal instanceof Instant) { |
| 112 |
return "yyyyMMdd'T'HHmmssX"; |
| 113 |
} |
| 114 |
if (hasOffset(temporal)) { |
| 115 |
return "yyyyMMdd'T'HHmmssxx"; |
| 116 |
} |
| 117 |
if (hasTime(temporal)) { |
| 118 |
return "yyyyMMdd'T'HHmmss"; |
| 119 |
} |
| 120 |
return "yyyyMMdd"; |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public String format(TemporalAccessor temporalAccessor) { |
| 130 |
String pattern = getPattern(temporalAccessor); |
| 131 |
DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern, Locale.ROOT); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if (temporalAccessor instanceof Instant) { |
| 138 |
temporalAccessor = ((Instant) temporalAccessor).atOffset(ZoneOffset.UTC); |
| 139 |
} |
| 140 |
|
| 141 |
return df.format(temporalAccessor); |
| 142 |
} |
| 143 |
|
| 144 |
abstract String getPattern(TemporalAccessor temporal); |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static Temporal parse(String string) { |
| 166 |
TimestampPattern p = TimestampPattern.parse(string); |
| 167 |
if (p == null) { |
| 168 |
throw Messages.INSTANCE.getIllegalArgumentException(41, string); |
| 169 |
} |
| 170 |
|
| 171 |
try { |
| 172 |
LocalDate date = LocalDate.of(p.year(), p.month(), p.date()); |
| 173 |
if (!p.hasTime()) { |
| 174 |
return date; |
| 175 |
} |
| 176 |
|
| 177 |
LocalTime time = LocalTime.of(p.hour(), p.minute(), p.second(), p.nanosecond()); |
| 178 |
LocalDateTime datetime = LocalDateTime.of(date, time); |
| 179 |
|
| 180 |
ZoneOffset offset = p.offset(); |
| 181 |
if (offset == null) { |
| 182 |
return datetime; |
| 183 |
} |
| 184 |
|
| 185 |
OffsetDateTime offsetDateTime = OffsetDateTime.of(datetime, offset); |
| 186 |
return "Z".equals(offset.getId()) ? Instant.from(offsetDateTime) : offsetDateTime; |
| 187 |
} catch (DateTimeException e) { |
| 188 |
throw new IllegalArgumentException(e); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
private static class TimestampPattern { |
| 197 |
|
| 198 |
private static final Pattern regex = Pattern.compile( |
| 199 |
"^(\\d{4})(" + |
| 200 |
"-?(\\d{2})-?(\\d{2})|" + |
| 201 |
"-(\\d{1,2})-(\\d{1,2})" + |
| 202 |
")" + |
| 203 |
"(" + |
| 204 |
"T(\\d{2}):?(\\d{2}):?(\\d{2})(\\.\\d+)?" + |
| 205 |
"(" + |
| 206 |
"Z|([-+])((\\d{2})|((\\d{2}):?(\\d{2})))" + |
| 207 |
")?" + |
| 208 |
")?$" |
| 209 |
); |
| 210 |
|
| 211 |
|
| 212 |
private final Matcher matcher; |
| 213 |
|
| 214 |
private TimestampPattern(Matcher matcher) { |
| 215 |
this.matcher = matcher; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public static TimestampPattern parse(String string) { |
| 225 |
Matcher m = regex.matcher(string); |
| 226 |
return m.find() ? new TimestampPattern(m) : null; |
| 227 |
} |
| 228 |
|
| 229 |
public int year() { |
| 230 |
return parseInt(1); |
| 231 |
} |
| 232 |
|
| 233 |
public int month() { |
| 234 |
return parseInt(3, 5); |
| 235 |
} |
| 236 |
|
| 237 |
public int date() { |
| 238 |
return parseInt(4, 6); |
| 239 |
} |
| 240 |
|
| 241 |
public boolean hasTime() { |
| 242 |
return matcher.group(8) != null; |
| 243 |
} |
| 244 |
|
| 245 |
public int hour() { |
| 246 |
return parseInt(8); |
| 247 |
} |
| 248 |
|
| 249 |
public int minute() { |
| 250 |
return parseInt(9); |
| 251 |
} |
| 252 |
|
| 253 |
public int second() { |
| 254 |
return parseInt(10); |
| 255 |
} |
| 256 |
|
| 257 |
public int nanosecond() { |
| 258 |
String s = matcher.group(11); |
| 259 |
if (s == null) { |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
double nanos = Double.parseDouble(s) * TimeUnit.SECONDS.toNanos(1); |
| 264 |
return (int) Math.round(nanos); |
| 265 |
} |
| 266 |
|
| 267 |
public ZoneOffset offset() { |
| 268 |
String offsetStr = matcher.group(12); |
| 269 |
return (offsetStr == null) ? null : ZoneOffset.of(offsetStr); |
| 270 |
} |
| 271 |
|
| 272 |
private int parseInt(int... group) { |
| 273 |
for (int g : group) { |
| 274 |
String s = matcher.group(g); |
| 275 |
if (s != null) { |
| 276 |
return Integer.parseInt(s); |
| 277 |
} |
| 278 |
} |
| 279 |
throw new NullPointerException(); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public static boolean hasTime(TemporalAccessor temporalAccessor) { |
| 289 |
return temporalAccessor instanceof Instant || temporalAccessor.isSupported(ChronoField.HOUR_OF_DAY); |
| 290 |
} |
| 291 |
|
| 292 |
private static boolean hasOffset(TemporalAccessor temporalAccessor) { |
| 293 |
return temporalAccessor.isSupported(ChronoField.OFFSET_SECONDS); |
| 294 |
} |
| 295 |
} |