|
| 1 | +// Civil date conversion algorithms adapted from Howard Hinnant's date algorithms. |
| 2 | +// Placed in the public domain by Howard Hinnant. |
| 3 | +// https://howardhinnant.github.io/date_algorithms.html |
| 4 | + |
| 5 | +package io.sentry.vendor; |
| 6 | + |
| 7 | +import org.jetbrains.annotations.ApiStatus; |
| 8 | +import org.jetbrains.annotations.NotNull; |
| 9 | + |
| 10 | +@ApiStatus.Internal |
| 11 | +public final class SentryIso8601Utils { |
| 12 | + |
| 13 | + private static final long MILLIS_PER_SECOND = 1000L; |
| 14 | + private static final long MILLIS_PER_MINUTE = 60L * MILLIS_PER_SECOND; |
| 15 | + private static final long MILLIS_PER_HOUR = 60L * MILLIS_PER_MINUTE; |
| 16 | + private static final long MILLIS_PER_DAY = 24L * MILLIS_PER_HOUR; |
| 17 | + private static final int DAYS_0000_TO_1970 = 719468; |
| 18 | + |
| 19 | + private SentryIso8601Utils() {} |
| 20 | + |
| 21 | + public static long parseTimestamp(final @NotNull String timestamp) { |
| 22 | + final int length = timestamp.length(); |
| 23 | + int offset = 0; |
| 24 | + |
| 25 | + final int year = parseInt(timestamp, offset, offset += 4); |
| 26 | + if (checkOffset(timestamp, offset, '-')) { |
| 27 | + offset++; |
| 28 | + } |
| 29 | + |
| 30 | + final int month = parseInt(timestamp, offset, offset += 2); |
| 31 | + if (checkOffset(timestamp, offset, '-')) { |
| 32 | + offset++; |
| 33 | + } |
| 34 | + |
| 35 | + final int day = parseInt(timestamp, offset, offset += 2); |
| 36 | + validateDate(year, month, day); |
| 37 | + |
| 38 | + if (!checkOffset(timestamp, offset, 'T')) { |
| 39 | + if (offset != length) { |
| 40 | + throw new IllegalArgumentException("Invalid date separator"); |
| 41 | + } |
| 42 | + return epochMillis(year, month, day, 0, 0, 0, 0, 0); |
| 43 | + } |
| 44 | + offset++; |
| 45 | + |
| 46 | + final int hour = parseInt(timestamp, offset, offset += 2); |
| 47 | + if (checkOffset(timestamp, offset, ':')) { |
| 48 | + offset++; |
| 49 | + } |
| 50 | + |
| 51 | + final int minute = parseInt(timestamp, offset, offset += 2); |
| 52 | + if (checkOffset(timestamp, offset, ':')) { |
| 53 | + offset++; |
| 54 | + } |
| 55 | + |
| 56 | + int second = 0; |
| 57 | + int millisecond = 0; |
| 58 | + if (length > offset) { |
| 59 | + final char c = timestamp.charAt(offset); |
| 60 | + if (c != 'Z' && c != '+' && c != '-') { |
| 61 | + second = parseInt(timestamp, offset, offset += 2); |
| 62 | + if (second > 59 && second < 63) { |
| 63 | + second = 59; |
| 64 | + } |
| 65 | + if (checkOffset(timestamp, offset, '.')) { |
| 66 | + offset++; |
| 67 | + final int endOffset = indexOfNonDigit(timestamp, offset); |
| 68 | + if (endOffset == offset) { |
| 69 | + throw new IllegalArgumentException("Missing millisecond digits"); |
| 70 | + } |
| 71 | + final int parseEndOffset = Math.min(endOffset, offset + 3); |
| 72 | + final int fraction = parseInt(timestamp, offset, parseEndOffset); |
| 73 | + switch (parseEndOffset - offset) { |
| 74 | + case 1: |
| 75 | + millisecond = fraction * 100; |
| 76 | + break; |
| 77 | + case 2: |
| 78 | + millisecond = fraction * 10; |
| 79 | + break; |
| 80 | + default: |
| 81 | + millisecond = fraction; |
| 82 | + break; |
| 83 | + } |
| 84 | + offset = endOffset; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + validateTime(hour, minute, second, millisecond); |
| 89 | + |
| 90 | + if (length <= offset) { |
| 91 | + throw new IllegalArgumentException("No time zone indicator"); |
| 92 | + } |
| 93 | + |
| 94 | + final int timezoneOffsetMillis; |
| 95 | + final char timezoneIndicator = timestamp.charAt(offset); |
| 96 | + if (timezoneIndicator == 'Z') { |
| 97 | + timezoneOffsetMillis = 0; |
| 98 | + offset++; |
| 99 | + } else if (timezoneIndicator == '+' || timezoneIndicator == '-') { |
| 100 | + final int sign = timezoneIndicator == '+' ? 1 : -1; |
| 101 | + offset++; |
| 102 | + final int timezoneHour = parseInt(timestamp, offset, offset += 2); |
| 103 | + int timezoneMinute = 0; |
| 104 | + if (checkOffset(timestamp, offset, ':')) { |
| 105 | + offset++; |
| 106 | + } |
| 107 | + if (length >= offset + 2) { |
| 108 | + timezoneMinute = parseInt(timestamp, offset, offset += 2); |
| 109 | + } |
| 110 | + validateTimezone(timezoneHour, timezoneMinute); |
| 111 | + timezoneOffsetMillis = |
| 112 | + sign * (int) (timezoneHour * MILLIS_PER_HOUR + timezoneMinute * MILLIS_PER_MINUTE); |
| 113 | + } else { |
| 114 | + throw new IllegalArgumentException("Invalid time zone indicator"); |
| 115 | + } |
| 116 | + |
| 117 | + if (offset != length) { |
| 118 | + throw new IllegalArgumentException("Invalid trailing characters"); |
| 119 | + } |
| 120 | + |
| 121 | + return epochMillis(year, month, day, hour, minute, second, millisecond, timezoneOffsetMillis); |
| 122 | + } |
| 123 | + |
| 124 | + public static @NotNull String formatTimestamp(final long millis) { |
| 125 | + final long epochDay = Math.floorDiv(millis, MILLIS_PER_DAY); |
| 126 | + int millisOfDay = (int) Math.floorMod(millis, MILLIS_PER_DAY); |
| 127 | + |
| 128 | + final int[] yearMonthDay = epochDayToYearMonthDay(epochDay); |
| 129 | + final int hour = millisOfDay / (int) MILLIS_PER_HOUR; |
| 130 | + millisOfDay -= hour * (int) MILLIS_PER_HOUR; |
| 131 | + final int minute = millisOfDay / (int) MILLIS_PER_MINUTE; |
| 132 | + millisOfDay -= minute * (int) MILLIS_PER_MINUTE; |
| 133 | + final int second = millisOfDay / (int) MILLIS_PER_SECOND; |
| 134 | + final int millisecond = millisOfDay - second * (int) MILLIS_PER_SECOND; |
| 135 | + |
| 136 | + final StringBuilder timestamp = new StringBuilder("yyyy-MM-ddThh:mm:ss.sssZ".length()); |
| 137 | + padInt(timestamp, yearMonthDay[0], "yyyy".length()); |
| 138 | + timestamp.append('-'); |
| 139 | + padInt(timestamp, yearMonthDay[1], "MM".length()); |
| 140 | + timestamp.append('-'); |
| 141 | + padInt(timestamp, yearMonthDay[2], "dd".length()); |
| 142 | + timestamp.append('T'); |
| 143 | + padInt(timestamp, hour, "hh".length()); |
| 144 | + timestamp.append(':'); |
| 145 | + padInt(timestamp, minute, "mm".length()); |
| 146 | + timestamp.append(':'); |
| 147 | + padInt(timestamp, second, "ss".length()); |
| 148 | + timestamp.append('.'); |
| 149 | + padInt(timestamp, millisecond, "sss".length()); |
| 150 | + timestamp.append('Z'); |
| 151 | + return timestamp.toString(); |
| 152 | + } |
| 153 | + |
| 154 | + private static long epochMillis( |
| 155 | + final int year, |
| 156 | + final int month, |
| 157 | + final int day, |
| 158 | + final int hour, |
| 159 | + final int minute, |
| 160 | + final int second, |
| 161 | + final int millisecond, |
| 162 | + final int timezoneOffsetMillis) { |
| 163 | + return daysFromYearMonthDay(year, month, day) * MILLIS_PER_DAY |
| 164 | + + hour * MILLIS_PER_HOUR |
| 165 | + + minute * MILLIS_PER_MINUTE |
| 166 | + + second * MILLIS_PER_SECOND |
| 167 | + + millisecond |
| 168 | + - timezoneOffsetMillis; |
| 169 | + } |
| 170 | + |
| 171 | + private static long daysFromYearMonthDay(int year, final int month, final int day) { |
| 172 | + year -= month <= 2 ? 1 : 0; |
| 173 | + final long era = Math.floorDiv(year, 400); |
| 174 | + final int yearOfEra = (int) (year - era * 400); |
| 175 | + final int dayOfYear = (153 * (month + (month > 2 ? -3 : 9)) + 2) / 5 + day - 1; |
| 176 | + final int dayOfEra = yearOfEra * 365 + yearOfEra / 4 - yearOfEra / 100 + dayOfYear; |
| 177 | + return era * 146097 + dayOfEra - DAYS_0000_TO_1970; |
| 178 | + } |
| 179 | + |
| 180 | + private static int[] epochDayToYearMonthDay(long epochDay) { |
| 181 | + epochDay += DAYS_0000_TO_1970; |
| 182 | + final long era = Math.floorDiv(epochDay, 146097); |
| 183 | + final int dayOfEra = (int) (epochDay - era * 146097); |
| 184 | + final int yearOfEra = (dayOfEra - dayOfEra / 1460 + dayOfEra / 36524 - dayOfEra / 146096) / 365; |
| 185 | + final int year = (int) (yearOfEra + era * 400); |
| 186 | + final int dayOfYear = dayOfEra - (365 * yearOfEra + yearOfEra / 4 - yearOfEra / 100); |
| 187 | + final int monthPrime = (5 * dayOfYear + 2) / 153; |
| 188 | + final int day = dayOfYear - (153 * monthPrime + 2) / 5 + 1; |
| 189 | + final int month = monthPrime < 10 ? monthPrime + 3 : monthPrime - 9; |
| 190 | + return new int[] {year + (month <= 2 ? 1 : 0), month, day}; |
| 191 | + } |
| 192 | + |
| 193 | + private static void validateDate(final int year, final int month, final int day) { |
| 194 | + if (year < 1 || month < 1 || month > 12 || day < 1 || day > daysInMonth(year, month)) { |
| 195 | + throw new IllegalArgumentException("Invalid date"); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private static void validateTime( |
| 200 | + final int hour, final int minute, final int second, final int millisecond) { |
| 201 | + if (hour < 0 |
| 202 | + || hour > 23 |
| 203 | + || minute < 0 |
| 204 | + || minute > 59 |
| 205 | + || second < 0 |
| 206 | + || second > 59 |
| 207 | + || millisecond < 0 |
| 208 | + || millisecond > 999) { |
| 209 | + throw new IllegalArgumentException("Invalid time"); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + private static void validateTimezone(final int hour, final int minute) { |
| 214 | + if (hour < 0 || hour > 23 || minute < 0 || minute > 59) { |
| 215 | + throw new IllegalArgumentException("Invalid time zone"); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + private static int daysInMonth(final int year, final int month) { |
| 220 | + switch (month) { |
| 221 | + case 2: |
| 222 | + return isLeapYear(year) ? 29 : 28; |
| 223 | + case 4: |
| 224 | + case 6: |
| 225 | + case 9: |
| 226 | + case 11: |
| 227 | + return 30; |
| 228 | + default: |
| 229 | + return 31; |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + private static boolean isLeapYear(final int year) { |
| 234 | + return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0); |
| 235 | + } |
| 236 | + |
| 237 | + private static boolean checkOffset( |
| 238 | + final @NotNull String value, final int offset, final char expected) { |
| 239 | + return offset < value.length() && value.charAt(offset) == expected; |
| 240 | + } |
| 241 | + |
| 242 | + private static int parseInt( |
| 243 | + final @NotNull String value, final int beginIndex, final int endIndex) { |
| 244 | + if (beginIndex < 0 || endIndex > value.length() || beginIndex >= endIndex) { |
| 245 | + throw new NumberFormatException(value); |
| 246 | + } |
| 247 | + |
| 248 | + int result = 0; |
| 249 | + for (int i = beginIndex; i < endIndex; i++) { |
| 250 | + final char c = value.charAt(i); |
| 251 | + if (c < '0' || c > '9') { |
| 252 | + throw new NumberFormatException("Invalid number: " + value.substring(beginIndex, endIndex)); |
| 253 | + } |
| 254 | + result = result * 10 + c - '0'; |
| 255 | + } |
| 256 | + return result; |
| 257 | + } |
| 258 | + |
| 259 | + private static void padInt( |
| 260 | + final @NotNull StringBuilder buffer, final int value, final int length) { |
| 261 | + if (value < 0) { |
| 262 | + buffer.append('-'); |
| 263 | + padInt(buffer, -value, length); |
| 264 | + return; |
| 265 | + } |
| 266 | + final String strValue = Integer.toString(value); |
| 267 | + for (int i = length - strValue.length(); i > 0; i--) { |
| 268 | + buffer.append('0'); |
| 269 | + } |
| 270 | + buffer.append(strValue); |
| 271 | + } |
| 272 | + |
| 273 | + private static int indexOfNonDigit(final @NotNull String string, final int offset) { |
| 274 | + for (int i = offset; i < string.length(); i++) { |
| 275 | + final char c = string.charAt(i); |
| 276 | + if (c < '0' || c > '9') { |
| 277 | + return i; |
| 278 | + } |
| 279 | + } |
| 280 | + return string.length(); |
| 281 | + } |
| 282 | +} |
0 commit comments