juce_Time.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. namespace TimeHelpers
  22. {
  23. static std::tm millisToLocal (int64 millis) noexcept
  24. {
  25. #if JUCE_WINDOWS && JUCE_MINGW
  26. time_t now = (time_t) (millis / 1000);
  27. return *localtime (&now);
  28. #elif JUCE_WINDOWS
  29. std::tm result;
  30. millis /= 1000;
  31. if (_localtime64_s (&result, &millis) != 0)
  32. zerostruct (result);
  33. return result;
  34. #else
  35. std::tm result;
  36. time_t now = (time_t) (millis / 1000);
  37. if (localtime_r (&now, &result) == nullptr)
  38. zerostruct (result);
  39. return result;
  40. #endif
  41. }
  42. static std::tm millisToUTC (int64 millis) noexcept
  43. {
  44. #if JUCE_WINDOWS && JUCE_MINGW
  45. time_t now = (time_t) (millis / 1000);
  46. return *gmtime (&now);
  47. #elif JUCE_WINDOWS
  48. std::tm result;
  49. millis /= 1000;
  50. if (_gmtime64_s (&result, &millis) != 0)
  51. zerostruct (result);
  52. return result;
  53. #else
  54. std::tm result;
  55. time_t now = (time_t) (millis / 1000);
  56. if (gmtime_r (&now, &result) == nullptr)
  57. zerostruct (result);
  58. return result;
  59. #endif
  60. }
  61. static int getUTCOffsetSeconds (const int64 millis) noexcept
  62. {
  63. std::tm utc = millisToUTC (millis);
  64. utc.tm_isdst = -1; // Treat this UTC time as local to find the offset
  65. return (int) ((millis / 1000) - (int64) mktime (&utc));
  66. }
  67. static int extendedModulo (const int64 value, const int modulo) noexcept
  68. {
  69. return (int) (value >= 0 ? (value % modulo)
  70. : (value - ((value / modulo) + 1) * modulo));
  71. }
  72. static inline String formatString (const String& format, const std::tm* const tm)
  73. {
  74. #if JUCE_ANDROID
  75. typedef CharPointer_UTF8 StringType;
  76. #elif JUCE_WINDOWS
  77. typedef CharPointer_UTF16 StringType;
  78. #else
  79. typedef CharPointer_UTF32 StringType;
  80. #endif
  81. #ifdef JUCE_MSVC
  82. if (tm->tm_year < -1900 || tm->tm_year > 8099)
  83. return String(); // Visual Studio's library can only handle 0 -> 9999 AD
  84. #endif
  85. for (size_t bufferSize = 256; ; bufferSize += 256)
  86. {
  87. HeapBlock<StringType::CharType> buffer (bufferSize);
  88. const size_t numChars =
  89. #if JUCE_ANDROID
  90. strftime (buffer, bufferSize - 1, format.toUTF8(), tm);
  91. #elif JUCE_WINDOWS
  92. wcsftime (buffer, bufferSize - 1, format.toWideCharPointer(), tm);
  93. #else
  94. wcsftime (buffer, bufferSize - 1, format.toUTF32(), tm);
  95. #endif
  96. if (numChars > 0 || format.isEmpty())
  97. return String (StringType (buffer),
  98. StringType (buffer) + (int) numChars);
  99. }
  100. }
  101. //==============================================================================
  102. static inline bool isLeapYear (int year) noexcept
  103. {
  104. return (year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0));
  105. }
  106. static inline int daysFromJan1 (int year, int month) noexcept
  107. {
  108. const short dayOfYear[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
  109. 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
  110. return dayOfYear [(isLeapYear (year) ? 12 : 0) + month];
  111. }
  112. static inline int64 daysFromYear0 (int year) noexcept
  113. {
  114. --year;
  115. return 365 * year + (year / 400) - (year / 100) + (year / 4);
  116. }
  117. static inline int64 daysFrom1970 (int year) noexcept
  118. {
  119. return daysFromYear0 (year) - daysFromYear0 (1970);
  120. }
  121. static inline int64 daysFrom1970 (int year, int month) noexcept
  122. {
  123. if (month > 11)
  124. {
  125. year += month / 12;
  126. month %= 12;
  127. }
  128. else if (month < 0)
  129. {
  130. const int numYears = (11 - month) / 12;
  131. year -= numYears;
  132. month += 12 * numYears;
  133. }
  134. return daysFrom1970 (year) + daysFromJan1 (year, month);
  135. }
  136. // There's no posix function that does a UTC version of mktime,
  137. // so annoyingly we need to implement this manually..
  138. static inline int64 mktime_utc (const std::tm& t) noexcept
  139. {
  140. return 24 * 3600 * (daysFrom1970 (t.tm_year + 1900, t.tm_mon) + (t.tm_mday - 1))
  141. + 3600 * t.tm_hour
  142. + 60 * t.tm_min
  143. + t.tm_sec;
  144. }
  145. static uint32 lastMSCounterValue = 0;
  146. }
  147. //==============================================================================
  148. Time::Time() noexcept : millisSinceEpoch (0)
  149. {
  150. }
  151. Time::Time (const Time& other) noexcept : millisSinceEpoch (other.millisSinceEpoch)
  152. {
  153. }
  154. Time::Time (const int64 ms) noexcept : millisSinceEpoch (ms)
  155. {
  156. }
  157. Time::Time (const int year,
  158. const int month,
  159. const int day,
  160. const int hours,
  161. const int minutes,
  162. const int seconds,
  163. const int milliseconds,
  164. const bool useLocalTime) noexcept
  165. {
  166. std::tm t;
  167. t.tm_year = year - 1900;
  168. t.tm_mon = month;
  169. t.tm_mday = day;
  170. t.tm_hour = hours;
  171. t.tm_min = minutes;
  172. t.tm_sec = seconds;
  173. t.tm_isdst = -1;
  174. millisSinceEpoch = 1000 * (useLocalTime ? (int64) mktime (&t)
  175. : TimeHelpers::mktime_utc (t))
  176. + milliseconds;
  177. }
  178. Time::~Time() noexcept
  179. {
  180. }
  181. Time& Time::operator= (const Time& other) noexcept
  182. {
  183. millisSinceEpoch = other.millisSinceEpoch;
  184. return *this;
  185. }
  186. //==============================================================================
  187. int64 Time::currentTimeMillis() noexcept
  188. {
  189. #if JUCE_WINDOWS && ! JUCE_MINGW
  190. struct _timeb t;
  191. _ftime_s (&t);
  192. return ((int64) t.time) * 1000 + t.millitm;
  193. #else
  194. struct timeval tv;
  195. gettimeofday (&tv, nullptr);
  196. return ((int64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
  197. #endif
  198. }
  199. Time JUCE_CALLTYPE Time::getCurrentTime() noexcept
  200. {
  201. return Time (currentTimeMillis());
  202. }
  203. //==============================================================================
  204. uint32 juce_millisecondsSinceStartup() noexcept;
  205. uint32 Time::getMillisecondCounter() noexcept
  206. {
  207. const uint32 now = juce_millisecondsSinceStartup();
  208. if (now < TimeHelpers::lastMSCounterValue)
  209. {
  210. // in multi-threaded apps this might be called concurrently, so
  211. // make sure that our last counter value only increases and doesn't
  212. // go backwards..
  213. if (now < TimeHelpers::lastMSCounterValue - 1000)
  214. TimeHelpers::lastMSCounterValue = now;
  215. }
  216. else
  217. {
  218. TimeHelpers::lastMSCounterValue = now;
  219. }
  220. return now;
  221. }
  222. uint32 Time::getApproximateMillisecondCounter() noexcept
  223. {
  224. if (TimeHelpers::lastMSCounterValue == 0)
  225. getMillisecondCounter();
  226. return TimeHelpers::lastMSCounterValue;
  227. }
  228. void Time::waitForMillisecondCounter (const uint32 targetTime) noexcept
  229. {
  230. for (;;)
  231. {
  232. const uint32 now = getMillisecondCounter();
  233. if (now >= targetTime)
  234. break;
  235. const int toWait = (int) (targetTime - now);
  236. if (toWait > 2)
  237. {
  238. Thread::sleep (jmin (20, toWait >> 1));
  239. }
  240. else
  241. {
  242. // xxx should consider using mutex_pause on the mac as it apparently
  243. // makes it seem less like a spinlock and avoids lowering the thread pri.
  244. for (int i = 10; --i >= 0;)
  245. Thread::yield();
  246. }
  247. }
  248. }
  249. //==============================================================================
  250. double Time::highResolutionTicksToSeconds (const int64 ticks) noexcept
  251. {
  252. return ticks / (double) getHighResolutionTicksPerSecond();
  253. }
  254. int64 Time::secondsToHighResolutionTicks (const double seconds) noexcept
  255. {
  256. return (int64) (seconds * (double) getHighResolutionTicksPerSecond());
  257. }
  258. //==============================================================================
  259. String Time::toString (const bool includeDate,
  260. const bool includeTime,
  261. const bool includeSeconds,
  262. const bool use24HourClock) const noexcept
  263. {
  264. String result;
  265. if (includeDate)
  266. {
  267. result << getDayOfMonth() << ' '
  268. << getMonthName (true) << ' '
  269. << getYear();
  270. if (includeTime)
  271. result << ' ';
  272. }
  273. if (includeTime)
  274. {
  275. const int mins = getMinutes();
  276. result << (use24HourClock ? getHours() : getHoursInAmPmFormat())
  277. << (mins < 10 ? ":0" : ":") << mins;
  278. if (includeSeconds)
  279. {
  280. const int secs = getSeconds();
  281. result << (secs < 10 ? ":0" : ":") << secs;
  282. }
  283. if (! use24HourClock)
  284. result << (isAfternoon() ? "pm" : "am");
  285. }
  286. return result.trimEnd();
  287. }
  288. String Time::formatted (const String& format) const
  289. {
  290. std::tm t (TimeHelpers::millisToLocal (millisSinceEpoch));
  291. return TimeHelpers::formatString (format, &t);
  292. }
  293. //==============================================================================
  294. int Time::getYear() const noexcept { return TimeHelpers::millisToLocal (millisSinceEpoch).tm_year + 1900; }
  295. int Time::getMonth() const noexcept { return TimeHelpers::millisToLocal (millisSinceEpoch).tm_mon; }
  296. int Time::getDayOfYear() const noexcept { return TimeHelpers::millisToLocal (millisSinceEpoch).tm_yday; }
  297. int Time::getDayOfMonth() const noexcept { return TimeHelpers::millisToLocal (millisSinceEpoch).tm_mday; }
  298. int Time::getDayOfWeek() const noexcept { return TimeHelpers::millisToLocal (millisSinceEpoch).tm_wday; }
  299. int Time::getHours() const noexcept { return TimeHelpers::millisToLocal (millisSinceEpoch).tm_hour; }
  300. int Time::getMinutes() const noexcept { return TimeHelpers::millisToLocal (millisSinceEpoch).tm_min; }
  301. int Time::getSeconds() const noexcept { return TimeHelpers::extendedModulo (millisSinceEpoch / 1000, 60); }
  302. int Time::getMilliseconds() const noexcept { return TimeHelpers::extendedModulo (millisSinceEpoch, 1000); }
  303. int Time::getHoursInAmPmFormat() const noexcept
  304. {
  305. const int hours = getHours();
  306. if (hours == 0) return 12;
  307. if (hours <= 12) return hours;
  308. return hours - 12;
  309. }
  310. bool Time::isAfternoon() const noexcept
  311. {
  312. return getHours() >= 12;
  313. }
  314. bool Time::isDaylightSavingTime() const noexcept
  315. {
  316. return TimeHelpers::millisToLocal (millisSinceEpoch).tm_isdst != 0;
  317. }
  318. String Time::getTimeZone() const noexcept
  319. {
  320. String zone[2];
  321. #if JUCE_WINDOWS
  322. #if JUCE_MSVC || JUCE_CLANG
  323. _tzset();
  324. for (int i = 0; i < 2; ++i)
  325. {
  326. char name[128] = { 0 };
  327. size_t length;
  328. _get_tzname (&length, name, 127, i);
  329. zone[i] = name;
  330. }
  331. #else
  332. #warning "Can't find a replacement for tzset on mingw - ideas welcome!"
  333. #endif
  334. #else
  335. tzset();
  336. const char** const zonePtr = (const char**) tzname;
  337. zone[0] = zonePtr[0];
  338. zone[1] = zonePtr[1];
  339. #endif
  340. if (isDaylightSavingTime())
  341. {
  342. zone[0] = zone[1];
  343. if (zone[0].length() > 3
  344. && zone[0].containsIgnoreCase ("daylight")
  345. && zone[0].contains ("GMT"))
  346. zone[0] = "BST";
  347. }
  348. return zone[0].substring (0, 3);
  349. }
  350. int Time::getUTCOffsetSeconds() const noexcept
  351. {
  352. return TimeHelpers::getUTCOffsetSeconds (millisSinceEpoch);
  353. }
  354. String Time::getUTCOffsetString (bool includeSemiColon) const
  355. {
  356. if (int seconds = getUTCOffsetSeconds())
  357. {
  358. const int minutes = seconds / 60;
  359. return String::formatted (includeSemiColon ? "%+03d:%02d"
  360. : "%+03d%02d",
  361. minutes / 60,
  362. minutes % 60);
  363. }
  364. return "Z";
  365. }
  366. String Time::toISO8601 (bool includeDividerCharacters) const
  367. {
  368. return String::formatted (includeDividerCharacters ? "%04d-%02d-%02dT%02d:%02d:%06.03f"
  369. : "%04d%02d%02dT%02d%02d%06.03f",
  370. getYear(),
  371. getMonth() + 1,
  372. getDayOfMonth(),
  373. getHours(),
  374. getMinutes(),
  375. getSeconds() + getMilliseconds() / 1000.0)
  376. + getUTCOffsetString (includeDividerCharacters);
  377. }
  378. static int parseFixedSizeIntAndSkip (String::CharPointerType& t, int numChars, char charToSkip) noexcept
  379. {
  380. int n = 0;
  381. for (int i = numChars; --i >= 0;)
  382. {
  383. const int digit = (int) (*t - '0');
  384. if (! isPositiveAndBelow (digit, 10))
  385. return -1;
  386. ++t;
  387. n = n * 10 + digit;
  388. }
  389. if (charToSkip != 0 && *t == (juce_wchar) charToSkip)
  390. ++t;
  391. return n;
  392. }
  393. Time Time::fromISO8601 (StringRef iso) noexcept
  394. {
  395. String::CharPointerType t = iso.text;
  396. const int year = parseFixedSizeIntAndSkip (t, 4, '-');
  397. if (year < 0)
  398. return Time();
  399. const int month = parseFixedSizeIntAndSkip (t, 2, '-');
  400. if (month < 0)
  401. return Time();
  402. const int day = parseFixedSizeIntAndSkip (t, 2, 0);
  403. if (day < 0)
  404. return Time();
  405. int hours = 0, minutes = 0, milliseconds = 0;
  406. if (*t == 'T')
  407. {
  408. ++t;
  409. hours = parseFixedSizeIntAndSkip (t, 2, ':');
  410. if (hours < 0)
  411. return Time();
  412. minutes = parseFixedSizeIntAndSkip (t, 2, ':');
  413. if (minutes < 0)
  414. return Time();
  415. milliseconds = (int) (1000.0 * CharacterFunctions::readDoubleValue (t));
  416. }
  417. const juce_wchar nextChar = t.getAndAdvance();
  418. if (nextChar == '-' || nextChar == '+')
  419. {
  420. const int offsetHours = parseFixedSizeIntAndSkip (t, 2, ':');
  421. if (offsetHours < 0)
  422. return Time();
  423. const int offsetMinutes = parseFixedSizeIntAndSkip (t, 2, 0);
  424. if (offsetMinutes < 0)
  425. return Time();
  426. const int offsetMs = (offsetHours * 60 + offsetMinutes) * 60 * 1000;
  427. milliseconds += nextChar == '-' ? offsetMs : -offsetMs; // NB: this seems backwards but is correct!
  428. }
  429. else if (nextChar != 0 && nextChar != 'Z')
  430. {
  431. return Time();
  432. }
  433. return Time (year, month - 1, day, hours, minutes, 0, milliseconds, false);
  434. }
  435. String Time::getMonthName (const bool threeLetterVersion) const
  436. {
  437. return getMonthName (getMonth(), threeLetterVersion);
  438. }
  439. String Time::getWeekdayName (const bool threeLetterVersion) const
  440. {
  441. return getWeekdayName (getDayOfWeek(), threeLetterVersion);
  442. }
  443. static const char* const shortMonthNames[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  444. static const char* const longMonthNames[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  445. String Time::getMonthName (int monthNumber, const bool threeLetterVersion)
  446. {
  447. monthNumber %= 12;
  448. return TRANS (threeLetterVersion ? shortMonthNames [monthNumber]
  449. : longMonthNames [monthNumber]);
  450. }
  451. String Time::getWeekdayName (int day, const bool threeLetterVersion)
  452. {
  453. static const char* const shortDayNames[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  454. static const char* const longDayNames[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
  455. day %= 7;
  456. return TRANS (threeLetterVersion ? shortDayNames [day]
  457. : longDayNames [day]);
  458. }
  459. //==============================================================================
  460. Time& Time::operator+= (RelativeTime delta) noexcept { millisSinceEpoch += delta.inMilliseconds(); return *this; }
  461. Time& Time::operator-= (RelativeTime delta) noexcept { millisSinceEpoch -= delta.inMilliseconds(); return *this; }
  462. Time operator+ (Time time, RelativeTime delta) noexcept { Time t (time); return t += delta; }
  463. Time operator- (Time time, RelativeTime delta) noexcept { Time t (time); return t -= delta; }
  464. Time operator+ (RelativeTime delta, Time time) noexcept { Time t (time); return t += delta; }
  465. const RelativeTime operator- (Time time1, Time time2) noexcept { return RelativeTime::milliseconds (time1.toMilliseconds() - time2.toMilliseconds()); }
  466. bool operator== (Time time1, Time time2) noexcept { return time1.toMilliseconds() == time2.toMilliseconds(); }
  467. bool operator!= (Time time1, Time time2) noexcept { return time1.toMilliseconds() != time2.toMilliseconds(); }
  468. bool operator< (Time time1, Time time2) noexcept { return time1.toMilliseconds() < time2.toMilliseconds(); }
  469. bool operator> (Time time1, Time time2) noexcept { return time1.toMilliseconds() > time2.toMilliseconds(); }
  470. bool operator<= (Time time1, Time time2) noexcept { return time1.toMilliseconds() <= time2.toMilliseconds(); }
  471. bool operator>= (Time time1, Time time2) noexcept { return time1.toMilliseconds() >= time2.toMilliseconds(); }
  472. static int getMonthNumberForCompileDate (const String& m) noexcept
  473. {
  474. for (int i = 0; i < 12; ++i)
  475. if (m.equalsIgnoreCase (shortMonthNames[i]))
  476. return i;
  477. // If you hit this because your compiler has an unusual __DATE__
  478. // format, let us know so we can add support for it!
  479. jassertfalse;
  480. return 0;
  481. }
  482. Time Time::getCompilationDate()
  483. {
  484. StringArray dateTokens, timeTokens;
  485. dateTokens.addTokens (__DATE__, true);
  486. dateTokens.removeEmptyStrings (true);
  487. timeTokens.addTokens (__TIME__, ":", StringRef());
  488. return Time (dateTokens[2].getIntValue(),
  489. getMonthNumberForCompileDate (dateTokens[0]),
  490. dateTokens[1].getIntValue(),
  491. timeTokens[0].getIntValue(),
  492. timeTokens[1].getIntValue());
  493. }
  494. //==============================================================================
  495. //==============================================================================
  496. #if JUCE_UNIT_TESTS
  497. class TimeTests : public UnitTest
  498. {
  499. public:
  500. TimeTests() : UnitTest ("Time") {}
  501. void runTest() override
  502. {
  503. beginTest ("Time");
  504. Time t = Time::getCurrentTime();
  505. expect (t > Time());
  506. Thread::sleep (15);
  507. expect (Time::getCurrentTime() > t);
  508. expect (t.getTimeZone().isNotEmpty());
  509. expect (t.getUTCOffsetString (true) == "Z" || t.getUTCOffsetString (true).length() == 6);
  510. expect (t.getUTCOffsetString (false) == "Z" || t.getUTCOffsetString (false).length() == 5);
  511. expect (Time::fromISO8601 (t.toISO8601 (true)) == t);
  512. expect (Time::fromISO8601 (t.toISO8601 (false)) == t);
  513. expect (Time::fromISO8601 ("2016-02-16") == Time (2016, 1, 16, 0, 0, 0, 0, false));
  514. expect (Time::fromISO8601 ("20160216Z") == Time (2016, 1, 16, 0, 0, 0, 0, false));
  515. expect (Time::fromISO8601 ("2016-02-16T15:03:57+00:00") == Time (2016, 1, 16, 15, 3, 57, 0, false));
  516. expect (Time::fromISO8601 ("20160216T150357+0000") == Time (2016, 1, 16, 15, 3, 57, 0, false));
  517. expect (Time::fromISO8601 ("2016-02-16T15:03:57.999+00:00") == Time (2016, 1, 16, 15, 3, 57, 999, false));
  518. expect (Time::fromISO8601 ("20160216T150357.999+0000") == Time (2016, 1, 16, 15, 3, 57, 999, false));
  519. expect (Time::fromISO8601 ("2016-02-16T15:03:57.999Z") == Time (2016, 1, 16, 15, 3, 57, 999, false));
  520. expect (Time::fromISO8601 ("20160216T150357.999Z") == Time (2016, 1, 16, 15, 3, 57, 999, false));
  521. expect (Time::fromISO8601 ("2016-02-16T15:03:57.999-02:30") == Time (2016, 1, 16, 17, 33, 57, 999, false));
  522. expect (Time::fromISO8601 ("20160216T150357.999-0230") == Time (2016, 1, 16, 17, 33, 57, 999, false));
  523. expect (Time (1970, 0, 1, 0, 0, 0, 0, false) == Time (0));
  524. expect (Time (2106, 1, 7, 6, 28, 15, 0, false) == Time (4294967295000));
  525. expect (Time (2007, 10, 7, 1, 7, 20, 0, false) == Time (1194397640000));
  526. expect (Time (2038, 0, 19, 3, 14, 7, 0, false) == Time (2147483647000));
  527. expect (Time (2016, 2, 7, 11, 20, 8, 0, false) == Time (1457349608000));
  528. expect (Time (1969, 11, 31, 23, 59, 59, 0, false) == Time (-1000));
  529. expect (Time (1901, 11, 13, 20, 45, 53, 0, false) == Time (-2147483647000));
  530. expect (Time (1982, 1, 1, 12, 0, 0, 0, true) + RelativeTime::days (365) == Time (1983, 1, 1, 12, 0, 0, 0, true));
  531. expect (Time (1970, 1, 1, 12, 0, 0, 0, true) + RelativeTime::days (365) == Time (1971, 1, 1, 12, 0, 0, 0, true));
  532. expect (Time (2038, 1, 1, 12, 0, 0, 0, true) + RelativeTime::days (365) == Time (2039, 1, 1, 12, 0, 0, 0, true));
  533. expect (Time (1982, 1, 1, 12, 0, 0, 0, false) + RelativeTime::days (365) == Time (1983, 1, 1, 12, 0, 0, 0, false));
  534. expect (Time (1970, 1, 1, 12, 0, 0, 0, false) + RelativeTime::days (365) == Time (1971, 1, 1, 12, 0, 0, 0, false));
  535. expect (Time (2038, 1, 1, 12, 0, 0, 0, false) + RelativeTime::days (365) == Time (2039, 1, 1, 12, 0, 0, 0, false));
  536. }
  537. };
  538. static TimeTests timeTests;
  539. #endif