| 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.cpp - TADS time zone management |
| 10 | Function |
| 11 | Implements the time zone cache manager and time zone objects. |
| 12 | Notes |
| 13 | None |
| 14 | Modified |
| 15 | 02/05/12 MJRoberts - creation |
| 16 | */ |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <time.h> |
| 20 | #include <ctype.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <math.h> |
| 23 | |
| 24 | #include "t3std.h" |
| 25 | #include "osstzprs.h" |
| 26 | #include "vmtz.h" |
| 27 | #include "vmhash.h" |
| 28 | #include "vmglob.h" |
| 29 | #include "resload.h" |
| 30 | #include "vmhost.h" |
| 31 | #include "vmdate.h" |
| 32 | #include "vmlst.h" |
| 33 | #include "vmstr.h" |
| 34 | #include "vmtzobj.h" |
| 35 | |
| 36 | |
| 37 | /* ------------------------------------------------------------------------ */ |
| 38 | /* |
| 39 | * zone table hash entry |
| 40 | */ |
| 41 | |
| 42 | /* hash table entry types */ |
| 43 | #define ZONE_HASH_DB 1 /* defined in the zoneinfo db file */ |
| 44 | #define ZONE_HASH_LINK 2 /* link (alias) to another zone */ |
| 45 | #define ZONE_HASH_SYNTH 3 /* synthesized from an on os_tzinfo_t */ |
| 46 | |
| 47 | class ZoneHashEntry: public CVmHashEntryCI |
| 48 | { |
| 49 | public: |
| 50 | ZoneHashEntry(const char *str, size_t len, int copy, int typ) |
| 51 | : CVmHashEntryCI(str, len, copy) |
| 52 | { |
| 53 | this->tz = 0; |
| 54 | this->typ = typ; |
| 55 | this->nxt = 0; |
| 56 | this->link_from = 0; |
| 57 | } |
| 58 | |
| 59 | virtual ~ZoneHashEntry() |
| 60 | { |
| 61 | /* if we loaded a timezone object, delete it */ |
| 62 | if (tz != 0) |
| 63 | delete tz; |
| 64 | } |
| 65 | |
| 66 | /* resolve links - by default, we don't have any links */ |
| 67 | virtual ZoneHashEntry *resolve_links() { return this; } |
| 68 | |
| 69 | /* get my country code, if I have one */ |
| 70 | virtual const char *get_country() { return 0; } |
| 71 | |
| 72 | /* match my country code to that of another zone */ |
| 73 | int match_country(const char *country) |
| 74 | { |
| 75 | const char *this_country = get_country(); |
| 76 | return (this_country != 0 && country[0] != '\0' |
| 77 | && memcmp(this_country, country, 2) == 0); |
| 78 | } |
| 79 | |
| 80 | /* entry type (ZONE_HASH_xxx) */ |
| 81 | int typ; |
| 82 | |
| 83 | /* the cached zone object, if loaded */ |
| 84 | CVmTimeZone *tz; |
| 85 | |
| 86 | /* the head of the list of zones that link to us */ |
| 87 | class ZoneHashEntryLink *link_from; |
| 88 | |
| 89 | /* |
| 90 | * In addition to the hash table, we keep a simple linked list of all |
| 91 | * of the loaded zone objects. This lets us do iteraitons over the |
| 92 | * loaded list more easily (and quickly) than using the hash table. |
| 93 | */ |
| 94 | ZoneHashEntry *nxt; |
| 95 | }; |
| 96 | |
| 97 | class ZoneHashEntryDb: public ZoneHashEntry |
| 98 | { |
| 99 | public: |
| 100 | ZoneHashEntryDb(const char *str, size_t len, int copy, |
| 101 | uint32_t seekpos, const char *country) |
| 102 | : ZoneHashEntry(str, len, copy, ZONE_HASH_DB), seekpos(seekpos) |
| 103 | { |
| 104 | memcpy(this->country, country, 2); |
| 105 | } |
| 106 | |
| 107 | /* seek position in the database file */ |
| 108 | uint32_t seekpos; |
| 109 | |
| 110 | /* country code */ |
| 111 | virtual const char *get_country() { return country; } |
| 112 | char country[2]; |
| 113 | }; |
| 114 | |
| 115 | class ZoneHashEntryLink: public ZoneHashEntry |
| 116 | { |
| 117 | public: |
| 118 | ZoneHashEntryLink(const char *str, size_t len, int copy, |
| 119 | ZoneHashEntry *link_to) |
| 120 | : ZoneHashEntry(str, len, copy, ZONE_HASH_LINK) |
| 121 | { |
| 122 | /* add myself to the target's 'from' list */ |
| 123 | this->link_to = link_to->resolve_links(); |
| 124 | link_nxt = link_to->link_from; |
| 125 | this->link_to->link_from = this; |
| 126 | } |
| 127 | |
| 128 | /* resolve links - get the target hash entry for a linked hash entry */ |
| 129 | ZoneHashEntry *resolve_links() { return link_to->resolve_links(); } |
| 130 | |
| 131 | /* get the country from my resolved link */ |
| 132 | const char *get_country() { return resolve_links()->get_country(); } |
| 133 | |
| 134 | /* the zone we link to */ |
| 135 | ZoneHashEntry *link_to; |
| 136 | |
| 137 | /* the next link that links to our link_to */ |
| 138 | ZoneHashEntryLink *link_nxt; |
| 139 | }; |
| 140 | |
| 141 | class ZoneHashEntrySynth: public ZoneHashEntry |
| 142 | { |
| 143 | public: |
| 144 | ZoneHashEntrySynth(const char *str, size_t len, int copy, |
| 145 | const os_tzinfo_t *desc) |
| 146 | : ZoneHashEntry(str, len, copy, ZONE_HASH_SYNTH) |
| 147 | { |
| 148 | /* copy the description */ |
| 149 | memcpy(&synth, desc, sizeof(synth)); |
| 150 | } |
| 151 | |
| 152 | /* synthetic zone description */ |
| 153 | os_tzinfo_t synth; |
| 154 | }; |
| 155 | |
| 156 | |
| 157 | /* ------------------------------------------------------------------------ */ |
| 158 | /* |
| 159 | * zone search spec |
| 160 | */ |
| 161 | struct ZoneSearchSpec |
| 162 | { |
| 163 | ZoneSearchSpec() { abbr[0] = '\0'; gmtofs = INT32MAXVAL; dst = 'S'; } |
| 164 | ZoneSearchSpec(const char *abbr, int32_t gmtofs, char dst) |
| 165 | : gmtofs(gmtofs), dst(dst) |
| 166 | { |
| 167 | set_abbr(abbr); |
| 168 | } |
| 169 | |
| 170 | void set(const char *abbr, int32_t gmtofs, char dst) |
| 171 | { |
| 172 | set_abbr(abbr); |
| 173 | this->gmtofs = gmtofs; |
| 174 | this->dst = dst; |
| 175 | } |
| 176 | void set(const char *abbr, size_t abbrlen, int32_t gmtofs, char dst) |
| 177 | { |
| 178 | set_abbr(abbr, abbrlen); |
| 179 | this->gmtofs = gmtofs; |
| 180 | this->dst = dst; |
| 181 | } |
| 182 | |
| 183 | /* abbreviation string */ |
| 184 | char abbr[16]; |
| 185 | void set_abbr(const char *str, size_t len) |
| 186 | { lib_strcpy(abbr, sizeof(abbr), str, len); } |
| 187 | void set_abbr(const char *str) |
| 188 | { set_abbr(str, str != 0 ? strlen(str) : 0); } |
| 189 | |
| 190 | /* GMT offset in milliseconds */ |
| 191 | int32_t gmtofs; |
| 192 | |
| 193 | /* 'D' for daylight time, 'S' for standard time */ |
| 194 | char dst; |
| 195 | }; |
| 196 | |
| 197 | /* ------------------------------------------------------------------------ */ |
| 198 | /* |
| 199 | * Abbreviation hash entry. This records a mapping from a zone name |
| 200 | * abbreviation (e.g., "EST") to a list of associated zone objects. |
| 201 | */ |
| 202 | |
| 203 | /* individual mapping from an abbreviation to a zone */ |
| 204 | struct AbbrToZone |
| 205 | { |
| 206 | /* the zone we link to */ |
| 207 | ZoneHashEntry *zone; |
| 208 | |
| 209 | /* the GMT offset for this abbreviation in this zone, in milliseconds */ |
| 210 | int32_t gmtofs; |
| 211 | |
| 212 | /* |
| 213 | * 'D' if this is daylight savings time, 'S' for standard time, 'B' if |
| 214 | * it the abbreviation can refer to both types (Australian zones work |
| 215 | * this way: EST is Australian Eastern Standard Time AND Eastern Summer |
| 216 | * Time). When we have a 'B' zone, we'll also have the regular 'D' and |
| 217 | * 'S' entries for it, so that searches for the specific types will |
| 218 | * succeed; the 'B' entry is for get_zone_by_abbr(), to let it know |
| 219 | * that the zone isn't to be treated as a single fixed offset the way, |
| 220 | * say, PDT would be. |
| 221 | */ |
| 222 | char dst; |
| 223 | }; |
| 224 | |
| 225 | /* hash entry */ |
| 226 | class AbbrHashEntry: public CVmHashEntryCI |
| 227 | { |
| 228 | public: |
| 229 | AbbrHashEntry(const char *str, size_t len, int copy, int entry_cnt) |
| 230 | : CVmHashEntryCI(str, len, copy) |
| 231 | { |
| 232 | this->entry_cnt = entry_cnt; |
| 233 | this->entries = new AbbrToZone[entry_cnt]; |
| 234 | } |
| 235 | |
| 236 | ~AbbrHashEntry() |
| 237 | { |
| 238 | delete [] entries; |
| 239 | } |
| 240 | |
| 241 | void set_zone(int i, ZoneHashEntry *zone, int32_t gmtofs, char dst) |
| 242 | { |
| 243 | entries[i].zone = zone; |
| 244 | entries[i].gmtofs = gmtofs; |
| 245 | entries[i].dst = dst; |
| 246 | } |
| 247 | |
| 248 | /* search for a match to a search spec */ |
| 249 | ZoneHashEntry *search(int start_idx, const ZoneSearchSpec *spec) |
| 250 | { |
| 251 | for (int i = start_idx ; i < entry_cnt ; ++i) |
| 252 | { |
| 253 | if (entries[i].gmtofs == spec->gmtofs |
| 254 | && entries[i].dst == spec->dst) |
| 255 | return entries[i].zone; |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* search for a match to a search spec and a zone object */ |
| 262 | ZoneHashEntry *search(int start_idx, const ZoneSearchSpec *spec, |
| 263 | const ZoneHashEntry *zone) |
| 264 | { |
| 265 | for (int i = start_idx ; i < entry_cnt ; ++i) |
| 266 | { |
| 267 | if (entries[i].zone == zone |
| 268 | && entries[i].gmtofs == spec->gmtofs |
| 269 | && entries[i].dst == spec->dst) |
| 270 | return entries[i].zone; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | int entry_cnt; |
| 277 | AbbrToZone *entries; |
| 278 | }; |
| 279 | |
| 280 | |
| 281 | /* ------------------------------------------------------------------------ */ |
| 282 | /* |
| 283 | * Time zone cache manager |
| 284 | */ |
| 285 | |
| 286 | /* |
| 287 | * construction |
| 288 | */ |
| 289 | CVmTimeZoneCache::CVmTimeZoneCache() |
| 290 | { |
| 291 | /* create the zone tables */ |
| 292 | db_tab_ = new CVmHashTable(64, new CVmHashFuncCI(), TRUE); |
| 293 | synth_tab_ = new CVmHashTable(64, new CVmHashFuncCI(), TRUE); |
| 294 | |
| 295 | /* we haven't loaded our zone database yet */ |
| 296 | index_loaded_ = index_loaded_ok_ = FALSE; |
| 297 | zone_bytes_ = 0; |
| 298 | link_bytes_ = 0; |
| 299 | abbr_tab_ = 0; |
| 300 | abbr_bytes_ = 0; |
| 301 | first_loaded_ = 0; |
| 302 | local_zone_ = 0; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * destruction |
| 307 | */ |
| 308 | CVmTimeZoneCache::~CVmTimeZoneCache() |
| 309 | { |
| 310 | /* delete our zone table, zone index, and abbreviation table */ |
| 311 | if (db_tab_ != 0) |
| 312 | delete db_tab_; |
| 313 | if (synth_tab_ != 0) |
| 314 | delete synth_tab_; |
| 315 | if (abbr_tab_ != 0) |
| 316 | delete abbr_tab_; |
| 317 | |
| 318 | /* if we have a local zone object, delete it */ |
| 319 | if (local_zone_ != 0) |
| 320 | delete local_zone_; |
| 321 | |
| 322 | /* if we loaded the file data sections, delete them */ |
| 323 | if (zone_bytes_ != 0) |
| 324 | delete [] zone_bytes_; |
| 325 | if (link_bytes_ != 0) |
| 326 | delete [] link_bytes_; |
| 327 | if (abbr_bytes_ != 0) |
| 328 | delete [] abbr_bytes_; |
| 329 | } |
| 330 | |
| 331 | /* ------------------------------------------------------------------------ */ |
| 332 | /* |
| 333 | * Translate a ZoneHashEntry to a CVmTimeZone object |
| 334 | */ |
| 335 | CVmTimeZone *CVmTimeZoneCache::tz_from_hash(VMG_ ZoneHashEntry *entry) |
| 336 | { |
| 337 | /* if there's no table entry, there's nothing to load */ |
| 338 | if (entry == 0) |
| 339 | return 0; |
| 340 | |
| 341 | /* resolve links */ |
| 342 | entry = entry->resolve_links(); |
| 343 | |
| 344 | /* if we haven't loaded the timezone object yet, do so now */ |
| 345 | if (entry->tz == 0) |
| 346 | { |
| 347 | /* load it */ |
| 348 | entry->tz = CVmTimeZone::load(vmg_ entry); |
| 349 | |
| 350 | /* if successful, add it to the loaded zone list */ |
| 351 | if (entry->tz != 0) |
| 352 | { |
| 353 | entry->nxt = first_loaded_; |
| 354 | first_loaded_ = entry; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /* return the cached object */ |
| 359 | return entry->tz; |
| 360 | } |
| 361 | |
| 362 | /* ------------------------------------------------------------------------ */ |
| 363 | /* |
| 364 | * Parse a zone name in various formats |
| 365 | */ |
| 366 | CVmTimeZone *CVmTimeZoneCache::parse_zone(VMG_ const char *name, size_t len) |
| 367 | { |
| 368 | /* treat a null, empty string, or ":local" as the local zone */ |
| 369 | if (name == 0 || len == 0 |
| 370 | || (len == 6 && memcmp(name, ":local", 6) == 0)) |
| 371 | return get_local_zone(vmg0_); |
| 372 | |
| 373 | /* check for special zones: "Z", "UTC", "GMT" all mean UTC+0 */ |
| 374 | if (lib_stricmp(name, len, "z") == 0 |
| 375 | || lib_stricmp(name, len, "utc") == 0 |
| 376 | || lib_stricmp(name, len, "gmt") == 0) |
| 377 | return get_gmtofs_zone(vmg_ 0); |
| 378 | |
| 379 | /* try +8, -830, +8:30, -8:00:00 */ |
| 380 | CVmTimeZone *tz; |
| 381 | if ((tz = parse_zone_hhmmss(vmg_ "", name, len)) != 0) |
| 382 | return tz; |
| 383 | |
| 384 | /* try UTC+8, UTC-830, UTC+8:30, UTC-8:00:00 */ |
| 385 | if ((tz = parse_zone_hhmmss(vmg_ "utc", name, len)) != 0) |
| 386 | return tz; |
| 387 | |
| 388 | /* try parsing as a POSIX-style EST5EDT string */ |
| 389 | if ((tz = parse_zone_posixTZ(vmg_ name, len)) != 0) |
| 390 | return tz; |
| 391 | |
| 392 | /* it's not one of our special formats; look it up in the database */ |
| 393 | if ((tz = G_tzcache->get_db_zone(vmg_ name, len)) != 0) |
| 394 | return tz; |
| 395 | |
| 396 | /* return failure */ |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Parse a time zone spec in simplified POSIX TZ notation: EST-5, EST-5EDT, |
| 402 | * EST-5EDT-4. |
| 403 | */ |
| 404 | CVmTimeZone *CVmTimeZoneCache::parse_zone_posixTZ( |
| 405 | VMG_ const char *name, size_t len) |
| 406 | { |
| 407 | /* |
| 408 | * Run it through the POSIX TZ parser. Note that the zoneinfo format |
| 409 | * uses negative offsets going west (e.g., EST-5), in contrast to the |
| 410 | * original TZ format. |
| 411 | */ |
| 412 | os_tzinfo_t desc; |
| 413 | if (oss_parse_posix_tz(&desc, name, len, FALSE)) |
| 414 | { |
| 415 | /* got it - check to see if it's already defined */ |
| 416 | ZoneHashEntry *entry = (ZoneHashEntry *)synth_tab_->find(name, len); |
| 417 | if (entry == 0) |
| 418 | { |
| 419 | /* it's not there yet - create a new entry for it */ |
| 420 | entry = new ZoneHashEntrySynth(name, len, TRUE, &desc); |
| 421 | synth_tab_->add(entry); |
| 422 | } |
| 423 | |
| 424 | /* find or create its timezone object */ |
| 425 | return tz_from_hash(vmg_ entry); |
| 426 | } |
| 427 | |
| 428 | /* it's not ours */ |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * get a zone specified in a +hh, +hmm, +hhmm, +hh:mm or +hh:mm:ss format |
| 434 | */ |
| 435 | CVmTimeZone *CVmTimeZoneCache::parse_zone_hhmmss( |
| 436 | VMG_ const char *prefix, const char *name, size_t len) |
| 437 | { |
| 438 | /* check for the prefix - if it doesn't match, return failure */ |
| 439 | size_t plen = strlen(prefix); |
| 440 | if (plen != 0 && (len < plen || memicmp(name, prefix, plen) != 0)) |
| 441 | return 0; |
| 442 | |
| 443 | /* skip the prefix */ |
| 444 | name += plen; |
| 445 | len -= plen; |
| 446 | |
| 447 | /* parse the offset */ |
| 448 | int32_t ofs; |
| 449 | if (!parse_hhmmss(ofs, name, len, TRUE) || len != 0) |
| 450 | return 0; |
| 451 | |
| 452 | /* convert the hh:mm:ss value to seconds and look up the zone */ |
| 453 | return get_gmtofs_zone(vmg_ ofs); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * parse a [+-]h[:mm[:ss]] time/offset value |
| 458 | */ |
| 459 | int CVmTimeZoneCache::parse_hhmmss( |
| 460 | int32_t &ofs, const char *&name, size_t &len, int sign_required) |
| 461 | { |
| 462 | /* check for the sign */ |
| 463 | int s = 1; |
| 464 | if (len > 0 && name[0] == '+') |
| 465 | ++name, --len; |
| 466 | else if (len > 0 && name[0] == '-') |
| 467 | ++name, --len, s = -1; |
| 468 | else if (sign_required) |
| 469 | return FALSE; |
| 470 | |
| 471 | /* parse the hours */ |
| 472 | const char *hhp = name; |
| 473 | int hh = lib_atoi_adv(name, len); |
| 474 | |
| 475 | /* if we had no digits, or more than four in a row, fail */ |
| 476 | size_t hhlen = name - hhp; |
| 477 | if (hhlen < 1 || hhlen > 4) |
| 478 | return FALSE; |
| 479 | |
| 480 | /* if we had three or more consecutive digits, it's hmm or hhmm format */ |
| 481 | int mm = 0, ss = 0; |
| 482 | if (hhlen >= 3) |
| 483 | { |
| 484 | /* the hour is actually hours/minutes - divide out the components */ |
| 485 | mm = hh % 100; |
| 486 | hh /= 100; |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | /* not hmm/hhmm format, so it's hh[:mm[:ss]] - check for minutes */ |
| 491 | if (len >= 3 && *name == ':' && isdigit(name[1]) && isdigit(name[2])) |
| 492 | { |
| 493 | /* parse the minutes */ |
| 494 | ++name, --len; |
| 495 | mm = lib_atoi_adv(name, len); |
| 496 | |
| 497 | /* check for seconds */ |
| 498 | if (len >= 3 && *name == ':' |
| 499 | && isdigit(name[1]) && isdigit(name[2])) |
| 500 | { |
| 501 | ++name, --len; |
| 502 | ss = lib_atoi_adv(name, len); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /* success - compute the offset value in seconds */ |
| 508 | ofs = s*(hh*60*60 + mm*60 + ss); |
| 509 | return TRUE; |
| 510 | } |
| 511 | |
| 512 | /* ------------------------------------------------------------------------ */ |
| 513 | /* |
| 514 | * Look up or load a time zone from the database |
| 515 | */ |
| 516 | CVmTimeZone *CVmTimeZoneCache::get_db_zone(VMG_ const char *name, size_t len) |
| 517 | { |
| 518 | /* if we haven't loaded the zone index data yet, do so now */ |
| 519 | load_db_index(vmg0_); |
| 520 | |
| 521 | /* look up the hash table entry */ |
| 522 | return tz_from_hash(vmg_ (ZoneHashEntry *)db_tab_->find(name, len)); |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Generate an offset string in the minimal format: h, h:mm, h:mm:ss. |
| 527 | */ |
| 528 | #define F_HIDE_ZERO 0x0001 /* suppress a zero offset */ |
| 529 | #define F_PLUS_SIGN 0x0002 /* use a plus sign for positive offsets */ |
| 530 | static void gen_ofs_string(char *buf, size_t buflen, |
| 531 | const char *prefix, int32_t ofs, int flags) |
| 532 | { |
| 533 | /* figure the sign */ |
| 534 | const char *signch = ""; |
| 535 | if (ofs < 0) |
| 536 | ofs = -ofs, signch = "-"; |
| 537 | else if ((flags & F_PLUS_SIGN) != 0) |
| 538 | signch = "+"; |
| 539 | |
| 540 | /* decompose the offset into hours, minutes, and seconds */ |
| 541 | int hh = ofs / (60*60); |
| 542 | int mm = ofs/60 % 60; |
| 543 | int ss = ofs % 60; |
| 544 | |
| 545 | /* |
| 546 | * figure the format: prefix, sign character, then h:mm:ss if we have |
| 547 | * non-zero seconds, h:mm if we have non-zero minutes but no seconds, |
| 548 | * or just the hours if the minutes and seconds are both zero |
| 549 | */ |
| 550 | const char *fmt = (ss != 0 ? "%s%s%d:%02d:%02d" : |
| 551 | mm != 0 ? "%s%s%d:%02d" : |
| 552 | hh != 0 || (flags & F_HIDE_ZERO) == 0 ? "%s%s%d" : |
| 553 | "%s"); |
| 554 | |
| 555 | /* generate the string */ |
| 556 | t3sprintf(buf, buflen, fmt, prefix, signch, hh, mm, ss); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Look up or load a time zone for a GMT offset, in seconds |
| 561 | */ |
| 562 | CVmTimeZone *CVmTimeZoneCache::get_gmtofs_zone(VMG_ int32_t gmtofs_secs) |
| 563 | { |
| 564 | /* build our special name string */ |
| 565 | char name[50]; |
| 566 | gen_ofs_string(name, sizeof(name), "UTC", gmtofs_secs, |
| 567 | F_HIDE_ZERO | F_PLUS_SIGN); |
| 568 | |
| 569 | /* if we already have an entry cached, return it */ |
| 570 | ZoneHashEntry *entry = |
| 571 | (ZoneHashEntry *)synth_tab_->find(name, strlen(name)); |
| 572 | if (entry != 0) |
| 573 | return tz_from_hash(vmg_ entry); |
| 574 | |
| 575 | /* create a new synthetic entry for the zone */ |
| 576 | os_tzinfo_t desc; |
| 577 | memset(&desc, 0, sizeof(desc)); |
| 578 | desc.std_ofs = gmtofs_secs; |
| 579 | lib_strcpy(desc.std_abbr, sizeof(desc.std_abbr), name); |
| 580 | entry = new ZoneHashEntrySynth(name, strlen(name), TRUE, &desc); |
| 581 | |
| 582 | /* add it to the hash table */ |
| 583 | synth_tab_->add(entry); |
| 584 | |
| 585 | /* translate it to a CVmTimeZone object */ |
| 586 | return tz_from_hash(vmg_ entry); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Look up a zone by abbreviation. |
| 591 | * |
| 592 | * Zone abbreviations are inherently ambiguous, and there's no reliable way |
| 593 | * to figure out which zone a user might mean. The ideal solution would be |
| 594 | * for users to stop using zone abbreviations entirely and switch to |
| 595 | * zoneinfo location keys instead. But that's a tough sell, especially |
| 596 | * since most users are blissfully unaware that the problem even exists - |
| 597 | * who in the US knows that "CST" is also the name for time zones in Cuba, |
| 598 | * China, and Australia? It's unreasonable not to expect users to plug in |
| 599 | * abbreviations when entering date strings - especially given that the |
| 600 | * Date parser is otherwise very liberal about accepting free-form, |
| 601 | * colloquial formats. So we feel it's best to give it the college try at |
| 602 | * understanding zone abbreviations, but recognizing that it's ultimately a |
| 603 | * "best effort" sort of task, in that it's not something we can cleanly |
| 604 | * and definitively solve. |
| 605 | * |
| 606 | * We fill in *gmtofs_ms with the GMT offset implied by the zone |
| 607 | * abbreviation. If this is one of the funny Australian zones that uses |
| 608 | * the same abbreviation for both standard and daylight time, we return |
| 609 | * INT32MINVAL in *gmtofs_ms instead, to indicate that the abbreviation |
| 610 | * doesn't specify an offset by itself, but only specifies the zone; the |
| 611 | * offset in this case must be looked up via a date-sensitive query as |
| 612 | * though the zone had been specified by zoneinfo name instead of by |
| 613 | * abbreviation. |
| 614 | */ |
| 615 | CVmTimeZone *CVmTimeZoneCache::get_zone_by_abbr( |
| 616 | VMG_ int32_t *gmtofs_ms, const char *name, size_t len) |
| 617 | { |
| 618 | vmtzquery lresult; |
| 619 | |
| 620 | /* if we haven't loaded the zone index data yet, do so now */ |
| 621 | load_db_index(vmg0_); |
| 622 | |
| 623 | /* make sure we have an abbreviation table */ |
| 624 | if (abbr_tab_ == 0) |
| 625 | return 0; |
| 626 | |
| 627 | /* look up the abbrevation */ |
| 628 | AbbrHashEntry *ah = (AbbrHashEntry *)abbr_tab_->find(name, len); |
| 629 | |
| 630 | /* if there's no table entry, or it's empty, there's nothing to find */ |
| 631 | if (ah == 0 || ah->entry_cnt == 0) |
| 632 | return 0; |
| 633 | |
| 634 | /* get the local zone information */ |
| 635 | CVmTimeZone *ltz = get_local_zone(vmg0_); |
| 636 | const char *country = ltz->country(); |
| 637 | |
| 638 | /* we don't have our selection yet */ |
| 639 | AbbrToZone *atoz = 0, *ep; |
| 640 | |
| 641 | /* |
| 642 | * First, look to see if this is an abbreviation used in our system's |
| 643 | * local time zone. If so use that, since the most likely case is that |
| 644 | * the user is referring to her own local time. |
| 645 | */ |
| 646 | int i; |
| 647 | for (i = 0, ep = ah->entries ; i < ah->entry_cnt ; ++i, ++ep) |
| 648 | { |
| 649 | if (ep->zone->tz == ltz) |
| 650 | { |
| 651 | atoz = ep; |
| 652 | goto done; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * If that didn't work, look for a zone in the same country as the |
| 658 | * local time zone. This seems like the best bet for matching the |
| 659 | * user's expectations. |
| 660 | */ |
| 661 | for (i = 0, ep = ah->entries ; i < ah->entry_cnt ; ++i, ++ep) |
| 662 | { |
| 663 | if (ep->zone->match_country(country)) |
| 664 | { |
| 665 | atoz = ep; |
| 666 | goto done; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * If that failed, look for something within +- 3 hours of the local |
| 672 | * zone. That will at least get something in the same part of the |
| 673 | * world, and probably in the same country or a nearby country. Most |
| 674 | * zones that use the same abbreviation probably aren't right next door |
| 675 | * to each other, either, so this should be pretty selective. (There |
| 676 | * are exceptions. CST in North America is Central Standard Time at |
| 677 | * -6, but it's also Cuba Standard Time at -5. Hopefully most nearby |
| 678 | * zone authorities aren't as mutually antagonistic as the US and Cuba |
| 679 | * and do a better job of coordinating their zone naming.) |
| 680 | */ |
| 681 | ltz->query(&lresult); |
| 682 | for (i = 0, ep = ah->entries ; i < ah->entry_cnt ; ++i, ++ep) |
| 683 | { |
| 684 | if (abs(lresult.gmtofs - ep->gmtofs) < 3*60*60*1000) |
| 685 | { |
| 686 | atoz = ep; |
| 687 | goto done; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * If all else fails, match the first zone in the list. Our zoneinfo |
| 693 | * compiler places a specially hand-chosen primary zone for each |
| 694 | * abbreviation at the head of the abbreviation's zone list. The hand |
| 695 | * picks favor US and European zones, so this isn't the ideal way to |
| 696 | * handle this globally, but in the absence of any other selection |
| 697 | * criteria there's not much else we can do. |
| 698 | */ |
| 699 | atoz = &ah->entries[0]; |
| 700 | |
| 701 | done: |
| 702 | /* |
| 703 | * Pass the abbreviation's offset back to the caller. Most zone |
| 704 | * abbreviations refer to a fixed offset; e.g., PST is always -8, even |
| 705 | * in the summer when the US Pacific Time zone is observing daylight |
| 706 | * time for wall clock time. Writing "PST" explicitly means that we're |
| 707 | * talking about the -8 setting, rather than the current wall clock |
| 708 | * setting. |
| 709 | * |
| 710 | * However, some zones (all in Australia, in the current zoneinfo |
| 711 | * database) use a single abbreviation for both standard and daylight |
| 712 | * time, so these zones *don't* carry the special fixed offset |
| 713 | * information with the abbreviation. For example, Australian EST is |
| 714 | * Eastern Standard Time in the winter and Eastern Summer Time in the |
| 715 | * summer - same abbreviation, two different clock settings. So if we |
| 716 | * determine that we're talking about Australian EST, we need to |
| 717 | * determine the offset from the time of year, as we would for a spec |
| 718 | * like America/Los_Angeles (as opposed to PST). Abbreviations with |
| 719 | * multiple offsets in one zone are marked with 'B' as in the DST |
| 720 | * field. |
| 721 | */ |
| 722 | *gmtofs_ms = (atoz->dst != 'B' ? atoz->gmtofs : INT32MINVAL); |
| 723 | |
| 724 | /* return the timezone object */ |
| 725 | return tz_from_hash(vmg_ atoz->zone); |
| 726 | } |
| 727 | |
| 728 | /* ------------------------------------------------------------------------ */ |
| 729 | /* |
| 730 | * Get the local system time zone. |
| 731 | * |
| 732 | * The local time zone is special, in that it's always represented as a |
| 733 | * distinct CVmTimeZone object (and a distinct entry in our hash table) |
| 734 | * from the object for the zone it represents. This allows callers to |
| 735 | * distinguish a date that's implicitly in the local zone from one that's |
| 736 | * explicitly in the zone that happens to be the local zone. For example, |
| 737 | * in the CVmObjDate package, a date specified as "1 pm" is implicitly in |
| 738 | * the local time zone, whereas a date specified as "1 pm PST" is |
| 739 | * explicitly in Pacific Standard Time. If the local zone happens to be |
| 740 | * PST, both dates will be in PST now; but if the objects are saved and |
| 741 | * later restored on another machine that's in New York, the first date ("1 |
| 742 | * pm") will be rendered in EST, whereas the second ("1 pm PST") will |
| 743 | * continue to be rendered in PST. To make this possible, we have to know |
| 744 | * that the first date's CVmTimeZone object is the special "local" object, |
| 745 | * separate from the second date's CVmTimeZone object - even though both |
| 746 | * represent the same underlying database entry. |
| 747 | * |
| 748 | * To implement this, if we resolve the local zone to a database entry, we |
| 749 | * load a separate copy of the database entry so that the local zone is a |
| 750 | * distinct object. |
| 751 | */ |
| 752 | CVmTimeZone *CVmTimeZoneCache::get_local_zone(VMG0_) |
| 753 | { |
| 754 | /* if we have a cached local zone, return it */ |
| 755 | if (local_zone_ != 0) |
| 756 | return local_zone_; |
| 757 | |
| 758 | /* we haven't found our zone entry yet */ |
| 759 | ZoneHashEntry *entry = 0; |
| 760 | |
| 761 | /* see if we can get a zoneinfo database key from the operating system */ |
| 762 | char name[128]; |
| 763 | if (os_get_zoneinfo_key(name, sizeof(name)) |
| 764 | && load_db_index(vmg0_) |
| 765 | && (entry = (ZoneHashEntry *)db_tab_->find(name, strlen(name))) != 0) |
| 766 | { |
| 767 | /* we found a database entry - load a separate copy */ |
| 768 | local_zone_ = CVmTimeZone::load(vmg_ entry); |
| 769 | } |
| 770 | |
| 771 | /* if we didn't find it, try the lower-level timezone description */ |
| 772 | os_tzinfo_t info; |
| 773 | if (local_zone_ == 0 && os_get_timezone_info(&info)) |
| 774 | { |
| 775 | /* |
| 776 | * If they were able to give us separate standard and daylight |
| 777 | * offsets and abbreviations, search for the pair. Otherwise |
| 778 | * search for the current one. |
| 779 | * |
| 780 | * Note that the os_tzinfo_t specifies offsets in seconds, whereas |
| 781 | * the ZoneSearchSpec uses milliseconds, so multiply accordingly. |
| 782 | */ |
| 783 | if (info.std_ofs != info.dst_ofs |
| 784 | && info.std_abbr[0] != '\0' && info.dst_abbr[0] != '\0') |
| 785 | { |
| 786 | ZoneSearchSpec spec[2]; |
| 787 | spec[0].set(info.std_abbr, info.std_ofs*1000, 'S'); |
| 788 | spec[1].set(info.dst_abbr, info.dst_ofs*1000, 'D'); |
| 789 | entry = search(vmg_ spec, 2); |
| 790 | } |
| 791 | else |
| 792 | { |
| 793 | /* |
| 794 | * they couldn't distinguish standard and daylight, so just use |
| 795 | * the one that's currently in effect |
| 796 | */ |
| 797 | ZoneSearchSpec spec; |
| 798 | if (info.is_dst) |
| 799 | spec.set(info.dst_abbr, info.dst_ofs*1000, 'D'); |
| 800 | else |
| 801 | spec.set(info.std_abbr, info.std_ofs*1000, 'S'); |
| 802 | entry = search(vmg_ &spec, 1); |
| 803 | } |
| 804 | |
| 805 | /* check to see if we found a matching entry in the database */ |
| 806 | if (entry != 0) |
| 807 | { |
| 808 | /* we found an entry - load a separate copy */ |
| 809 | local_zone_ = CVmTimeZone::load(vmg_ entry); |
| 810 | } |
| 811 | else |
| 812 | { |
| 813 | /* |
| 814 | * we didn't find an entry, so synthesize a new one based on |
| 815 | * the zone description we got from the OS |
| 816 | */ |
| 817 | local_zone_ = new CVmTimeZone(&info); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * If we still don't have anything, try the the standard C++ library's |
| 823 | * time functions. These won't give us as much information as we were |
| 824 | * hoping to get from the OS, and probably won't work at all given that |
| 825 | * the OS routines probably went to the same primary sources that the |
| 826 | * CRTL will look at, but it's worth trying as a last resort. |
| 827 | */ |
| 828 | if (local_zone_ == 0) |
| 829 | { |
| 830 | /* strftime(%Z) format gives us the local time zone */ |
| 831 | os_time_t t = os_time(0); |
| 832 | struct tm *tm = os_localtime(&t); |
| 833 | strftime(name, sizeof(name), "%Z", tm); |
| 834 | |
| 835 | /* if we got a zone, look it up */ |
| 836 | if (name[0] != '\0') |
| 837 | { |
| 838 | /* |
| 839 | * Get the current minutes in the day in local time, and the |
| 840 | * local time day number. For the day number, use the year |
| 841 | * times 366 plus the day of the year; this doesn't correspond |
| 842 | * to a serial day number in our caldate_t system or really any |
| 843 | * other system, but it does produce monotonic values, which is |
| 844 | * all we need: our only use for this day number if to compare |
| 845 | * the UTC and local day numbers to see if the respective |
| 846 | * clocks are on different days, and if so in which direction. |
| 847 | */ |
| 848 | int ss_local = tm->tm_hour*60*60 + tm->tm_min*60 + tm->tm_sec; |
| 849 | long dayno_local = tm->tm_year*366L + tm->tm_yday; |
| 850 | char dst = tm->tm_isdst ? 'D' : 'S'; |
| 851 | |
| 852 | /* repeat the exercise in local time */ |
| 853 | tm = os_gmtime(&t); |
| 854 | int ss_utc = tm->tm_hour*60*60 + tm->tm_min*60 + tm->tm_sec; |
| 855 | long dayno_utc = tm->tm_year*366L + tm->tm_yday; |
| 856 | |
| 857 | /* if the clocks are on different days, adjust accordingly */ |
| 858 | if (dayno_utc > dayno_local) |
| 859 | ss_utc += 24*60*60; |
| 860 | else if (dayno_utc < dayno_local) |
| 861 | ss_utc -= 24*60*60; |
| 862 | |
| 863 | /* |
| 864 | * Search for the zone. For the GMT offset, figure the |
| 865 | * difference in milliseconds between local and UTC. |
| 866 | */ |
| 867 | ZoneSearchSpec spec(name, (ss_local - ss_utc)*1000, dst); |
| 868 | if ((entry = search(vmg_ &spec, 1)) != 0) |
| 869 | { |
| 870 | /* we found a database entry - load a private copy */ |
| 871 | local_zone_ = CVmTimeZone::load(vmg_ entry); |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | /* |
| 876 | * no database match; synthesize an entry based on the time |
| 877 | * zone info we got from the C time functions |
| 878 | */ |
| 879 | os_tzinfo_t desc; |
| 880 | memset(&desc, 0, sizeof(desc)); |
| 881 | desc.std_ofs = desc.dst_ofs = ss_local - ss_utc; |
| 882 | lib_strcpy(desc.std_abbr, sizeof(desc.std_abbr), name); |
| 883 | lib_strcpy(desc.dst_abbr, sizeof(desc.dst_abbr), name); |
| 884 | desc.is_dst = tm->tm_isdst; |
| 885 | local_zone_ = new CVmTimeZone(&desc); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /* if after all that we still have nothing, use UTC */ |
| 891 | if (local_zone_ == 0) |
| 892 | { |
| 893 | /* try looking up the basic UTC zone */ |
| 894 | os_tzinfo_t desc; |
| 895 | memset(&desc, 0, sizeof(desc)); |
| 896 | strcpy(desc.std_abbr, "UTC"); |
| 897 | local_zone_ = new CVmTimeZone(&desc); |
| 898 | } |
| 899 | |
| 900 | /* return the local zone we came up with */ |
| 901 | return local_zone_; |
| 902 | } |
| 903 | |
| 904 | /* ------------------------------------------------------------------------ */ |
| 905 | /* |
| 906 | * Create a missing database entry. This represents a database zone that's |
| 907 | * not defined in the version of the database we're using, but was imported |
| 908 | * from a saved game created with another database version. |
| 909 | */ |
| 910 | CVmTimeZone *CVmTimeZoneCache::create_missing_zone( |
| 911 | VMG_ const char *name, size_t namelen, const os_tzinfo_t *desc) |
| 912 | { |
| 913 | /* create the entry */ |
| 914 | ZoneHashEntry *e = new ZoneHashEntrySynth(name, namelen, TRUE, desc); |
| 915 | |
| 916 | /* add it to our database table */ |
| 917 | db_tab_->add(e); |
| 918 | |
| 919 | /* return the new entry */ |
| 920 | return tz_from_hash(vmg_ e); |
| 921 | } |
| 922 | |
| 923 | /* ------------------------------------------------------------------------ */ |
| 924 | /* |
| 925 | * search for a zone that matches all of the given search specs |
| 926 | */ |
| 927 | ZoneHashEntry *CVmTimeZoneCache::search( |
| 928 | VMG_ const ZoneSearchSpec *specs, int cnt) |
| 929 | { |
| 930 | /* if there are no abbreviations, fail */ |
| 931 | if (!load_db_index(vmg0_) || abbr_tab_ == 0) |
| 932 | return 0; |
| 933 | |
| 934 | /* if we're searching just one list, this is easy... */ |
| 935 | if (cnt == 1) |
| 936 | { |
| 937 | /* look up this abbreviation */ |
| 938 | AbbrHashEntry *e = (AbbrHashEntry *)abbr_tab_->find( |
| 939 | specs[0].abbr, strlen(specs[0].abbr)); |
| 940 | if (e == 0) |
| 941 | return 0; |
| 942 | |
| 943 | /* return the first entry that matches the spec */ |
| 944 | return e->search(0, specs); |
| 945 | } |
| 946 | else if (cnt == 2) |
| 947 | { |
| 948 | /* look up the two abbreviations */ |
| 949 | AbbrHashEntry *e0 = (AbbrHashEntry *)abbr_tab_->find( |
| 950 | specs[0].abbr, strlen(specs[0].abbr)); |
| 951 | AbbrHashEntry *e1 = (AbbrHashEntry *)abbr_tab_->find( |
| 952 | specs[1].abbr, strlen(specs[1].abbr)); |
| 953 | |
| 954 | /* if either abbreviation is missing, fail */ |
| 955 | if (e0 == 0 || e0->entry_cnt == 0 || e1 == 0 || e1->entry_cnt == 0) |
| 956 | return 0; |
| 957 | |
| 958 | /* find the earliest intersection */ |
| 959 | for (int i = 0, j = 0 ; i < e0->entry_cnt && j < e1->entry_cnt ; |
| 960 | ++i, ++j) |
| 961 | { |
| 962 | /* find the next first-list element matching the first spec */ |
| 963 | ZoneHashEntry *z0 = e0->search(i, &specs[0]); |
| 964 | if (z0 == 0) |
| 965 | return 0; |
| 966 | |
| 967 | /* |
| 968 | * look for a second-list element that matches the second spec |
| 969 | * AND the first-list zone we just matched |
| 970 | */ |
| 971 | ZoneHashEntry *z1 = e1->search(j, &specs[1], z0); |
| 972 | if (z1 != 0) |
| 973 | return z1; |
| 974 | |
| 975 | /* |
| 976 | * no luck; find the next second-list element matching just the |
| 977 | * second spec, then find a first-list item matching z1 and the |
| 978 | * first spec |
| 979 | */ |
| 980 | z1 = e1->search(j, &specs[1]); |
| 981 | if ((z0 = e0->search(i+1, &specs[0], z1)) != 0) |
| 982 | return z0; |
| 983 | } |
| 984 | |
| 985 | /* didn't find it */ |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * we're really only used for STD+DST searches; if we're called upon |
| 991 | * for anything else, it's unexpected |
| 992 | */ |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | |
| 997 | |
| 998 | /* ------------------------------------------------------------------------ */ |
| 999 | /* |
| 1000 | * Load the zoneinfo database index. Returns true on success, false on |
| 1001 | * failure. |
| 1002 | */ |
| 1003 | int CVmTimeZoneCache::load_db_index(VMG0_) |
| 1004 | { |
| 1005 | /* if we've already loaded the index (or tried to), we're done */ |
| 1006 | if (index_loaded_) |
| 1007 | return index_loaded_ok_; |
| 1008 | |
| 1009 | /* we've now tried loading the index */ |
| 1010 | index_loaded_ = TRUE; |
| 1011 | |
| 1012 | /* open the zone file */ |
| 1013 | osfildef *fp = open_zoneinfo_file(vmg0_); |
| 1014 | if (fp == 0) |
| 1015 | return FALSE; |
| 1016 | |
| 1017 | /* read the signature */ |
| 1018 | char buf[24]; |
| 1019 | int ok = !osfrb(fp, buf, 24) && memcmp(buf, "T3TZ", 4) == 0; |
| 1020 | |
| 1021 | /* if the signature checked out, read the zone table */ |
| 1022 | uint32_t zone_table_len = 0, zone_cnt = 0; |
| 1023 | if (ok) |
| 1024 | { |
| 1025 | /* decode the zone name table length */ |
| 1026 | zone_table_len = osrp4(buf + 16) - 4; |
| 1027 | zone_cnt = osrp4(buf + 20); |
| 1028 | |
| 1029 | /* allocate space to load the table */ |
| 1030 | zone_bytes_ = new char[zone_table_len]; |
| 1031 | |
| 1032 | /* read the table */ |
| 1033 | ok = zone_bytes_ != 0 && !osfrb(fp, zone_bytes_, zone_table_len); |
| 1034 | } |
| 1035 | |
| 1036 | /* if everything is working so far, read the link table */ |
| 1037 | uint32_t link_table_len = 0; |
| 1038 | if (ok && !osfrb(fp, buf, 8)) |
| 1039 | { |
| 1040 | /* load the link table */ |
| 1041 | link_table_len = osrp4(buf) - 4; |
| 1042 | /* uint32_t link_cnt = osrp4(buf+4) - not currently needed */ |
| 1043 | link_bytes_ = new char[link_table_len]; |
| 1044 | ok = link_bytes_ != 0 && !osfrb(fp, link_bytes_, link_table_len); |
| 1045 | } |
| 1046 | |
| 1047 | /* if we're still good, read the abbreviation mappings */ |
| 1048 | uint32_t abbr_table_len = 0; |
| 1049 | if (ok && !osfrb(fp, buf, 8)) |
| 1050 | { |
| 1051 | /* load the abbreviation table */ |
| 1052 | abbr_table_len = osrp4(buf) - 4; |
| 1053 | /* uint32_t abbr_cnt = osrp4(buf + 4) - not currently needed */ |
| 1054 | abbr_bytes_ = new char[abbr_table_len]; |
| 1055 | ok = abbr_bytes_ != 0 && !osfrb(fp, abbr_bytes_, abbr_table_len); |
| 1056 | } |
| 1057 | |
| 1058 | /* we're done with the file */ |
| 1059 | osfcls(fp); |
| 1060 | |
| 1061 | /* if we read the zone table successfully, create a hash table from it */ |
| 1062 | ZoneHashEntry **zone_index = 0; |
| 1063 | if (ok) |
| 1064 | { |
| 1065 | /* create the numerical index */ |
| 1066 | zone_index = new ZoneHashEntry *[zone_cnt]; |
| 1067 | |
| 1068 | /* populate it */ |
| 1069 | const char *zone_end = zone_bytes_ + zone_table_len; |
| 1070 | int zi = 0; |
| 1071 | for (const char *p = zone_bytes_ ; p < zone_end ; ) |
| 1072 | { |
| 1073 | /* read and skip the name length prefix */ |
| 1074 | size_t len = osrp1(p); |
| 1075 | ++p; |
| 1076 | |
| 1077 | /* create this entry */ |
| 1078 | ZoneHashEntry *e = new ZoneHashEntryDb( |
| 1079 | p, len, FALSE, osrp4(p+len+1), p+len+1+4); |
| 1080 | |
| 1081 | /* add it to the hash table and zone index */ |
| 1082 | db_tab_->add(e); |
| 1083 | zone_index[zi++] = e; |
| 1084 | |
| 1085 | /* skip the name, seek pointer, and country code */ |
| 1086 | p += len + 1 + 4 + 2; |
| 1087 | } |
| 1088 | |
| 1089 | /* read the links */ |
| 1090 | const char *link_end = link_bytes_ + link_table_len; |
| 1091 | for (const char *p = link_bytes_ ; p < link_end ; ) |
| 1092 | { |
| 1093 | /* read and skip the name length */ |
| 1094 | size_t len = osrp1(p); |
| 1095 | ++p; |
| 1096 | |
| 1097 | /* read the TO index */ |
| 1098 | unsigned int to = osrp4(p + len + 1); |
| 1099 | |
| 1100 | /* look up the TO field */ |
| 1101 | ZoneHashEntry *e_to = (to < zone_cnt ? zone_index[to] : 0); |
| 1102 | |
| 1103 | /* if we found it, create the new entry for the FROM */ |
| 1104 | if (e_to != 0) |
| 1105 | db_tab_->add(new ZoneHashEntryLink(p, len, FALSE, e_to)); |
| 1106 | |
| 1107 | /* skip the entry */ |
| 1108 | p += len + 1 + 4; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /* if there are no errors so far, read the abbreviation table */ |
| 1113 | if (ok) |
| 1114 | { |
| 1115 | /* create the abbreviation hash table */ |
| 1116 | abbr_tab_ = new CVmHashTable(64, new CVmHashFuncCI(), TRUE); |
| 1117 | |
| 1118 | /* populate it */ |
| 1119 | const char *abbr_end = abbr_bytes_ + abbr_table_len; |
| 1120 | for (const char *p = abbr_bytes_ ; p < abbr_end ; ) |
| 1121 | { |
| 1122 | /* read and skip the abbreviation length prefix */ |
| 1123 | size_t len = osrp1(p); |
| 1124 | ++p; |
| 1125 | |
| 1126 | /* read the number of zone entries */ |
| 1127 | int entry_cnt = osrp1(p + len + 1); |
| 1128 | |
| 1129 | /* create and add the hash table entry */ |
| 1130 | AbbrHashEntry *e = new AbbrHashEntry(p, len, FALSE, entry_cnt); |
| 1131 | abbr_tab_->add(e); |
| 1132 | |
| 1133 | /* populate the entry */ |
| 1134 | int ei; |
| 1135 | for (p += len + 1 + 1, ei = 0 ; ei < entry_cnt ; p += 9, ++ei) |
| 1136 | { |
| 1137 | /* look up the zone */ |
| 1138 | uint32_t zi = osrp4(p); |
| 1139 | ZoneHashEntry *zone = (zi < zone_cnt ? zone_index[zi] : 0); |
| 1140 | |
| 1141 | /* add the entry */ |
| 1142 | e->set_zone(ei, zone, osrp4s(p+4), p[8]); |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | /* we're done with the numerical zone index */ |
| 1148 | if (zone_index != 0) |
| 1149 | delete [] zone_index; |
| 1150 | |
| 1151 | /* return the status */ |
| 1152 | index_loaded_ok_ = ok; |
| 1153 | return ok; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * Open the zoneinfo file |
| 1158 | */ |
| 1159 | osfildef *CVmTimeZoneCache::open_zoneinfo_file(VMG0_) |
| 1160 | { |
| 1161 | /* open the file through the system resource loader */ |
| 1162 | return G_host_ifc->get_sys_res_loader()->open_res_file( |
| 1163 | "timezones.t3tz", 0, "TMZN"); |
| 1164 | } |
| 1165 | |
| 1166 | /* ------------------------------------------------------------------------ */ |
| 1167 | /* |
| 1168 | * Time zone object |
| 1169 | */ |
| 1170 | |
| 1171 | /* file data block sizes */ |
| 1172 | const int TRANS_SIZE = 9; |
| 1173 | const int TYPE_SIZE = 9; |
| 1174 | const int RULE_SIZE = 17; |
| 1175 | |
| 1176 | /* |
| 1177 | * construction |
| 1178 | */ |
| 1179 | CVmTimeZone::CVmTimeZone(ZoneHashEntryDb *entry, const char *file_data, |
| 1180 | unsigned int trans_cnt, unsigned int type_cnt, |
| 1181 | unsigned int rule_cnt, unsigned int abbr_bytes) |
| 1182 | { |
| 1183 | /* keep a pointer to our hash table entry */ |
| 1184 | hashentry_ = entry; |
| 1185 | |
| 1186 | /* since we have a hash entry, we don't need a separate name */ |
| 1187 | name_ = 0; |
| 1188 | |
| 1189 | /* allocate our arrays (do this first, as they have cross references) */ |
| 1190 | trans_ = (trans_cnt_ = trans_cnt) != 0 ? new vmtz_trans[trans_cnt] : 0; |
| 1191 | rule_ = (rule_cnt_ = rule_cnt) != 0 ? new vmtz_rule[rule_cnt] : 0; |
| 1192 | ttype_ = (ttype_cnt_ = type_cnt) != 0 ? new vmtz_ttype[type_cnt] : 0; |
| 1193 | abbr_ = (abbr_bytes != 0 ? new char[abbr_bytes] : 0); |
| 1194 | |
| 1195 | /* assume we have no descriptive data */ |
| 1196 | country_[0] = '\0'; |
| 1197 | coords_[0] = '\0'; |
| 1198 | desc_ = 0; |
| 1199 | |
| 1200 | /* set up to read from the file data block */ |
| 1201 | const char *p = file_data; |
| 1202 | unsigned int i; |
| 1203 | |
| 1204 | /* decode the transition list */ |
| 1205 | vmtz_trans *t; |
| 1206 | for (t = trans_, i = 0 ; i < trans_cnt ; ++i, ++t, p += TRANS_SIZE) |
| 1207 | { |
| 1208 | t->dayno = osrp4s(p); |
| 1209 | t->daytime = osrp4s(p+4); |
| 1210 | unsigned int idx = osrp1(p+8); |
| 1211 | t->ttype = idx < type_cnt ? &ttype_[idx] : 0; |
| 1212 | } |
| 1213 | |
| 1214 | /* decode the rule list */ |
| 1215 | vmtz_rule *r; |
| 1216 | for (r = rule_, i = 0 ; i < rule_cnt ; ++i, ++r, p += RULE_SIZE) |
| 1217 | { |
| 1218 | unsigned int idx = osrp1(p); |
| 1219 | r->fmt = idx < abbr_bytes ? abbr_ + idx : 0; |
| 1220 | r->mm = osrp1(p+1); |
| 1221 | r->when = osrp1(p+2); |
| 1222 | r->dd = osrp1(p+3); |
| 1223 | r->weekday = osrp1(p+4); |
| 1224 | r->at = osrp4s(p+5); |
| 1225 | r->gmtofs = osrp4s(p+9); |
| 1226 | r->save = osrp4s(p+13); |
| 1227 | |
| 1228 | /* decode the time zone from the AT data */ |
| 1229 | r->at_zone = (r->at >> 24) & 0xff; |
| 1230 | r->at &= 0x00ffffff; |
| 1231 | } |
| 1232 | |
| 1233 | /* decode the type list */ |
| 1234 | vmtz_ttype *tt; |
| 1235 | for (tt = ttype_, i = 0 ; i < type_cnt ; ++i, ++tt, p += TYPE_SIZE) |
| 1236 | { |
| 1237 | tt->gmtofs = osrp4s(p); |
| 1238 | tt->save = osrp4s(p+4); |
| 1239 | unsigned int idx = osrp1(p+8); |
| 1240 | tt->fmt = idx < abbr_bytes ? abbr_ + idx : 0; |
| 1241 | } |
| 1242 | |
| 1243 | /* copy the format abbreviation strings */ |
| 1244 | if (abbr_bytes != 0) |
| 1245 | { |
| 1246 | memcpy(abbr_, p, abbr_bytes); |
| 1247 | p += abbr_bytes; |
| 1248 | } |
| 1249 | |
| 1250 | /* save the descriptive data, if available */ |
| 1251 | if (*p != '\0') |
| 1252 | { |
| 1253 | /* save the country code */ |
| 1254 | lib_strcpy(country_, sizeof(country_), p, 2); |
| 1255 | p += 2; |
| 1256 | |
| 1257 | /* save the coordinate string */ |
| 1258 | lib_strcpy(coords_, sizeof(coords_), p, 15); |
| 1259 | p += 16; |
| 1260 | |
| 1261 | /* save the description */ |
| 1262 | size_t dlen = osrp1(p); |
| 1263 | desc_ = new char[dlen + 1]; |
| 1264 | lib_strcpy(desc_, dlen + 1, p+1, dlen); |
| 1265 | p += dlen + 2; |
| 1266 | } |
| 1267 | else |
| 1268 | { |
| 1269 | /* null byte for country - no descriptive information */ |
| 1270 | } |
| 1271 | |
| 1272 | #if 0// DEBUG $$$ |
| 1273 | for (t = trans_, i = 0 ; i < trans_cnt ; ++i, ++t) |
| 1274 | { |
| 1275 | caldate_t cd(t->dayno); |
| 1276 | printf("%04d-%02d-%02d %02d:%02d -> GMT%+d (%+d) %s\n", |
| 1277 | cd.y, cd.m, cd.d, |
| 1278 | (int)t->daytime/(60*60), (int)->daytime/60 % 60, |
| 1279 | t->ttype->gmtofs, t->ttype->save, t->ttype->fmt); |
| 1280 | } |
| 1281 | #endif |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Create a synthesized zone from an os descriptor |
| 1286 | */ |
| 1287 | CVmTimeZone::CVmTimeZone(ZoneHashEntrySynth *entry) |
| 1288 | { |
| 1289 | /* keep a pointer to our zone entry */ |
| 1290 | hashentry_ = entry; |
| 1291 | |
| 1292 | /* since we have a hash entry, we don't need a separate name */ |
| 1293 | name_ = 0; |
| 1294 | |
| 1295 | /* initialize from the descriptor */ |
| 1296 | init(&entry->synth); |
| 1297 | } |
| 1298 | |
| 1299 | /* |
| 1300 | * Create a synthesized zone from an os descriptor |
| 1301 | */ |
| 1302 | CVmTimeZone::CVmTimeZone(const os_tzinfo_t *desc) |
| 1303 | { |
| 1304 | /* no hash entry */ |
| 1305 | hashentry_ = 0; |
| 1306 | |
| 1307 | /* |
| 1308 | * Synthesize a POSIX-style name from the abbreviation and offset. If |
| 1309 | * we have both standard and daylight abbreviations and distinct |
| 1310 | * offsets, generate a POSIX-style "EST5DST" string. Otherwise just |
| 1311 | * use the active abbreviation and offset. |
| 1312 | * |
| 1313 | * Note that the offset in POSIX strings uses the opposite sign from |
| 1314 | * what we use - EST is +5 in POSIX strings vs -5 in the zoneinfo |
| 1315 | * scheme (which is what we use). |
| 1316 | */ |
| 1317 | char buf[128]; |
| 1318 | if (desc->std_abbr[0] != 0 && desc->dst_abbr[0] != 0 |
| 1319 | && desc->std_ofs != desc->dst_ofs) |
| 1320 | { |
| 1321 | /* |
| 1322 | * Use both abbreviations, as in EST5DST. If the daylight offset |
| 1323 | * is one hour higher than the standard offset, omit the offset |
| 1324 | * from the daylight part, since that's the default; otherwise |
| 1325 | * include it. |
| 1326 | */ |
| 1327 | char buf1[128], buf2[128]; |
| 1328 | gen_ofs_string(buf1, sizeof(buf1), desc->std_abbr, -desc->std_ofs, 0); |
| 1329 | if (desc->dst_ofs == desc->std_ofs + 3600) |
| 1330 | lib_strcpy(buf2, sizeof(buf2), desc->dst_abbr); |
| 1331 | else |
| 1332 | gen_ofs_string(buf2, sizeof(buf2), |
| 1333 | desc->dst_abbr, -desc->dst_ofs, 0); |
| 1334 | |
| 1335 | /* build the combined string */ |
| 1336 | t3sprintf(buf, sizeof(buf), "%s%s", buf1, buf2); |
| 1337 | } |
| 1338 | else if (desc->is_dst) |
| 1339 | { |
| 1340 | /* use the daylight abbreviation only */ |
| 1341 | gen_ofs_string(buf, sizeof(buf), desc->dst_abbr, -desc->dst_ofs, 0); |
| 1342 | } |
| 1343 | else |
| 1344 | { |
| 1345 | /* use the standard time abbreviation only */ |
| 1346 | gen_ofs_string(buf, sizeof(buf), desc->std_abbr, -desc->std_ofs, 0); |
| 1347 | } |
| 1348 | |
| 1349 | /* save the name */ |
| 1350 | name_ = lib_copy_str(buf); |
| 1351 | |
| 1352 | /* initialize from the descriptor */ |
| 1353 | init(desc); |
| 1354 | } |
| 1355 | |
| 1356 | /* |
| 1357 | * Initialize from an OS timezone descriptor |
| 1358 | */ |
| 1359 | void CVmTimeZone::init(const os_tzinfo_t *desc) |
| 1360 | { |
| 1361 | /* we don't have any transitions or rules */ |
| 1362 | trans_ = 0; |
| 1363 | trans_cnt_ = 0; |
| 1364 | rule_ = 0; |
| 1365 | rule_cnt_ = 0; |
| 1366 | |
| 1367 | /* make a copy of our one abbreviation(s) */ |
| 1368 | size_t std_abbr_len = strlen(desc->std_abbr); |
| 1369 | size_t dst_abbr_len = strlen(desc->dst_abbr); |
| 1370 | abbr_ = new char[std_abbr_len + dst_abbr_len + 2]; |
| 1371 | |
| 1372 | char *std_abbr = abbr_; |
| 1373 | strcpy(std_abbr, desc->std_abbr); |
| 1374 | |
| 1375 | char *dst_abbr = abbr_ + std_abbr_len + 1; |
| 1376 | strcpy(dst_abbr, desc->dst_abbr); |
| 1377 | |
| 1378 | /* if rules for the start/end dates are specified, use them */ |
| 1379 | if ((desc->dst_start.jday != 0 |
| 1380 | || desc->dst_start.yday != 0 |
| 1381 | || desc->dst_start.month != 0) |
| 1382 | && (desc->dst_end.jday != 0 |
| 1383 | || desc->dst_end.yday != 0 |
| 1384 | || desc->dst_end.month != 0)) |
| 1385 | { |
| 1386 | /* we have start/end rules - create our copies */ |
| 1387 | rule_cnt_ = 2; |
| 1388 | rule_ = new vmtz_rule[2]; |
| 1389 | |
| 1390 | /* |
| 1391 | * Set the rules from the OS description. Note that the OS |
| 1392 | * description gives offsets in seconds, whereas we store |
| 1393 | * everything in milliseconds. |
| 1394 | */ |
| 1395 | rule_[0].set(&desc->dst_start, dst_abbr, desc->std_ofs*1000, |
| 1396 | (desc->dst_ofs - desc->std_ofs)*1000); |
| 1397 | rule_[1].set(&desc->dst_end, std_abbr, desc->std_ofs*1000, 0); |
| 1398 | |
| 1399 | /* |
| 1400 | * allocate space for two types - one for standard time, one for |
| 1401 | * daylight time |
| 1402 | */ |
| 1403 | ttype_ = new vmtz_ttype[2]; |
| 1404 | ttype_cnt_ = 2; |
| 1405 | |
| 1406 | /* set up the standard time information */ |
| 1407 | ttype_[0].gmtofs = desc->std_ofs * 1000; |
| 1408 | ttype_[0].save = 0; |
| 1409 | ttype_[0].fmt = std_abbr; |
| 1410 | |
| 1411 | /* set up the daylight time information */ |
| 1412 | ttype_[1].gmtofs = desc->dst_ofs * 1000; |
| 1413 | ttype_[1].save = (desc->dst_ofs - desc->std_ofs) * 1000; |
| 1414 | ttype_[1].fmt = dst_abbr; |
| 1415 | } |
| 1416 | else |
| 1417 | { |
| 1418 | /* |
| 1419 | * There are no transition rules, so we only have use for whichever |
| 1420 | * setting is currently in effect. |
| 1421 | */ |
| 1422 | ttype_ = new vmtz_ttype[1]; |
| 1423 | ttype_cnt_ = 1; |
| 1424 | |
| 1425 | /* use the current setting only */ |
| 1426 | ttype_[0].save = 0; |
| 1427 | if (desc->is_dst) |
| 1428 | { |
| 1429 | ttype_[0].gmtofs = desc->dst_ofs * 1000; |
| 1430 | ttype_[0].fmt = dst_abbr; |
| 1431 | } |
| 1432 | else |
| 1433 | { |
| 1434 | ttype_[0].gmtofs = desc->std_ofs * 1000; |
| 1435 | ttype_[0].fmt = std_abbr; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | /* we have no descriptive information */ |
| 1440 | country_[0] = '\0'; |
| 1441 | coords_[0] = '\0'; |
| 1442 | desc_ = 0; |
| 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | * destruction |
| 1447 | */ |
| 1448 | CVmTimeZone::~CVmTimeZone() |
| 1449 | { |
| 1450 | /* delete allocated items */ |
| 1451 | lib_free_str(name_); |
| 1452 | if (trans_ != 0) |
| 1453 | delete [] trans_; |
| 1454 | if (ttype_ != 0) |
| 1455 | delete [] ttype_; |
| 1456 | if (rule_ != 0) |
| 1457 | delete [] rule_; |
| 1458 | if (abbr_ != 0) |
| 1459 | delete [] abbr_; |
| 1460 | if (desc_ != 0) |
| 1461 | delete [] desc_; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * Load or create a CVmTimeZone object from a hash table entry. This |
| 1466 | * creates a new object, even if the hash table entry already has an |
| 1467 | * associated object. |
| 1468 | */ |
| 1469 | CVmTimeZone *CVmTimeZone::load(VMG_ ZoneHashEntry *entry) |
| 1470 | { |
| 1471 | /* load according to the data source in the hash table entry */ |
| 1472 | if (entry->typ == ZONE_HASH_DB) |
| 1473 | { |
| 1474 | /* it's in the database file - open the file */ |
| 1475 | osfildef *fp = CVmTimeZoneCache::open_zoneinfo_file(vmg0_); |
| 1476 | if (fp == 0) |
| 1477 | return 0; |
| 1478 | |
| 1479 | /* seek to our data */ |
| 1480 | osfseek(fp, ((ZoneHashEntryDb *)entry)->seekpos, OSFSK_SET); |
| 1481 | |
| 1482 | /* read the length prefix */ |
| 1483 | char pfx[2]; |
| 1484 | int ok = !osfrb(fp, pfx, 2); |
| 1485 | |
| 1486 | /* allocate space for the file data */ |
| 1487 | unsigned int alo = osrp2(pfx); |
| 1488 | char *buf = ok ? new char[alo] : 0; |
| 1489 | |
| 1490 | /* read the file data */ |
| 1491 | ok = buf != 0 && !osfrb(fp, buf, alo); |
| 1492 | |
| 1493 | /* decode the header */ |
| 1494 | unsigned int trans_cnt = osrp2(buf); |
| 1495 | unsigned int type_cnt = osrp2(buf+2); |
| 1496 | unsigned int rule_cnt = osrp1(buf+4); |
| 1497 | unsigned int abbr_bytes = osrp1(buf+5); |
| 1498 | |
| 1499 | /* create the time zone object */ |
| 1500 | CVmTimeZone *tz = 0; |
| 1501 | if (ok) |
| 1502 | { |
| 1503 | tz = new CVmTimeZone( |
| 1504 | (ZoneHashEntryDb *)entry, |
| 1505 | buf+6, trans_cnt, type_cnt, rule_cnt, abbr_bytes); |
| 1506 | } |
| 1507 | |
| 1508 | /* release resources */ |
| 1509 | if (buf != 0) |
| 1510 | delete [] buf; |
| 1511 | osfcls(fp); |
| 1512 | |
| 1513 | /* return the loaded object */ |
| 1514 | return tz; |
| 1515 | } |
| 1516 | else if (entry->typ == ZONE_HASH_LINK) |
| 1517 | { |
| 1518 | /* load from the linked object */ |
| 1519 | return load(vmg_ entry->resolve_links()); |
| 1520 | } |
| 1521 | else if (entry->typ == ZONE_HASH_SYNTH) |
| 1522 | { |
| 1523 | /* create from the zone descriptor */ |
| 1524 | return new CVmTimeZone((ZoneHashEntrySynth *)entry); |
| 1525 | } |
| 1526 | else |
| 1527 | { |
| 1528 | /* unknown type */ |
| 1529 | return 0; |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | /* ------------------------------------------------------------------------ */ |
| 1534 | /* |
| 1535 | * Get the zone name |
| 1536 | */ |
| 1537 | const char *CVmTimeZone::get_name(size_t &len) const |
| 1538 | { |
| 1539 | /* use the name, or the hash table name, or the primary abbreviation */ |
| 1540 | if (name_ != 0) |
| 1541 | { |
| 1542 | /* we have an explicit name field - use it */ |
| 1543 | len = strlen(name_); |
| 1544 | return name_; |
| 1545 | } |
| 1546 | else if (hashentry_ != 0) |
| 1547 | { |
| 1548 | /* get the name from our hash table entry */ |
| 1549 | len = hashentry_->getlen(); |
| 1550 | return hashentry_->getstr(); |
| 1551 | } |
| 1552 | else |
| 1553 | { |
| 1554 | /* no name or hash entry - use our primary abbreviation */ |
| 1555 | len = strlen(abbr_); |
| 1556 | return abbr_; |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | /* |
| 1561 | * Populate a List object with our alias list. This sets the first element |
| 1562 | * to our primary name, and remaining elements to aliases. |
| 1563 | */ |
| 1564 | vm_obj_id_t CVmTimeZone::get_name_list(VMG0_) const |
| 1565 | { |
| 1566 | /* no names found so far */ |
| 1567 | int cnt = 0; |
| 1568 | |
| 1569 | /* count names, depending on what information we have */ |
| 1570 | if (hashentry_ != 0) |
| 1571 | { |
| 1572 | /* the hash entry gives us our primary name */ |
| 1573 | cnt += 1; |
| 1574 | |
| 1575 | /* count incoming alias links */ |
| 1576 | for (ZoneHashEntryLink *e = hashentry_->link_from ; e != 0 ; |
| 1577 | e = e->link_nxt, ++cnt) ; |
| 1578 | } |
| 1579 | else if (name_ != 0) |
| 1580 | { |
| 1581 | /* we have only the one primary name */ |
| 1582 | cnt += 1; |
| 1583 | } |
| 1584 | else if (abbr_ != 0 && abbr_[0] != '\0') |
| 1585 | { |
| 1586 | /* no name, but we at least have an abbreviation we can use in lieu */ |
| 1587 | cnt += 1; |
| 1588 | } |
| 1589 | |
| 1590 | /* allocate the result list; stack it for gc protection */ |
| 1591 | vm_obj_id_t lstid = CVmObjList::create(vmg_ FALSE, cnt); |
| 1592 | G_stk->push()->set_obj(lstid); |
| 1593 | CVmObjList *lst = (CVmObjList *)vm_objp(vmg_ lstid); |
| 1594 | lst->cons_clear(); |
| 1595 | |
| 1596 | /* start at index 0 */ |
| 1597 | int idx = 0; |
| 1598 | vm_val_t ele; |
| 1599 | |
| 1600 | /* |
| 1601 | * add our primary name - this is our explicit name_ string, our hash |
| 1602 | * entry name, or our primary abbreviation (in order of priority) |
| 1603 | */ |
| 1604 | if (hashentry_ != 0) |
| 1605 | { |
| 1606 | /* set the name from our hash entry */ |
| 1607 | ele.set_obj(CVmObjString::create( |
| 1608 | vmg_ FALSE, hashentry_->getstr(), hashentry_->getlen())); |
| 1609 | lst->cons_set_element(idx++, &ele); |
| 1610 | |
| 1611 | /* add our aliases */ |
| 1612 | for (ZoneHashEntryLink *link = hashentry_->link_from ; link != 0 ; |
| 1613 | link = link->link_nxt) |
| 1614 | { |
| 1615 | ele.set_obj(CVmObjString::create( |
| 1616 | vmg_ FALSE, link->getstr(), link->getlen())); |
| 1617 | lst->cons_set_element(idx++, &ele); |
| 1618 | } |
| 1619 | } |
| 1620 | else if (name_ != 0) |
| 1621 | { |
| 1622 | /* the first item is our primary name */ |
| 1623 | ele.set_obj(CVmObjString::create(vmg_ FALSE, name_, strlen(name_))); |
| 1624 | lst->cons_set_element(idx++, &ele); |
| 1625 | } |
| 1626 | else if (abbr_ != 0 && abbr_[0] != '\0') |
| 1627 | { |
| 1628 | /* use our primary abbreviation as our name */ |
| 1629 | ele.set_obj(CVmObjString::create(vmg_ FALSE, abbr_, strlen(abbr_))); |
| 1630 | lst->cons_set_element(idx++, &ele); |
| 1631 | } |
| 1632 | |
| 1633 | /* discard the gc protection */ |
| 1634 | G_stk->discard(); |
| 1635 | |
| 1636 | /* return the new list object */ |
| 1637 | return lstid; |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Get a history item for a given date |
| 1642 | */ |
| 1643 | vm_obj_id_t CVmTimeZone::get_history_item( |
| 1644 | VMG_ int32_t dayno, int32_t daytime) const |
| 1645 | { |
| 1646 | /* query the transition for the given date/time */ |
| 1647 | vmtzquery result; |
| 1648 | query(&result, dayno, daytime, FALSE); |
| 1649 | |
| 1650 | /* synthesize a transition description */ |
| 1651 | vmtz_ttype tty(&result); |
| 1652 | vmtz_trans t(result.start_dayno, result.start_daytime, &tty); |
| 1653 | |
| 1654 | /* build and return the List representation */ |
| 1655 | return t.get_as_list(vmg0_); |
| 1656 | } |
| 1657 | |
| 1658 | /* |
| 1659 | * Get a list of history items |
| 1660 | */ |
| 1661 | vm_obj_id_t CVmTimeZone::get_history_list(VMG0_) const |
| 1662 | { |
| 1663 | /* allocate the result list; stack it for gc protection */ |
| 1664 | vm_obj_id_t lstid = CVmObjList::create(vmg_ FALSE, trans_cnt_ + 1); |
| 1665 | G_stk->push()->set_obj(lstid); |
| 1666 | CVmObjList *lst = (CVmObjList *)vm_objp(vmg_ lstid); |
| 1667 | lst->cons_clear(); |
| 1668 | |
| 1669 | /* set the special first item for the pre-establishment settings */ |
| 1670 | vmtz_trans t_pre(INT32MINVAL, INT32MINVAL, &ttype_[0]); |
| 1671 | vm_val_t ele; |
| 1672 | ele.set_obj(t_pre.get_as_list(vmg0_)); |
| 1673 | lst->cons_set_element(0, &ele); |
| 1674 | |
| 1675 | /* run through the transitions */ |
| 1676 | vmtz_trans *t = trans_; |
| 1677 | for (int i = 1 ; i <= trans_cnt_ ; ++i, ++t) |
| 1678 | { |
| 1679 | /* add this transition to the list */ |
| 1680 | ele.set_obj(t->get_as_list(vmg0_)); |
| 1681 | lst->cons_set_element(i, &ele); |
| 1682 | } |
| 1683 | |
| 1684 | /* discard the gc protection and return the result list */ |
| 1685 | G_stk->discard(1); |
| 1686 | return lstid; |
| 1687 | } |
| 1688 | |
| 1689 | /* |
| 1690 | * Get a list of the ongoing rules |
| 1691 | */ |
| 1692 | vm_obj_id_t CVmTimeZone::get_rule_list(VMG0_) const |
| 1693 | { |
| 1694 | /* allocate the result list; stack it for gc protection */ |
| 1695 | vm_obj_id_t lstid = CVmObjList::create(vmg_ FALSE, rule_cnt_); |
| 1696 | G_stk->push()->set_obj(lstid); |
| 1697 | CVmObjList *lst = (CVmObjList *)vm_objp(vmg_ lstid); |
| 1698 | lst->cons_clear(); |
| 1699 | |
| 1700 | /* run through the rules */ |
| 1701 | vmtz_rule *r = rule_; |
| 1702 | for (int i = 0 ; i < rule_cnt_ ; ++i, ++r) |
| 1703 | { |
| 1704 | /* create a sublist to describe this rule */ |
| 1705 | vm_val_t ele; |
| 1706 | ele.set_obj(CVmObjList::create(vmg_ FALSE, 10)); |
| 1707 | lst->cons_set_element(i, &ele); |
| 1708 | |
| 1709 | /* get the sublist and clear it out */ |
| 1710 | CVmObjList *elst = (CVmObjList *)vm_objp(vmg_ ele.val.obj); |
| 1711 | elst->cons_clear(); |
| 1712 | |
| 1713 | /* [1] = abbreviation */ |
| 1714 | ele.set_obj(CVmObjString::create(vmg_ FALSE, r->fmt, strlen(r->fmt))); |
| 1715 | elst->cons_set_element(0, &ele); |
| 1716 | |
| 1717 | /* [2] = standard time offset in milliseconds */ |
| 1718 | ele.set_int(r->gmtofs); |
| 1719 | elst->cons_set_element(1, &ele); |
| 1720 | |
| 1721 | /* [3] = daylight savings delta in milliseconds */ |
| 1722 | ele.set_int(r->save); |
| 1723 | elst->cons_set_element(2, &ele); |
| 1724 | |
| 1725 | /* [4] = encoded 'when' rule: "Mar lastSun", "Mar Sun>=7", "Mar 7" */ |
| 1726 | char buf[128]; |
| 1727 | static const char *mon = "JanFebMarAprMayJunJulAugSepOctNov"; |
| 1728 | static const char *wkdy = "SunMonTueWedThuFriSat"; |
| 1729 | static const char *tpl[] = { |
| 1730 | "%.0s%02d", "last %.3s", "%.3s>=%d", "%.3s<=d" |
| 1731 | }; |
| 1732 | if (r->when == 0 && r->dd > 31) |
| 1733 | { |
| 1734 | /* day of month past 31 -> it's actually a day of the year */ |
| 1735 | t3sprintf(buf, sizeof(buf), "DOY %d", r->dd); |
| 1736 | } |
| 1737 | else if (r->when >= 0 && r->when < countof(tpl) |
| 1738 | && r->weekday >= 1 && r->weekday <= 7) |
| 1739 | { |
| 1740 | /* valid 'when' code */ |
| 1741 | memcpy(buf, mon + (r->mm - 1)*3, 3); |
| 1742 | buf[3] = ' '; |
| 1743 | t3sprintf(buf+4, sizeof(buf)-4, tpl[(unsigned char)r->when], |
| 1744 | wkdy + (r->weekday-1)*3, r->dd); |
| 1745 | } |
| 1746 | else |
| 1747 | { |
| 1748 | /* invalid 'when' code */ |
| 1749 | t3sprintf(buf, sizeof(buf), "INVAL(when=%d, weekday=%d, dd=%d)", |
| 1750 | r->when, r->weekday, r->dd); |
| 1751 | } |
| 1752 | |
| 1753 | ele.set_obj(CVmObjString::create(vmg_ FALSE, buf, strlen(buf))); |
| 1754 | elst->cons_set_element(3, &ele); |
| 1755 | |
| 1756 | /* [5] = month */ |
| 1757 | ele.set_int(r->mm); |
| 1758 | elst->cons_set_element(4, &ele); |
| 1759 | |
| 1760 | /* [6] = when */ |
| 1761 | ele.set_int(r->when); |
| 1762 | elst->cons_set_element(5, &ele); |
| 1763 | |
| 1764 | /* [7] = day of the month */ |
| 1765 | ele.set_int(r->dd); |
| 1766 | elst->cons_set_element(6, &ele); |
| 1767 | |
| 1768 | /* [8] = weekday */ |
| 1769 | ele.set_int(r->weekday); |
| 1770 | elst->cons_set_element(7, &ele); |
| 1771 | |
| 1772 | /* [9] = at time */ |
| 1773 | ele.set_int(r->at); |
| 1774 | elst->cons_set_element(8, &ele); |
| 1775 | |
| 1776 | /* [10] = at zone */ |
| 1777 | char u = (r->at_zone == 0x80 ? 'u' : r->at_zone == 0x40 ? 's' : 'w'); |
| 1778 | ele.set_obj(CVmObjString::create(vmg_ FALSE, &u, 1)); |
| 1779 | elst->cons_set_element(9, &ele); |
| 1780 | } |
| 1781 | |
| 1782 | /* discard the gc protection and return the result list */ |
| 1783 | G_stk->discard(1); |
| 1784 | return lstid; |
| 1785 | } |
| 1786 | |
| 1787 | /* ------------------------------------------------------------------------ */ |
| 1788 | /* |
| 1789 | * Query the time zone information at the current moment |
| 1790 | */ |
| 1791 | void CVmTimeZone::query(vmtzquery *result) const |
| 1792 | { |
| 1793 | /* get the current system time */ |
| 1794 | int32_t dayno, daytime; |
| 1795 | caldate_t::now(dayno, daytime); |
| 1796 | |
| 1797 | /* query the time zone information */ |
| 1798 | query(result, dayno, daytime, FALSE); |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | * Query the time zone information at the given time (UTC); daytime is |
| 1803 | * milliseconds after midnight. |
| 1804 | */ |
| 1805 | void CVmTimeZone::query(vmtzquery *result, int32_t dayno, int32_t daytime, |
| 1806 | int local) const |
| 1807 | { |
| 1808 | /* |
| 1809 | * if we're after the last transition, or we have no transitions, check |
| 1810 | * for ongoing rules |
| 1811 | */ |
| 1812 | if (trans_cnt_ == 0 |
| 1813 | || trans_[trans_cnt_-1].compare_to(dayno, daytime, local) <= 0) |
| 1814 | { |
| 1815 | /* |
| 1816 | * We won't find our answer in the transition list, so check for |
| 1817 | * ongoing rules that cover out-of-bounds dates. |
| 1818 | */ |
| 1819 | if (rule_cnt_ != 0) |
| 1820 | { |
| 1821 | /* |
| 1822 | * We have ongoing rules. |
| 1823 | * |
| 1824 | * Starting in the year AFTER the target date, search for the |
| 1825 | * last (in time) rule that fires on or before the target date. |
| 1826 | * This is the rule that defines the time zone for the target |
| 1827 | * date. If we don't find a rule on or before the target date, |
| 1828 | * back up one year and repeat. |
| 1829 | * |
| 1830 | * The reason we have to start next year is that there's a |
| 1831 | * possibility that the target date will be in the following |
| 1832 | * year in local time; rules are generally stated in local |
| 1833 | * time, so this difference could matter. For example, if the |
| 1834 | * target date is Dec 31 2010 23:59 UTC, that could be Jan 1 |
| 1835 | * 2011 07:59 local time. If we then had a rule that fired at |
| 1836 | * 01:00 on Jan 1, that rule in 2011 would be the correct |
| 1837 | * result of our search. This is a pathological case that's |
| 1838 | * unlikely to arise in the wild, but it's not impossible. |
| 1839 | * Likewise, a target date on Jan 1 could end up preceding a |
| 1840 | * rule that fires on Dec 31 the previous year; looping |
| 1841 | * backwards on year until we find a rule that fires on or |
| 1842 | * before the target will catch these cases. |
| 1843 | * |
| 1844 | * Note that our zoneinfo compiler guarantees that it will |
| 1845 | * produce at least one complete cycle fixed transitions (in |
| 1846 | * our trans_ list) for the open-ended rules. Since we're |
| 1847 | * after our last transition, we know that the period before a |
| 1848 | * rule's trigger date is controlled by the previous rule (in |
| 1849 | * time order, treating the list as circular) and not by any |
| 1850 | * other zone history change. This makes it possible to work |
| 1851 | * backwards through the list, since we don't have to look for |
| 1852 | * other possible zone definitions in the period before each |
| 1853 | * rule. |
| 1854 | */ |
| 1855 | caldate_t cd(dayno); |
| 1856 | for (int yy = cd.y + 1 ; yy >= cd.y - 2 ; --yy) |
| 1857 | { |
| 1858 | /* look for the last rule firing on or after the target */ |
| 1859 | int latest = -1; |
| 1860 | int32_t latest_rday = 0, latest_rtime = 0; |
| 1861 | for (int i = 0 ; i < rule_cnt_ ; ++i) |
| 1862 | { |
| 1863 | /* |
| 1864 | * we might need the previous rule to resolve the |
| 1865 | * current rule; the list is circular because it forms |
| 1866 | * an annual cycle |
| 1867 | */ |
| 1868 | int iprv = (i == 0 ? rule_cnt_ : i) - 1; |
| 1869 | |
| 1870 | /* get the concrete date for this rule in this year */ |
| 1871 | int32_t rday, rtime; |
| 1872 | rule_[i].resolve(rday, rtime, yy, &rule_[iprv]); |
| 1873 | |
| 1874 | /* adjust to local time if that's what we're looking for */ |
| 1875 | if (local) |
| 1876 | { |
| 1877 | rtime += rule_[i].gmtofs + rule_[i].save; |
| 1878 | caldate_t::normalize(rday, rtime); |
| 1879 | } |
| 1880 | |
| 1881 | /* |
| 1882 | * If the target date is after the resolved rule firing |
| 1883 | * time, and this rule is later than the last one we |
| 1884 | * matched, this is the best match so far. (Assume |
| 1885 | * that we never have two rules on the same day, so we |
| 1886 | * don't need to bother checking rtime against the last |
| 1887 | * matching rule.) Note that we don't match exactly at |
| 1888 | * the rule time - our policy is to put the moment of a |
| 1889 | * transition in the previous period. |
| 1890 | */ |
| 1891 | if ((dayno > rday || (dayno == rday && daytime > rtime)) |
| 1892 | && (latest < 0 || rday > latest_rday)) |
| 1893 | { |
| 1894 | latest = i; |
| 1895 | latest_rday = rday; |
| 1896 | latest_rtime = rtime; |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | /* if we found a match, return its settings */ |
| 1901 | if (latest >= 0) |
| 1902 | { |
| 1903 | result->set(&rule_[latest], latest_rday, latest_rtime); |
| 1904 | return; |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | /* |
| 1909 | * We should never get here - it should be *almost* impossible |
| 1910 | * not to find a match in the prior year, but not quite |
| 1911 | * impossible: if a zone only has rules that fire late on Dec |
| 1912 | * 31, we could have a target early on Jan 1 UTC which, when |
| 1913 | * translated to local time, is before all of the Dec 31 rules. |
| 1914 | * But it should be truly impossible not to find a match in |
| 1915 | * target year minus 2 - no UTC-to-local difference could take |
| 1916 | * us back a whole year. So there's something wrong in our |
| 1917 | * logic if we get here... |
| 1918 | */ |
| 1919 | assert(FALSE); |
| 1920 | } |
| 1921 | else if (trans_cnt_ != 0) |
| 1922 | { |
| 1923 | /* |
| 1924 | * There are no rules, and we're after the last transition, so |
| 1925 | * the last transition is permanent. |
| 1926 | */ |
| 1927 | result->set(&trans_[trans_cnt_-1]); |
| 1928 | } |
| 1929 | else |
| 1930 | { |
| 1931 | /* |
| 1932 | * There are no rules and no transitions. This means that the |
| 1933 | * zone is defined entirely by its first time type entry. |
| 1934 | */ |
| 1935 | result->set(&ttype_[0], INT32MINVAL, INT32MINVAL); |
| 1936 | return; |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | * If we're at or before the first transition, use the |
| 1942 | * pre-establishment settings. This is usually the local mean time |
| 1943 | * settings from before a standard time zone was first established at |
| 1944 | * this location. |
| 1945 | */ |
| 1946 | if (trans_[0].compare_to(dayno, daytime, local) >= 0) |
| 1947 | { |
| 1948 | /* the pre-establishment settings are in ttype[0] */ |
| 1949 | result->set(&ttype_[0], INT32MINVAL, INT32MINVAL); |
| 1950 | return; |
| 1951 | } |
| 1952 | |
| 1953 | /* |
| 1954 | * The date is within our transition list, so search for the nearest |
| 1955 | * transition on or before the target date. We're looking for a |
| 1956 | * date/time after our first transition and before our last, so it's a |
| 1957 | * date covered by a history item. Search our transition list for the |
| 1958 | * target date/time. |
| 1959 | */ |
| 1960 | int lo = 0, hi = trans_cnt_ - 1, cur; |
| 1961 | while (lo < hi) |
| 1962 | { |
| 1963 | /* split the difference */ |
| 1964 | cur = (hi + lo)/2; |
| 1965 | |
| 1966 | /* check this entry */ |
| 1967 | vmtz_trans *tcur = &trans_[cur]; |
| 1968 | int dir = tcur->compare_to(dayno, daytime, local); |
| 1969 | if (dir < 0) |
| 1970 | { |
| 1971 | /* tcur < target - search the upper half */ |
| 1972 | lo = cur + 1; |
| 1973 | } |
| 1974 | else if (dir > 0) |
| 1975 | { |
| 1976 | /* tcur > target - search the lower half */ |
| 1977 | hi = cur - 1; |
| 1978 | } |
| 1979 | else |
| 1980 | { |
| 1981 | /* |
| 1982 | * exact match - treat the moment of the transition as being in |
| 1983 | * the previous interval, since we usually define the moment of |
| 1984 | * the transition in terms of the local time that was in effect |
| 1985 | * up until that moment |
| 1986 | */ |
| 1987 | #if 0 /* GLK: Modified from upstream. */ |
| 1988 | result->set(tcur > 0 ? tcur - 1 : tcur); |
| 1989 | #else |
| 1990 | result->set(tcur != 0 ? tcur - 1 : tcur); |
| 1991 | #endif |
| 1992 | return; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | /* check the final 'lo' value; it might have ended up one too high */ |
| 1997 | cur = trans_[lo].compare_to(dayno, daytime, local) >= 0 |
| 1998 | && lo > 0 |
| 1999 | && trans_[lo-1].compare_to(dayno, daytime, local) < 0 ? lo - 1 : lo; |
| 2000 | |
| 2001 | /* set the result */ |
| 2002 | result->set(&trans_[cur]); |
| 2003 | } |
| 2004 | |
| 2005 | /* ------------------------------------------------------------------------ */ |
| 2006 | /* |
| 2007 | * Compare my date to the given date, which can be expressed in either UTC |
| 2008 | * or local wall clock time. |
| 2009 | */ |
| 2010 | int vmtz_trans::compare_to(int32_t dayno, int32_t daytime, int local) const |
| 2011 | { |
| 2012 | /* get my value, converting to local time if required */ |
| 2013 | int32_t dd = this->dayno, tt = this->daytime; |
| 2014 | if (local) |
| 2015 | { |
| 2016 | tt += ttype->gmtofs + ttype->save; |
| 2017 | caldate_t::normalize(dd, tt); |
| 2018 | } |
| 2019 | |
| 2020 | /* compare by day, then by time if on the same day */ |
| 2021 | return (dd != dayno ? dd - dayno : tt - daytime); |
| 2022 | } |
| 2023 | |
| 2024 | /* |
| 2025 | * Create a List object representing the transition |
| 2026 | */ |
| 2027 | vm_obj_id_t vmtz_trans::get_as_list(VMG0_) const |
| 2028 | { |
| 2029 | /* create a result list for this item - [date, ofs, save, fmt] */ |
| 2030 | vm_obj_id_t lstid = CVmObjList::create(vmg_ FALSE, 4); |
| 2031 | G_stk->push()->set_obj(lstid); |
| 2032 | |
| 2033 | /* get the list and clear it out, since we're building it in pieces */ |
| 2034 | CVmObjList *lst = (CVmObjList *)vm_objp(vmg_ lstid); |
| 2035 | lst->cons_clear(); |
| 2036 | |
| 2037 | /* |
| 2038 | * Element [1] is the transition time as a Date object, or nil if the |
| 2039 | * date is INT32MINVALs (which represents the beginning of time, for |
| 2040 | * the pre-establishment settings). |
| 2041 | */ |
| 2042 | vm_val_t ele; |
| 2043 | if (dayno == INT32MINVAL && daytime == INT32MINVAL) |
| 2044 | ele.set_nil(); |
| 2045 | else |
| 2046 | ele.set_obj(CVmObjDate::create(vmg_ FALSE, dayno, daytime)); |
| 2047 | lst->cons_set_element(0, &ele); |
| 2048 | |
| 2049 | /* element [2] is the UTC offset */ |
| 2050 | ele.set_int(ttype->gmtofs); |
| 2051 | lst->cons_set_element(1, &ele); |
| 2052 | |
| 2053 | /* element [3] is the daylight savings delta */ |
| 2054 | ele.set_int(ttype->save); |
| 2055 | lst->cons_set_element(2, &ele); |
| 2056 | |
| 2057 | /* element [4] is the abbreviation string */ |
| 2058 | ele.set_obj(CVmObjString::create( |
| 2059 | vmg_ FALSE, ttype->fmt, strlen(ttype->fmt))); |
| 2060 | lst->cons_set_element(3, &ele); |
| 2061 | |
| 2062 | /* discard gc protection and return the list object */ |
| 2063 | G_stk->discard(1); |
| 2064 | return lstid; |
| 2065 | } |
| 2066 | |
| 2067 | |
| 2068 | /* ------------------------------------------------------------------------ */ |
| 2069 | /* |
| 2070 | * synthesize a ttype from a query result |
| 2071 | */ |
| 2072 | vmtz_ttype::vmtz_ttype(const vmtzquery *query) |
| 2073 | { |
| 2074 | gmtofs = query->gmtofs; |
| 2075 | save = query->save; |
| 2076 | fmt = query->abbr; |
| 2077 | } |
| 2078 | |
| 2079 | |
| 2080 | /* ------------------------------------------------------------------------ */ |
| 2081 | /* |
| 2082 | * Resolve a rule in the given year. Returns the firing time in UTC. |
| 2083 | */ |
| 2084 | void vmtz_rule::resolve(int32_t &dayno, int32_t &daytime, int year, |
| 2085 | const vmtz_rule *prev_rule) |
| 2086 | { |
| 2087 | /* set up our year and month; we have yet to work out the day */ |
| 2088 | int w, delta; |
| 2089 | caldate_t cd(year, mm, 0); |
| 2090 | |
| 2091 | /* figure out the day according to our type */ |
| 2092 | switch (when) |
| 2093 | { |
| 2094 | case 0: |
| 2095 | /* the rule takes effect on a the fixed day 'dd' */ |
| 2096 | cd.d = dd; |
| 2097 | break; |
| 2098 | |
| 2099 | case 1: |
| 2100 | /* |
| 2101 | * The rule takes effect on the last <weekday> of the month. Get |
| 2102 | * the weekday of the last day of this month (which we can figure |
| 2103 | * using day 0 of next month). |
| 2104 | */ |
| 2105 | cd.m += 1; |
| 2106 | w = cd.weekday() + 1; |
| 2107 | |
| 2108 | /* figure the days from mm/last to the previous target weekday */ |
| 2109 | delta = w - weekday; |
| 2110 | if (delta < 0) |
| 2111 | delta += 7; |
| 2112 | |
| 2113 | /* go back that many days */ |
| 2114 | cd.d -= delta; |
| 2115 | break; |
| 2116 | |
| 2117 | case 2: |
| 2118 | /* first <weekday> on or after <dd> - get the weekday of <dd> */ |
| 2119 | cd.d = dd; |
| 2120 | w = cd.weekday() + 1; |
| 2121 | |
| 2122 | /* figure days between <dd> and the next <weekday> */ |
| 2123 | delta = weekday - w; |
| 2124 | if (delta < 0) |
| 2125 | delta += 7; |
| 2126 | |
| 2127 | /* go ahead that many days */ |
| 2128 | cd.d += delta; |
| 2129 | break; |
| 2130 | |
| 2131 | case 3: |
| 2132 | /* first <weekday> on or before <dd> - get the weekday of <dd> */ |
| 2133 | cd.d = dd; |
| 2134 | w = cd.weekday() + 1; |
| 2135 | |
| 2136 | /* figure the days from <dd> to the next <weekday> */ |
| 2137 | delta = weekday - w; |
| 2138 | if (delta < 0) |
| 2139 | delta += 7; |
| 2140 | |
| 2141 | /* go ahead that many days and back a week */ |
| 2142 | cd.d += delta - 7; |
| 2143 | break; |
| 2144 | } |
| 2145 | |
| 2146 | /* we now have our effective date and time */ |
| 2147 | dayno = cd.dayno(); |
| 2148 | daytime = at; |
| 2149 | |
| 2150 | /* adjust the time zone */ |
| 2151 | switch (at_zone) |
| 2152 | { |
| 2153 | case 0x80: |
| 2154 | /* the rule was stated in UTC, which is what we're after */ |
| 2155 | break; |
| 2156 | |
| 2157 | case 0x00: |
| 2158 | /* |
| 2159 | * The rule was stated in local wall clock time, in the period in |
| 2160 | * effect just before the rule. That means we use the zone |
| 2161 | * settings of the *previous* rule, which defines the period |
| 2162 | * leading up to this rule's firing time. Convert to UTC by |
| 2163 | * subtracting the standard time offset plus the daylight offset |
| 2164 | * for the prior period. |
| 2165 | */ |
| 2166 | daytime -= prev_rule->gmtofs + prev_rule->save; |
| 2167 | break; |
| 2168 | |
| 2169 | case 0x40: |
| 2170 | /* |
| 2171 | * the rule was stated in local standard time, so subtract the |
| 2172 | * standard time offset for the prior period |
| 2173 | */ |
| 2174 | daytime -= prev_rule->gmtofs; |
| 2175 | break; |
| 2176 | } |
| 2177 | |
| 2178 | /* normalize the time to keep it within 0-23:59:59 */ |
| 2179 | caldate_t::normalize(dayno, daytime); |
| 2180 | } |
| 2181 | |
| 2182 | /* ------------------------------------------------------------------------ */ |
| 2183 | /* |
| 2184 | * Set from an OS descriptor |
| 2185 | */ |
| 2186 | void vmtz_rule::set(const os_tzrule_t *desc, const char *abbr, |
| 2187 | int32_t gmtofs, int32_t save) |
| 2188 | { |
| 2189 | /* set the basic description */ |
| 2190 | this->fmt = abbr; |
| 2191 | this->gmtofs = gmtofs; |
| 2192 | this->save = save; |
| 2193 | |
| 2194 | /* the OS description always uses local wall clock time */ |
| 2195 | this->at = desc->time * 1000; |
| 2196 | this->at_zone = 0; |
| 2197 | |
| 2198 | /* transcode the date to our format */ |
| 2199 | if (desc->jday != 0) |
| 2200 | { |
| 2201 | /* |
| 2202 | * "Jn" = nth day of the year, NOT counting Feb 29. Since the date |
| 2203 | * mapping is the same for leap years and non-leap years, this is |
| 2204 | * just an obtuse way of specifying a month and day. So, figure |
| 2205 | * the mm/dd date that the Jn date represents, and map it to our |
| 2206 | * mode 0 (day <dd> of <month>). We can figure the month/day by |
| 2207 | * setting up a calendar with "January N" in any non-leap year - |
| 2208 | * since it's a non-leap year, it won't have a Feb 29, so we'll get |
| 2209 | * the right mapping for dates after J59. |
| 2210 | */ |
| 2211 | caldate_t cd(2011, 1, desc->jday); |
| 2212 | cd.normalize_date(); |
| 2213 | this->when = 0; |
| 2214 | this->mm = (char)cd.m; |
| 2215 | this->dd = (short)cd.d; |
| 2216 | } |
| 2217 | else if (desc->yday != 0) |
| 2218 | { |
| 2219 | /* |
| 2220 | * "n" = nth day of the year, counting Feb 29. We can encode this |
| 2221 | * as "January n", even if n > 31, since our calendar calculator |
| 2222 | * handle overflows in the day of the month by carrying them into |
| 2223 | * subsequent months. |
| 2224 | */ |
| 2225 | this->when = 0; |
| 2226 | this->mm = 1; |
| 2227 | this->dd = (short)desc->yday; |
| 2228 | } |
| 2229 | else if (desc->month != 0) |
| 2230 | { |
| 2231 | /* Month/week/day spec */ |
| 2232 | this->mm = (char)desc->month; |
| 2233 | this->weekday = (char)desc->day; |
| 2234 | if (desc->week == 5) |
| 2235 | { |
| 2236 | /* week 5 means "last <weekday>", which is our mode 1 */ |
| 2237 | this->when = 1; |
| 2238 | } |
| 2239 | else |
| 2240 | { |
| 2241 | /* |
| 2242 | * week 1-4 means "nth <weekday>"; we can encode this as mode |
| 2243 | * 2, "weekday>=n", where n is 1 for the first week, 8 for the |
| 2244 | * second week, 15 for the third week, 22 for the fourth week |
| 2245 | */ |
| 2246 | this->when = 2; |
| 2247 | this->dd = (char)(desc->week - 1)*7 + 1; |
| 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | /* ------------------------------------------------------------------------ */ |
| 2253 | /* |
| 2254 | * Convert a UTC time to local |
| 2255 | */ |
| 2256 | const char *CVmTimeZone::utc_to_local( |
| 2257 | int32_t &dayno, int32_t &daytime, int32_t &tzofs) |
| 2258 | { |
| 2259 | /* look up the time zone information at the given time */ |
| 2260 | vmtzquery result; |
| 2261 | query(&result, dayno, daytime, FALSE); |
| 2262 | |
| 2263 | /* figure the full offset */ |
| 2264 | tzofs = result.gmtofs + result.save; |
| 2265 | |
| 2266 | /* adjust the time to local by adding the GMT offset */ |
| 2267 | daytime += tzofs; |
| 2268 | caldate_t::normalize(dayno, daytime); |
| 2269 | |
| 2270 | /* return the abbreviation */ |
| 2271 | return result.abbr; |
| 2272 | } |
| 2273 | |
| 2274 | /* |
| 2275 | * Convert a local time to UTC |
| 2276 | */ |
| 2277 | void CVmTimeZone::local_to_utc(int32_t &dayno, int32_t &daytime) |
| 2278 | { |
| 2279 | /* look up the time zone information at the given local time */ |
| 2280 | vmtzquery result; |
| 2281 | query(&result, dayno, daytime, TRUE); |
| 2282 | |
| 2283 | /* adjust the time to local by subtracting the GMT offset */ |
| 2284 | daytime -= result.gmtofs + result.save; |
| 2285 | caldate_t::normalize(dayno, daytime); |
| 2286 | } |
| 2287 |