3 THIS IS A SUBSET OF THE FULL LIBTAI. CHANGES HAVE BEEN MADE TO SUIT
4 LIBKUMU STYLE AND TYPE CONVENTIONS. ALL BUGS BELONG TO JOHN HURST.
5 THE FOLLOWING IS FOR ATTRIBUTION, THANK YOU MR. BERNSTEIN FOR WRITING
6 AND DISTRIBUTING SUCH GREAT SOFTWARE:
11 D. J. Bernstein, djb@pobox.com
12 http://pobox.com/~djb/libtai.html
15 libtai is a library for storing and manipulating dates and times.
17 libtai supports two time scales: (1) TAI64, covering a few hundred
18 billion years with 1-second precision; (2) TAI64NA, covering the same
19 period with 1-attosecond precision. Both scales are defined in terms of
20 TAI, the current international real time standard.
22 libtai provides an internal format for TAI64, struct tai, designed for
23 fast time manipulations. The tai_pack() and tai_unpack() routines
24 convert between struct tai and a portable 8-byte TAI64 storage format.
25 libtai provides similar internal and external formats for TAI64NA.
27 libtai provides struct caldate to store dates in year-month-day form. It
28 can convert struct caldate, under the Gregorian calendar, to a modified
29 Julian day number for easy date arithmetic.
31 libtai provides struct caltime to store calendar dates and times along
32 with UTC offsets. It can convert from struct tai to struct caltime in
33 UTC, accounting for leap seconds, for accurate date and time display. It
34 can also convert back from struct caltime to struct tai for user input.
35 Its overall UTC-to-TAI conversion speed is 100x better than the usual
36 UNIX mktime() implementation.
38 This version of libtai requires a UNIX system with gettimeofday(). It
39 will be easy to port to other operating systems with compilers
40 supporting 64-bit arithmetic.
42 The libtai source code is in the public domain.
48 \brief portable time functions
56 caldate_frommjd(Kumu::TAI::caldate* cd, i32_t day)
59 i32_t year, month, yday;
64 while (day >= 146097L) { day -= 146097L; ++year; }
66 /* year * 146097 + day - 678881 is MJD; 0 <= day < 146097 */
67 /* 2000-03-01, MJD 51604, is year 5, day 0 */
70 if (day == 146096L) { year += 3; day = 36524L; }
71 else { year += day / 36524L; day %= 36524L; }
78 if (day == 1460) { year += 3; day = 365; }
79 else { year += day / 365; day %= 365; }
83 month = (day + 5) / 306;
84 day = (day + 5) % 306;
86 if (month >= 10) { yday -= 306; ++year; month -= 10; }
87 else { yday += 59; month += 2; }
90 cd->month = month + 1;
95 static ui32_t times365[4] = { 0, 365, 730, 1095 } ;
96 static ui32_t times36524[4] = { 0, 36524UL, 73048UL, 109572UL } ;
97 static ui32_t montab[12] =
98 { 0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337 } ;
99 /* month length after february is (306 * m + 5) / 10 */
103 caldate_mjd(const Kumu::TAI::caldate* cd)
108 d = cd->day - 678882L;
112 d += 146097L * (y / 400);
115 if (m >= 2) m -= 2; else { m += 10; --y; }
119 if (m < 0) { m += 12; --y; }
123 d += 146097L * (y / 400);
125 if (y < 0) { y += 400; d -= 146097L; }
127 d += times365[y & 3];
130 d += 1461L * (y % 25);
133 d += times36524[y & 3];
141 caltime_utc(Kumu::TAI::caltime* ct, const Kumu::TAI::tai* t)
144 Kumu::TAI::tai t2 = *t;
148 /* XXX: check for overfow? */
153 s = u % ui64_C(86400);
155 ct->second = (s % 60); s /= 60;
156 ct->minute = s % 60; s /= 60;
160 caldate_frommjd(&ct->date,/*XXX*/(i32_t) (u - ui64_C(53375995543064)));
167 caltime_tai(const Kumu::TAI::caltime* ct, Kumu::TAI::tai* t)
172 /* XXX: check for overflow? */
174 day = caldate_mjd(&ct->date);
176 s = ct->hour * 60 + ct->minute;
177 s = (s - ct->offset) * 60 + ct->second;
179 t->x = day * ui64_C(86400) + ui64_C(4611686014920671114) + (i64_t)s;
184 Kumu::TAI::tai::now()
187 gettimeofday(&now, 0);
188 x = ui64_C(4611686018427387914) + (ui64_t)now.tv_sec;
192 const Kumu::TAI::tai&
193 Kumu::TAI::tai::operator=(const Kumu::TAI::caltime& rhs)
195 caltime_tai(&rhs, this);
200 const Kumu::TAI::caltime&
201 Kumu::TAI::caltime::operator=(const Kumu::TAI::tai& rhs)
203 caltime_utc(this, &rhs);