Debian Codegraph @

terps/tads/tads3/vmtz.h

raw symbols SCIP
1 /*
2 * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 vmtz.h - TADS time zone management
10 Function
11 TADS uses the IANA zoneinfo database (aka the Olson database) for
12 converting time values between universal time (UTC) and local time
13 representations. This is used in particular in the Date intrinsic class,
14 which provides a bytecode API for time and date conversions, formatting,
15 and arithmetic.
16
17 This zoneinfo database lets us determine the correct local time, given a
18 UTC time, in just about any regional time zone on just about any date since
19 time zones were invented. It takes into account the ever-changing daylight
20 savings rules as well as historical time zone realignments and
21 redefinitions. We don't just project the current time zone rules back in
22 time, as most operating systems do in their time managers; the zoneinfo
23 database tells us the actual changes that occurred. For example, in the
24 United States, we'll properly start daylight savings time from the last
25 Sunday in April in 1986, but use the second Sunday in April in 2007 and
26 later. Likewise, we'll properly display EWT (Eastern War Time) if asked
27 for a local time in New York in 1943.
28
29 We use a custom binary version of the zoneinfo database. The TADS source
30 code distribution includes a compiler (written in TADS) that reads the
31 zoneinfo sources files as distributed by IANA and generates our custom
32 binary file. We don't compile the file merely to have it in binary form,
33 but rather because the source form of the information requires quite a lot
34 of work to interpret. It uses a considerable amount of abstraction, both
35 to make the zone histories and rules more readily human-readable and to
36 make them more compact and reusable (e.g., it allows for expressing rules
37 at the national level that span multiple time zones, such as in the US).
38 The main reason to compile the material is to boil down the abstraction to
39 a form that can be used efficiently at run-time. While we're at it we
40 generate a binary file format that's fast and easy to read.
41
42 This package includes a cache manager that reads zone data from the
43 binary file on demand. The zoneinfo database (and thus our custom binary
44 version of it) contains quite a lot of information, since it has historical
45 records on each of about 450 time zones around the world. It amounts to
46 about 300k of compiled binary data in our format. Rather than read all of
47 that into memory, we'll read it as needed per time zone. Most programs
48 will use only the local time zone of the local system, so in most cases
49 we'll never need to read more than one zone.
50 Notes
51 None
52 Modified
53 02/05/12 MJRoberts - creation
54 */
55
56 #ifndef VMTZ_H
57 #define VMTZ_H
58
59 #include "t3std.h"
60 #include "vmglob.h"
61 #include "vmobj.h"
62
63
64 /* ------------------------------------------------------------------------ */
65 /*
66 * Time zone cache. This loads time zone data on demand.
67 */
68 class CVmTimeZoneCache
69 {
70 friend class CVmTimeZone;
71
72 public:
73 CVmTimeZoneCache();
74 ~CVmTimeZoneCache();
75
76 /*
77 * Parse a timezone name, returning a CVmTimeZone object representing
78 * the zone. This accepts names in the following formats:
79 *
80 *. :local - the local zone
81 *
82 *. UTC+offset - or GMT+ofs, Z+ofs, or simply +ofs: a zone at the
83 *. given hh[:mm[:ss]] offset from UTC. This type of
84 *. zone uses a fixed offset year round and for all
85 *. dates, not tied to any location's history.
86 *
87 *. zoneinfoName - the name of a zoneinfo database entry, such as
88 *. "America/Los_Angeles". This type uses the zone
89 *. history from the database.
90 */
91 class CVmTimeZone *parse_zone(VMG_ const char *name, size_t len);
92
93 /*
94 * Get a time zone object by name from the zoneinfo database; returns
95 * the cached copy if present, otherwise loads the entry from disk.
96 */
97 class CVmTimeZone *get_db_zone(VMG_ const char *name, size_t len);
98 class CVmTimeZone *get_db_zone(VMG_ const char *name)
99 { return get_db_zone(vmg_ name, strlen(name)); }
100
101 /*
102 * Get a time zone object by GMT offset, in seconds. Returns a zone
103 * that represents the fixed GMT offset, not for any particular
104 * location.
105 */
106 class CVmTimeZone *get_gmtofs_zone(VMG_ int32_t gmtofs_secs);
107
108 /*
109 * Get a time zone object by abbreviation. This searches the
110 * abbreviation list for a match, and returns the corresponding
111 * timezone object if found. We also fill in the GMT offset for the
112 * abbreviation - this is required when the zone is specified by
113 * abbreviation because an abbreviation carries a standard time or
114 * daylight time designation, which we'd normally infer from the zone
115 * and date information. An explicit daylight or standard time
116 * designation overrides the time that's normally in effect on the
117 * given date. For example, "1/1/2012 12:00 PDT" corresopnds to
118 * 1/1/2012 19:00 GMT, whereas "1/1/2012 12:00" America/Los_Angeles
119 * corresponds to 20:00 GMT. The difference is that standard time is
120 * normally in effect on that date: a time specified by location
121 * (America/Los_Angeles) is interpreted as a wall clock time for that
122 * date according to the prevailing standard/daylight rules for the
123 * zone, whereas a time explicitly in PDT is interpreted as daylight
124 * time regardless of whether daylight or standard time is actually in
125 * effect in the zone on the given date.
126 */
127 class CVmTimeZone *get_zone_by_abbr(VMG_ int32_t *gmtofs_ms,
128 const char *name, size_t len);
129
130 /*
131 * Create a missing database zone. This creates a placeholder zone
132 * representing a zone imported from a saved game using a different
133 * version of the database that includes a zone that's not defined in
134 * the version we're using.
135 */
136 class CVmTimeZone *create_missing_zone(
137 VMG_ const char *name, size_t namelen, const os_tzinfo_t *desc);
138
139 /* open the zoneinfo binary file */
140 static osfildef *open_zoneinfo_file(VMG0_);
141
142 /*
143 * Get the system default local time zone. Returns a CVmTimeZone
144 * object representing the local zone.
145 */
146 class CVmTimeZone *get_local_zone(VMG0_);
147
148 /* is the given zone the special local zone? */
149 int is_local_zone(class CVmTimeZone *zone) const
150 { return zone == local_zone_; }
151
152 protected:
153 /* load the zoneinfo database index from the binary file */
154 int load_db_index(VMG0_);
155
156 /* get or load the CVmTimeZone object for a hash entry */
157 CVmTimeZone *tz_from_hash(VMG_ class ZoneHashEntry *entry);
158
159 /* parse a "UTC+-h[:mm[:ss]]" offset string into a zone */
160 CVmTimeZone *parse_zone_hhmmss(
161 VMG_ const char *prefix, const char *name, size_t len);
162
163 /* parse a zone spec in POSIX TZ format (EST5, EST5EDT, EST5EDT4) */
164 CVmTimeZone *parse_zone_posixTZ(VMG_ const char *name, size_t len);
165
166 /* parse an [+-]h[:mm[:ss]] time/offset value */
167 int parse_hhmmss(int32_t &ofs, const char *&name, size_t &len,
168 int sign_required);
169
170 /* search for a set of zone specifications */
171 class ZoneHashEntry *search(
172 VMG_ const struct ZoneSearchSpec *specs, int cnt);
173 class ZoneHashEntry *search(
174 VMG_ const struct ZoneSearchSpec *specs, class AbbrHashEntry **e,
175 int cnt, class ZoneHashEntry *zone);
176
177 /* have we attempted to load the index yet? */
178 int index_loaded_;
179
180 /* did we successfully load the index? */
181 int index_loaded_ok_;
182
183 /*
184 * Cached local zone object. Determining the local time zone can be a
185 * non-trivial amount of work, so we cache this after we figure it out
186 * the first time.
187 */
188 class CVmTimeZone *local_zone_;
189
190 /* zone name hash table for zones loaded from the zoneinfo database */
191 class CVmHashTable *db_tab_;
192
193 /* zone name hash table for synthesized zones - GMT+ofs, EST5EDT, etc */
194 class CVmHashTable *synth_tab_;
195
196 /* abbreviation hash table */
197 class CVmHashTable *abbr_tab_;
198
199 /* head of linked list of *loaded* zone objects */
200 class ZoneHashEntry *first_loaded_;
201
202 /* zone index, loaded from the binary file */
203 char *zone_bytes_;
204
205 /* link index, loaded from the binary file */
206 char *link_bytes_;
207
208 /* abbreviation mapping index, loaded from the binary file */
209 char *abbr_bytes_;
210 };
211
212 /* ------------------------------------------------------------------------ */
213 /*
214 * Time type. This describes the settings for a time zone during the
215 * period covered by a transition.
216 */
217 struct vmtz_ttype
218 {
219 vmtz_ttype() { }
220 vmtz_ttype(const struct vmtzquery *query);
221 vmtz_ttype(int32_t gmtofs, int32_t save, const char *fmt)
222 {
223 this->gmtofs = gmtofs;
224 this->save = save;
225 this->fmt = fmt;
226 }
227
228 /*
229 * standard time ofset, in milliseconds - add this to UTC to get local
230 * standard time
231 */
232 int32_t gmtofs;
233
234 /*
235 * daylight time offset, in milliseconds - add to local standard time
236 * to get local wall clock time
237 */
238 int32_t save;
239
240 /* time zone format string abbreviation ("EDT", etc) */
241 const char *fmt;
242 };
243
244 /*
245 * Transition - this records a change in a time zone's rules. Each
246 * transition covers the period from its FROM date (inclusive) to (but not
247 * including) the next transition's FROM date.
248 */
249 struct vmtz_trans
250 {
251 vmtz_trans() { }
252 vmtz_trans(int32_t dayno, int32_t daytime, vmtz_ttype *ttype)
253 {
254 this->dayno = dayno;
255 this->daytime = daytime;
256 this->ttype = ttype;
257 }
258
259 /*
260 * Date/time of the transition, relative to 3/1/0000 UTC; the time is
261 * in milliseconds after midnight. This is starting time for the
262 * transition - it's the moment that our ttype comes into effect.
263 */
264 int32_t dayno;
265 int32_t daytime;
266
267 /* compare my date to the given date/time (time in milliseconds) */
268 int compare_to(int32_t dayno, int32_t daytime, int local) const;
269
270 /* create a List object representing the transition */
271 vm_obj_id_t get_as_list(VMG0_) const;
272
273 /* time zone settings in effect on and after this transition */
274 struct vmtz_ttype *ttype;
275 };
276
277 /*
278 * Time zone daylight savings rule. This describes a switch between
279 * standard time and daylight time. Any given rule fires once a year.
280 * Rules apply to the period after the last enumerated transition in the
281 * time zone; this allows us to project the current rules that apply in the
282 * zone forward in time indefinitely, without having to enumerate all of
283 * the possible transitions (which would be impractical given that our time
284 * type can in principle represent millions of years into the future).
285 */
286 struct vmtz_rule
287 {
288 /* the format abbreviation to use when the rule is in effect */
289 const char *fmt;
290
291 /* the gmt offset for standard time during this rule, in milliseconds */
292 int32_t gmtofs;
293
294 /* daylight savings time offset from standard time, in milliseconds */
295 int32_t save;
296
297 /* the month the rule takes effect (1=January) */
298 char mm;
299
300 /*
301 * When in the month the rule takes effect:
302 *
303 *. 0 -> takes effect on the fixed day of the month <dd>
304 *. 1 -> last <weekday> of the month
305 *. 2 -> first <weekday> on or after <dd>
306 *. 3 -> first <weekday> on or before <dd>
307 */
308 char when;
309
310 /*
311 * The day of the month the rule takes effect (1-366). (This can be
312 * greater than 31, because it's also used to encode POSIX TZ-style
313 * rules with "Julian day of year counting Feb 29". Our calendar
314 * calculator can handle overflows in the day of month, carrying them
315 * automatically into subsequent months, so we can encode the Nth day
316 * of the year as simply January Nth, and we'll get the right result
317 * even if N > 31.
318 */
319 short dd;
320
321 /* the day of the week the rule takes effect (1=Sunday) */
322 char weekday;
323
324 /*
325 * Time zone for the effective time/date: 0 -> local wall clock time
326 * (with the zone settings for the period immediately before the rule
327 * takes effect); 0x40 -> local standard time (for the period
328 * immediately before the rule takes effect), 0x80 -> UTC
329 */
330 uchar at_zone;
331
332 /* time of day the rule takes effect, as milliseconds after midnight */
333 int32_t at;
334
335 /* resolve this rule in the given year */
336 void resolve(int32_t &dayno, int32_t &daytime, int year,
337 const vmtz_rule *prev_rule);
338
339 /* set from an OS DST start/end date descriptor */
340 void set(const os_tzrule_t *desc, const char *abbr,
341 int32_t gmtofs, int32_t save);
342 };
343
344
345 /* ------------------------------------------------------------------------ */
346 /*
347 * Time zone info query structure
348 */
349 struct vmtzquery
350 {
351 /* starting date/time of the transition or rule that applies */
352 int32_t start_dayno;
353 int32_t start_daytime;
354
355 /* GMT offset for standard time, in milliseconds */
356 int32_t gmtofs;
357
358 /* daylight savings offset from standard time, in milliseconds */
359 int32_t save;
360
361 /* zone name abbreviation ("EST", etc) */
362 const char *abbr;
363
364 /* set from a transition item */
365 void set(const vmtz_trans *trans)
366 { set(trans->ttype, trans->dayno, trans->daytime); }
367
368 /* set from a time type */
369 void set(const vmtz_ttype *tty, int32_t dayno, int32_t daytime)
370 {
371 start_dayno = dayno;
372 start_daytime = daytime;
373 gmtofs = tty->gmtofs;
374 save = tty->save;
375 abbr = tty->fmt;
376 }
377
378 /* set from a rule */
379 void set(const vmtz_rule *rule, int32_t dayno, int32_t daytime)
380 {
381 start_dayno = dayno;
382 start_daytime = daytime;
383 gmtofs = rule->gmtofs;
384 save = rule->save;
385 abbr = rule->fmt;
386 }
387 };
388
389 /* ------------------------------------------------------------------------ */
390 /*
391 * Time zone object.
392 */
393 class CVmTimeZone
394 {
395 public:
396 /* create from a timezone descriptor */
397 CVmTimeZone(const os_tzinfo_t *desc);
398
399 /* destruction */
400 ~CVmTimeZone();
401
402 /* load from the zoneinfo database file */
403 static CVmTimeZone *load(VMG_ class ZoneHashEntry *entry);
404
405 /* get the primary name of the zone */
406 const char *get_name(size_t &len) const;
407
408 /* create a List object with this zone's names and aliases */
409 vm_obj_id_t get_name_list(VMG0_) const;
410
411 /* get the single history item that applies to the given date */
412 vm_obj_id_t get_history_item(VMG_ int32_t dayno, int32_t daytime) const;
413
414 /* create a List object populated with the transition list */
415 vm_obj_id_t get_history_list(VMG0_) const;
416
417 /* create a List object populated with the ongoing rules */
418 vm_obj_id_t get_rule_list(VMG0_) const;
419
420 /* country code, if available (ISO3166 two-letter country code) */
421 const char *country() const { return country_; }
422
423 /* coordinates, if available (+DDMM+DDDMM or +DDMMSS+DDDMMSS format) */
424 const char *coords() const { return coords_; }
425
426 /* zone.tab description/comment */
427 const char *desc() const { return desc_; }
428
429 /*
430 * Convert a local wall clock time in this zone to UTC. 'daytime' is
431 * in milliseconds after midnight.
432 */
433 void local_to_utc(int32_t &dayno, int32_t &daytime);
434
435 /*
436 * Convert a UTC time to local time in this zone. Returns the format
437 * abbreviation for the converted time (e.g., PDT for Pacific Daylight
438 * Time). 'daytime' is in milliseconds after midnight. Fills in
439 * 'tzofs' with the time zone's offset from UTC, in milliseconds.
440 */
441 const char *utc_to_local(int32_t &dayno, int32_t &daytime, int32_t &tzofs);
442
443 /*
444 * Query the time zone information at the given time/right now. The
445 * time is in milliseconds after midnight.
446 */
447 void query(vmtzquery *result,
448 int32_t dayno, int32_t daytime, int local) const;
449 void query(vmtzquery *result) const;
450
451 protected:
452 /* create from binary data loaded from the file */
453 CVmTimeZone(class ZoneHashEntryDb *entry, const char *file_data,
454 unsigned int trans_cnt, unsigned int type_cnt,
455 unsigned int rule_cnt, unsigned int abbr_bytes);
456
457 /* create a synthetic zone */
458 CVmTimeZone(class ZoneHashEntrySynth *entry);
459
460 /* initialize from an OS timezone descriptor */
461 void init(const os_tzinfo_t *desc);
462
463 /* my hash table entry */
464 class ZoneHashEntry *hashentry_;
465
466 /* my name; used only if we don't have a hash entry */
467 char *name_;
468
469 /* transition list */
470 int trans_cnt_;
471 vmtz_trans *trans_;
472
473 /* time type list */
474 int ttype_cnt_;
475 vmtz_ttype *ttype_;
476
477 /* rule list */
478 int rule_cnt_;
479 vmtz_rule *rule_;
480
481 /* abbreviations */
482 char *abbr_;
483
484 /* country code */
485 char country_[3];
486
487 /* coordinates */
488 char coords_[16];
489
490 /* comments/description from zone.tab */
491 char *desc_;
492 };
493
494 #endif