jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # clock.tcl -- jpayne@69: # jpayne@69: # This file implements the portions of the [clock] ensemble that are jpayne@69: # coded in Tcl. Refer to the users' manual to see the description of jpayne@69: # the [clock] command and its subcommands. jpayne@69: # jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # Copyright (c) 2004-2007 Kevin B. Kenny jpayne@69: # See the file "license.terms" for information on usage and redistribution jpayne@69: # of this file, and for a DISCLAIMER OF ALL WARRANTIES. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: # We must have message catalogs that support the root locale, and we need jpayne@69: # access to the Registry on Windows systems. jpayne@69: jpayne@69: uplevel \#0 { jpayne@69: package require msgcat 1.6 jpayne@69: if { $::tcl_platform(platform) eq {windows} } { jpayne@69: if { [catch { package require registry 1.1 }] } { jpayne@69: namespace eval ::tcl::clock [list variable NoRegistry {}] jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Put the library directory into the namespace for the ensemble so that the jpayne@69: # library code can find message catalogs and time zone definition files. jpayne@69: jpayne@69: namespace eval ::tcl::clock \ jpayne@69: [list variable LibDir [file dirname [info script]]] jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # clock -- jpayne@69: # jpayne@69: # Manipulate times. jpayne@69: # jpayne@69: # The 'clock' command manipulates time. Refer to the user documentation for jpayne@69: # the available subcommands and what they do. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: namespace eval ::tcl::clock { jpayne@69: jpayne@69: # Export the subcommands jpayne@69: jpayne@69: namespace export format jpayne@69: namespace export clicks jpayne@69: namespace export microseconds jpayne@69: namespace export milliseconds jpayne@69: namespace export scan jpayne@69: namespace export seconds jpayne@69: namespace export add jpayne@69: jpayne@69: # Import the message catalog commands that we use. jpayne@69: jpayne@69: namespace import ::msgcat::mcload jpayne@69: namespace import ::msgcat::mclocale jpayne@69: namespace import ::msgcat::mc jpayne@69: namespace import ::msgcat::mcpackagelocale jpayne@69: jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ::tcl::clock::Initialize -- jpayne@69: # jpayne@69: # Finish initializing the 'clock' subsystem jpayne@69: # jpayne@69: # Results: jpayne@69: # None. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # Namespace variable in the 'clock' subsystem are initialized. jpayne@69: # jpayne@69: # The '::tcl::clock::Initialize' procedure initializes the namespace variables jpayne@69: # and root locale message catalog for the 'clock' subsystem. It is broken jpayne@69: # into a procedure rather than simply evaluated as a script so that it will be jpayne@69: # able to use local variables, avoiding the dangers of 'creative writing' as jpayne@69: # in Bug 1185933. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::Initialize {} { jpayne@69: jpayne@69: rename ::tcl::clock::Initialize {} jpayne@69: jpayne@69: variable LibDir jpayne@69: jpayne@69: # Define the Greenwich time zone jpayne@69: jpayne@69: proc InitTZData {} { jpayne@69: variable TZData jpayne@69: array unset TZData jpayne@69: set TZData(:Etc/GMT) { jpayne@69: {-9223372036854775808 0 0 GMT} jpayne@69: } jpayne@69: set TZData(:GMT) $TZData(:Etc/GMT) jpayne@69: set TZData(:Etc/UTC) { jpayne@69: {-9223372036854775808 0 0 UTC} jpayne@69: } jpayne@69: set TZData(:UTC) $TZData(:Etc/UTC) jpayne@69: set TZData(:localtime) {} jpayne@69: } jpayne@69: InitTZData jpayne@69: jpayne@69: mcpackagelocale set {} jpayne@69: ::msgcat::mcpackageconfig set mcfolder [file join $LibDir msgs] jpayne@69: ::msgcat::mcpackageconfig set unknowncmd "" jpayne@69: ::msgcat::mcpackageconfig set changecmd ChangeCurrentLocale jpayne@69: jpayne@69: # Define the message catalog for the root locale. jpayne@69: jpayne@69: ::msgcat::mcmset {} { jpayne@69: AM {am} jpayne@69: BCE {B.C.E.} jpayne@69: CE {C.E.} jpayne@69: DATE_FORMAT {%m/%d/%Y} jpayne@69: DATE_TIME_FORMAT {%a %b %e %H:%M:%S %Y} jpayne@69: DAYS_OF_WEEK_ABBREV { jpayne@69: Sun Mon Tue Wed Thu Fri Sat jpayne@69: } jpayne@69: DAYS_OF_WEEK_FULL { jpayne@69: Sunday Monday Tuesday Wednesday Thursday Friday Saturday jpayne@69: } jpayne@69: GREGORIAN_CHANGE_DATE 2299161 jpayne@69: LOCALE_DATE_FORMAT {%m/%d/%Y} jpayne@69: LOCALE_DATE_TIME_FORMAT {%a %b %e %H:%M:%S %Y} jpayne@69: LOCALE_ERAS {} jpayne@69: LOCALE_NUMERALS { jpayne@69: 00 01 02 03 04 05 06 07 08 09 jpayne@69: 10 11 12 13 14 15 16 17 18 19 jpayne@69: 20 21 22 23 24 25 26 27 28 29 jpayne@69: 30 31 32 33 34 35 36 37 38 39 jpayne@69: 40 41 42 43 44 45 46 47 48 49 jpayne@69: 50 51 52 53 54 55 56 57 58 59 jpayne@69: 60 61 62 63 64 65 66 67 68 69 jpayne@69: 70 71 72 73 74 75 76 77 78 79 jpayne@69: 80 81 82 83 84 85 86 87 88 89 jpayne@69: 90 91 92 93 94 95 96 97 98 99 jpayne@69: } jpayne@69: LOCALE_TIME_FORMAT {%H:%M:%S} jpayne@69: LOCALE_YEAR_FORMAT {%EC%Ey} jpayne@69: MONTHS_ABBREV { jpayne@69: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec jpayne@69: } jpayne@69: MONTHS_FULL { jpayne@69: January February March jpayne@69: April May June jpayne@69: July August September jpayne@69: October November December jpayne@69: } jpayne@69: PM {pm} jpayne@69: TIME_FORMAT {%H:%M:%S} jpayne@69: TIME_FORMAT_12 {%I:%M:%S %P} jpayne@69: TIME_FORMAT_24 {%H:%M} jpayne@69: TIME_FORMAT_24_SECS {%H:%M:%S} jpayne@69: } jpayne@69: jpayne@69: # Define a few Gregorian change dates for other locales. In most cases jpayne@69: # the change date follows a language, because a nation's colonies changed jpayne@69: # at the same time as the nation itself. In many cases, different jpayne@69: # national boundaries existed; the dominating rule is to follow the jpayne@69: # nation's capital. jpayne@69: jpayne@69: # Italy, Spain, Portugal, Poland jpayne@69: jpayne@69: ::msgcat::mcset it GREGORIAN_CHANGE_DATE 2299161 jpayne@69: ::msgcat::mcset es GREGORIAN_CHANGE_DATE 2299161 jpayne@69: ::msgcat::mcset pt GREGORIAN_CHANGE_DATE 2299161 jpayne@69: ::msgcat::mcset pl GREGORIAN_CHANGE_DATE 2299161 jpayne@69: jpayne@69: # France, Austria jpayne@69: jpayne@69: ::msgcat::mcset fr GREGORIAN_CHANGE_DATE 2299227 jpayne@69: jpayne@69: # For Belgium, we follow Southern Netherlands; Liege Diocese changed jpayne@69: # several weeks later. jpayne@69: jpayne@69: ::msgcat::mcset fr_BE GREGORIAN_CHANGE_DATE 2299238 jpayne@69: ::msgcat::mcset nl_BE GREGORIAN_CHANGE_DATE 2299238 jpayne@69: jpayne@69: # Austria jpayne@69: jpayne@69: ::msgcat::mcset de_AT GREGORIAN_CHANGE_DATE 2299527 jpayne@69: jpayne@69: # Hungary jpayne@69: jpayne@69: ::msgcat::mcset hu GREGORIAN_CHANGE_DATE 2301004 jpayne@69: jpayne@69: # Germany, Norway, Denmark (Catholic Germany changed earlier) jpayne@69: jpayne@69: ::msgcat::mcset de_DE GREGORIAN_CHANGE_DATE 2342032 jpayne@69: ::msgcat::mcset nb GREGORIAN_CHANGE_DATE 2342032 jpayne@69: ::msgcat::mcset nn GREGORIAN_CHANGE_DATE 2342032 jpayne@69: ::msgcat::mcset no GREGORIAN_CHANGE_DATE 2342032 jpayne@69: ::msgcat::mcset da GREGORIAN_CHANGE_DATE 2342032 jpayne@69: jpayne@69: # Holland (Brabant, Gelderland, Flanders, Friesland, etc. changed at jpayne@69: # various times) jpayne@69: jpayne@69: ::msgcat::mcset nl GREGORIAN_CHANGE_DATE 2342165 jpayne@69: jpayne@69: # Protestant Switzerland (Catholic cantons changed earlier) jpayne@69: jpayne@69: ::msgcat::mcset fr_CH GREGORIAN_CHANGE_DATE 2361342 jpayne@69: ::msgcat::mcset it_CH GREGORIAN_CHANGE_DATE 2361342 jpayne@69: ::msgcat::mcset de_CH GREGORIAN_CHANGE_DATE 2361342 jpayne@69: jpayne@69: # English speaking countries jpayne@69: jpayne@69: ::msgcat::mcset en GREGORIAN_CHANGE_DATE 2361222 jpayne@69: jpayne@69: # Sweden (had several changes onto and off of the Gregorian calendar) jpayne@69: jpayne@69: ::msgcat::mcset sv GREGORIAN_CHANGE_DATE 2361390 jpayne@69: jpayne@69: # Russia jpayne@69: jpayne@69: ::msgcat::mcset ru GREGORIAN_CHANGE_DATE 2421639 jpayne@69: jpayne@69: # Romania (Transylvania changed earler - perhaps de_RO should show the jpayne@69: # earlier date?) jpayne@69: jpayne@69: ::msgcat::mcset ro GREGORIAN_CHANGE_DATE 2422063 jpayne@69: jpayne@69: # Greece jpayne@69: jpayne@69: ::msgcat::mcset el GREGORIAN_CHANGE_DATE 2423480 jpayne@69: jpayne@69: #------------------------------------------------------------------ jpayne@69: # jpayne@69: # CONSTANTS jpayne@69: # jpayne@69: #------------------------------------------------------------------ jpayne@69: jpayne@69: # Paths at which binary time zone data for the Olson libraries are known jpayne@69: # to reside on various operating systems jpayne@69: jpayne@69: variable ZoneinfoPaths {} jpayne@69: foreach path { jpayne@69: /usr/share/zoneinfo jpayne@69: /usr/share/lib/zoneinfo jpayne@69: /usr/lib/zoneinfo jpayne@69: /usr/local/etc/zoneinfo jpayne@69: } { jpayne@69: if { [file isdirectory $path] } { jpayne@69: lappend ZoneinfoPaths $path jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Define the directories for time zone data and message catalogs. jpayne@69: jpayne@69: variable DataDir [file join $LibDir tzdata] jpayne@69: jpayne@69: # Number of days in the months, in common years and leap years. jpayne@69: jpayne@69: variable DaysInRomanMonthInCommonYear \ jpayne@69: { 31 28 31 30 31 30 31 31 30 31 30 31 } jpayne@69: variable DaysInRomanMonthInLeapYear \ jpayne@69: { 31 29 31 30 31 30 31 31 30 31 30 31 } jpayne@69: variable DaysInPriorMonthsInCommonYear [list 0] jpayne@69: variable DaysInPriorMonthsInLeapYear [list 0] jpayne@69: set i 0 jpayne@69: foreach j $DaysInRomanMonthInCommonYear { jpayne@69: lappend DaysInPriorMonthsInCommonYear [incr i $j] jpayne@69: } jpayne@69: set i 0 jpayne@69: foreach j $DaysInRomanMonthInLeapYear { jpayne@69: lappend DaysInPriorMonthsInLeapYear [incr i $j] jpayne@69: } jpayne@69: jpayne@69: # Another epoch (Hi, Jeff!) jpayne@69: jpayne@69: variable Roddenberry 1946 jpayne@69: jpayne@69: # Integer ranges jpayne@69: jpayne@69: variable MINWIDE -9223372036854775808 jpayne@69: variable MAXWIDE 9223372036854775807 jpayne@69: jpayne@69: # Day before Leap Day jpayne@69: jpayne@69: variable FEB_28 58 jpayne@69: jpayne@69: # Translation table to map Windows TZI onto cities, so that the Olson jpayne@69: # rules can apply. In some cases the mapping is ambiguous, so it's wise jpayne@69: # to specify $::env(TCL_TZ) rather than simply depending on the system jpayne@69: # time zone. jpayne@69: jpayne@69: # The keys are long lists of values obtained from the time zone jpayne@69: # information in the Registry. In order, the list elements are: jpayne@69: # Bias StandardBias DaylightBias jpayne@69: # StandardDate.wYear StandardDate.wMonth StandardDate.wDayOfWeek jpayne@69: # StandardDate.wDay StandardDate.wHour StandardDate.wMinute jpayne@69: # StandardDate.wSecond StandardDate.wMilliseconds jpayne@69: # DaylightDate.wYear DaylightDate.wMonth DaylightDate.wDayOfWeek jpayne@69: # DaylightDate.wDay DaylightDate.wHour DaylightDate.wMinute jpayne@69: # DaylightDate.wSecond DaylightDate.wMilliseconds jpayne@69: # The values are the names of time zones where those rules apply. There jpayne@69: # is considerable ambiguity in certain zones; an attempt has been made to jpayne@69: # make a reasonable guess, but this table needs to be taken with a grain jpayne@69: # of salt. jpayne@69: jpayne@69: variable WinZoneInfo [dict create {*}{ jpayne@69: {-43200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Kwajalein jpayne@69: {-39600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Midway jpayne@69: {-36000 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Honolulu jpayne@69: {-32400 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/Anchorage jpayne@69: {-28800 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/Los_Angeles jpayne@69: {-28800 0 3600 0 10 0 5 2 0 0 0 0 4 0 1 2 0 0 0} :America/Tijuana jpayne@69: {-25200 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/Denver jpayne@69: {-25200 0 3600 0 10 0 5 2 0 0 0 0 4 0 1 2 0 0 0} :America/Chihuahua jpayne@69: {-25200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :America/Phoenix jpayne@69: {-21600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :America/Regina jpayne@69: {-21600 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/Chicago jpayne@69: {-21600 0 3600 0 10 0 5 2 0 0 0 0 4 0 1 2 0 0 0} :America/Mexico_City jpayne@69: {-18000 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/New_York jpayne@69: {-18000 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :America/Indianapolis jpayne@69: {-14400 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :America/Caracas jpayne@69: {-14400 0 3600 0 3 6 2 23 59 59 999 0 10 6 2 23 59 59 999} jpayne@69: :America/Santiago jpayne@69: {-14400 0 3600 0 2 0 5 2 0 0 0 0 11 0 1 2 0 0 0} :America/Manaus jpayne@69: {-14400 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/Halifax jpayne@69: {-12600 0 3600 0 10 0 5 2 0 0 0 0 4 0 1 2 0 0 0} :America/St_Johns jpayne@69: {-10800 0 3600 0 2 0 2 2 0 0 0 0 10 0 3 2 0 0 0} :America/Sao_Paulo jpayne@69: {-10800 0 3600 0 10 0 5 2 0 0 0 0 4 0 1 2 0 0 0} :America/Godthab jpayne@69: {-10800 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :America/Buenos_Aires jpayne@69: {-10800 0 3600 0 2 0 5 2 0 0 0 0 11 0 1 2 0 0 0} :America/Bahia jpayne@69: {-10800 0 3600 0 3 0 2 2 0 0 0 0 10 0 1 2 0 0 0} :America/Montevideo jpayne@69: {-7200 0 3600 0 9 0 5 2 0 0 0 0 3 0 5 2 0 0 0} :America/Noronha jpayne@69: {-3600 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Atlantic/Azores jpayne@69: {-3600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Atlantic/Cape_Verde jpayne@69: {0 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :UTC jpayne@69: {0 0 3600 0 10 0 5 2 0 0 0 0 3 0 5 1 0 0 0} :Europe/London jpayne@69: {3600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Africa/Kinshasa jpayne@69: {3600 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :CET jpayne@69: {7200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Africa/Harare jpayne@69: {7200 0 3600 0 9 4 5 23 59 59 0 0 4 4 5 23 59 59 0} jpayne@69: :Africa/Cairo jpayne@69: {7200 0 3600 0 10 0 5 4 0 0 0 0 3 0 5 3 0 0 0} :Europe/Helsinki jpayne@69: {7200 0 3600 0 9 0 3 2 0 0 0 0 3 5 5 2 0 0 0} :Asia/Jerusalem jpayne@69: {7200 0 3600 0 9 0 5 1 0 0 0 0 3 0 5 0 0 0 0} :Europe/Bucharest jpayne@69: {7200 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Europe/Athens jpayne@69: {7200 0 3600 0 9 5 5 1 0 0 0 0 3 4 5 0 0 0 0} :Asia/Amman jpayne@69: {7200 0 3600 0 10 6 5 23 59 59 999 0 3 0 5 0 0 0 0} jpayne@69: :Asia/Beirut jpayne@69: {7200 0 -3600 0 4 0 1 2 0 0 0 0 9 0 1 2 0 0 0} :Africa/Windhoek jpayne@69: {10800 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Riyadh jpayne@69: {10800 0 3600 0 10 0 1 4 0 0 0 0 4 0 1 3 0 0 0} :Asia/Baghdad jpayne@69: {10800 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Europe/Moscow jpayne@69: {12600 0 3600 0 9 2 4 2 0 0 0 0 3 0 1 2 0 0 0} :Asia/Tehran jpayne@69: {14400 0 3600 0 10 0 5 5 0 0 0 0 3 0 5 4 0 0 0} :Asia/Baku jpayne@69: {14400 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Muscat jpayne@69: {14400 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Tbilisi jpayne@69: {16200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Kabul jpayne@69: {18000 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Karachi jpayne@69: {18000 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Yekaterinburg jpayne@69: {19800 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Calcutta jpayne@69: {20700 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Katmandu jpayne@69: {21600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Dhaka jpayne@69: {21600 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Novosibirsk jpayne@69: {23400 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Rangoon jpayne@69: {25200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Bangkok jpayne@69: {25200 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Krasnoyarsk jpayne@69: {28800 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Chongqing jpayne@69: {28800 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Irkutsk jpayne@69: {32400 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Tokyo jpayne@69: {32400 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Yakutsk jpayne@69: {34200 0 3600 0 3 0 5 3 0 0 0 0 10 0 5 2 0 0 0} :Australia/Adelaide jpayne@69: {34200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Australia/Darwin jpayne@69: {36000 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Australia/Brisbane jpayne@69: {36000 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Vladivostok jpayne@69: {36000 0 3600 0 3 0 5 3 0 0 0 0 10 0 1 2 0 0 0} :Australia/Hobart jpayne@69: {36000 0 3600 0 3 0 5 3 0 0 0 0 10 0 5 2 0 0 0} :Australia/Sydney jpayne@69: {39600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Noumea jpayne@69: {43200 0 3600 0 3 0 3 3 0 0 0 0 10 0 1 2 0 0 0} :Pacific/Auckland jpayne@69: {43200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Fiji jpayne@69: {46800 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Tongatapu jpayne@69: }] jpayne@69: jpayne@69: # Groups of fields that specify the date, priorities, and code bursts that jpayne@69: # determine Julian Day Number given those groups. The code in [clock jpayne@69: # scan] will choose the highest priority (lowest numbered) set of fields jpayne@69: # that determines the date. jpayne@69: jpayne@69: variable DateParseActions { jpayne@69: jpayne@69: { seconds } 0 {} jpayne@69: jpayne@69: { julianDay } 1 {} jpayne@69: jpayne@69: { era century yearOfCentury month dayOfMonth } 2 { jpayne@69: dict set date year [expr { 100 * [dict get $date century] jpayne@69: + [dict get $date yearOfCentury] }] jpayne@69: set date [GetJulianDayFromEraYearMonthDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: { era century yearOfCentury dayOfYear } 2 { jpayne@69: dict set date year [expr { 100 * [dict get $date century] jpayne@69: + [dict get $date yearOfCentury] }] jpayne@69: set date [GetJulianDayFromEraYearDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: jpayne@69: { century yearOfCentury month dayOfMonth } 3 { jpayne@69: dict set date era CE jpayne@69: dict set date year [expr { 100 * [dict get $date century] jpayne@69: + [dict get $date yearOfCentury] }] jpayne@69: set date [GetJulianDayFromEraYearMonthDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: { century yearOfCentury dayOfYear } 3 { jpayne@69: dict set date era CE jpayne@69: dict set date year [expr { 100 * [dict get $date century] jpayne@69: + [dict get $date yearOfCentury] }] jpayne@69: set date [GetJulianDayFromEraYearDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: { iso8601Century iso8601YearOfCentury iso8601Week dayOfWeek } 3 { jpayne@69: dict set date era CE jpayne@69: dict set date iso8601Year \ jpayne@69: [expr { 100 * [dict get $date iso8601Century] jpayne@69: + [dict get $date iso8601YearOfCentury] }] jpayne@69: set date [GetJulianDayFromEraYearWeekDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: jpayne@69: { yearOfCentury month dayOfMonth } 4 { jpayne@69: set date [InterpretTwoDigitYear $date[set date {}] $baseTime] jpayne@69: dict set date era CE jpayne@69: set date [GetJulianDayFromEraYearMonthDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: { yearOfCentury dayOfYear } 4 { jpayne@69: set date [InterpretTwoDigitYear $date[set date {}] $baseTime] jpayne@69: dict set date era CE jpayne@69: set date [GetJulianDayFromEraYearDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: { iso8601YearOfCentury iso8601Week dayOfWeek } 4 { jpayne@69: set date [InterpretTwoDigitYear \ jpayne@69: $date[set date {}] $baseTime \ jpayne@69: iso8601YearOfCentury iso8601Year] jpayne@69: dict set date era CE jpayne@69: set date [GetJulianDayFromEraYearWeekDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: jpayne@69: { month dayOfMonth } 5 { jpayne@69: set date [AssignBaseYear $date[set date {}] \ jpayne@69: $baseTime $timeZone $changeover] jpayne@69: set date [GetJulianDayFromEraYearMonthDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: { dayOfYear } 5 { jpayne@69: set date [AssignBaseYear $date[set date {}] \ jpayne@69: $baseTime $timeZone $changeover] jpayne@69: set date [GetJulianDayFromEraYearDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: { iso8601Week dayOfWeek } 5 { jpayne@69: set date [AssignBaseIso8601Year $date[set date {}] \ jpayne@69: $baseTime $timeZone $changeover] jpayne@69: set date [GetJulianDayFromEraYearWeekDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: jpayne@69: { dayOfMonth } 6 { jpayne@69: set date [AssignBaseMonth $date[set date {}] \ jpayne@69: $baseTime $timeZone $changeover] jpayne@69: set date [GetJulianDayFromEraYearMonthDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: jpayne@69: { dayOfWeek } 7 { jpayne@69: set date [AssignBaseWeek $date[set date {}] \ jpayne@69: $baseTime $timeZone $changeover] jpayne@69: set date [GetJulianDayFromEraYearWeekDay $date[set date {}] \ jpayne@69: $changeover] jpayne@69: } jpayne@69: jpayne@69: {} 8 { jpayne@69: set date [AssignBaseJulianDay $date[set date {}] \ jpayne@69: $baseTime $timeZone $changeover] jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Groups of fields that specify time of day, priorities, and code that jpayne@69: # processes them jpayne@69: jpayne@69: variable TimeParseActions { jpayne@69: jpayne@69: seconds 1 {} jpayne@69: jpayne@69: { hourAMPM minute second amPmIndicator } 2 { jpayne@69: dict set date secondOfDay [InterpretHMSP $date] jpayne@69: } jpayne@69: { hour minute second } 2 { jpayne@69: dict set date secondOfDay [InterpretHMS $date] jpayne@69: } jpayne@69: jpayne@69: { hourAMPM minute amPmIndicator } 3 { jpayne@69: dict set date second 0 jpayne@69: dict set date secondOfDay [InterpretHMSP $date] jpayne@69: } jpayne@69: { hour minute } 3 { jpayne@69: dict set date second 0 jpayne@69: dict set date secondOfDay [InterpretHMS $date] jpayne@69: } jpayne@69: jpayne@69: { hourAMPM amPmIndicator } 4 { jpayne@69: dict set date minute 0 jpayne@69: dict set date second 0 jpayne@69: dict set date secondOfDay [InterpretHMSP $date] jpayne@69: } jpayne@69: { hour } 4 { jpayne@69: dict set date minute 0 jpayne@69: dict set date second 0 jpayne@69: dict set date secondOfDay [InterpretHMS $date] jpayne@69: } jpayne@69: jpayne@69: { } 5 { jpayne@69: dict set date secondOfDay 0 jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Legacy time zones, used primarily for parsing RFC822 dates. jpayne@69: jpayne@69: variable LegacyTimeZone [dict create \ jpayne@69: gmt +0000 \ jpayne@69: ut +0000 \ jpayne@69: utc +0000 \ jpayne@69: bst +0100 \ jpayne@69: wet +0000 \ jpayne@69: wat -0100 \ jpayne@69: at -0200 \ jpayne@69: nft -0330 \ jpayne@69: nst -0330 \ jpayne@69: ndt -0230 \ jpayne@69: ast -0400 \ jpayne@69: adt -0300 \ jpayne@69: est -0500 \ jpayne@69: edt -0400 \ jpayne@69: cst -0600 \ jpayne@69: cdt -0500 \ jpayne@69: mst -0700 \ jpayne@69: mdt -0600 \ jpayne@69: pst -0800 \ jpayne@69: pdt -0700 \ jpayne@69: yst -0900 \ jpayne@69: ydt -0800 \ jpayne@69: hst -1000 \ jpayne@69: hdt -0900 \ jpayne@69: cat -1000 \ jpayne@69: ahst -1000 \ jpayne@69: nt -1100 \ jpayne@69: idlw -1200 \ jpayne@69: cet +0100 \ jpayne@69: cest +0200 \ jpayne@69: met +0100 \ jpayne@69: mewt +0100 \ jpayne@69: mest +0200 \ jpayne@69: swt +0100 \ jpayne@69: sst +0200 \ jpayne@69: fwt +0100 \ jpayne@69: fst +0200 \ jpayne@69: eet +0200 \ jpayne@69: eest +0300 \ jpayne@69: bt +0300 \ jpayne@69: it +0330 \ jpayne@69: zp4 +0400 \ jpayne@69: zp5 +0500 \ jpayne@69: ist +0530 \ jpayne@69: zp6 +0600 \ jpayne@69: wast +0700 \ jpayne@69: wadt +0800 \ jpayne@69: jt +0730 \ jpayne@69: cct +0800 \ jpayne@69: jst +0900 \ jpayne@69: kst +0900 \ jpayne@69: cast +0930 \ jpayne@69: jdt +1000 \ jpayne@69: kdt +1000 \ jpayne@69: cadt +1030 \ jpayne@69: east +1000 \ jpayne@69: eadt +1030 \ jpayne@69: gst +1000 \ jpayne@69: nzt +1200 \ jpayne@69: nzst +1200 \ jpayne@69: nzdt +1300 \ jpayne@69: idle +1200 \ jpayne@69: a +0100 \ jpayne@69: b +0200 \ jpayne@69: c +0300 \ jpayne@69: d +0400 \ jpayne@69: e +0500 \ jpayne@69: f +0600 \ jpayne@69: g +0700 \ jpayne@69: h +0800 \ jpayne@69: i +0900 \ jpayne@69: k +1000 \ jpayne@69: l +1100 \ jpayne@69: m +1200 \ jpayne@69: n -0100 \ jpayne@69: o -0200 \ jpayne@69: p -0300 \ jpayne@69: q -0400 \ jpayne@69: r -0500 \ jpayne@69: s -0600 \ jpayne@69: t -0700 \ jpayne@69: u -0800 \ jpayne@69: v -0900 \ jpayne@69: w -1000 \ jpayne@69: x -1100 \ jpayne@69: y -1200 \ jpayne@69: z +0000 \ jpayne@69: ] jpayne@69: jpayne@69: # Caches jpayne@69: jpayne@69: variable LocaleNumeralCache {}; # Dictionary whose keys are locale jpayne@69: # names and whose values are pairs jpayne@69: # comprising regexes matching numerals jpayne@69: # in the given locales and dictionaries jpayne@69: # mapping the numerals to their numeric jpayne@69: # values. jpayne@69: # variable CachedSystemTimeZone; # If 'CachedSystemTimeZone' exists, jpayne@69: # it contains the value of the jpayne@69: # system time zone, as determined from jpayne@69: # the environment. jpayne@69: variable TimeZoneBad {}; # Dictionary whose keys are time zone jpayne@69: # names and whose values are 1 if jpayne@69: # the time zone is unknown and 0 jpayne@69: # if it is known. jpayne@69: variable TZData; # Array whose keys are time zone names jpayne@69: # and whose values are lists of quads jpayne@69: # comprising start time, UTC offset, jpayne@69: # Daylight Saving Time indicator, and jpayne@69: # time zone abbreviation. jpayne@69: variable FormatProc; # Array mapping format group jpayne@69: # and locale to the name of a procedure jpayne@69: # that renders the given format jpayne@69: } jpayne@69: ::tcl::clock::Initialize jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # clock format -- jpayne@69: # jpayne@69: # Formats a count of seconds since the Posix Epoch as a time of day. jpayne@69: # jpayne@69: # The 'clock format' command formats times of day for output. Refer to the jpayne@69: # user documentation to see what it does. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::format { args } { jpayne@69: jpayne@69: variable FormatProc jpayne@69: variable TZData jpayne@69: jpayne@69: lassign [ParseFormatArgs {*}$args] format locale timezone jpayne@69: set locale [string tolower $locale] jpayne@69: set clockval [lindex $args 0] jpayne@69: jpayne@69: # Get the data for time changes in the given zone jpayne@69: jpayne@69: if {$timezone eq ""} { jpayne@69: set timezone [GetSystemTimeZone] jpayne@69: } jpayne@69: if {![info exists TZData($timezone)]} { jpayne@69: if {[catch {SetupTimeZone $timezone} retval opts]} { jpayne@69: dict unset opts -errorinfo jpayne@69: return -options $opts $retval jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Build a procedure to format the result. Cache the built procedure's name jpayne@69: # in the 'FormatProc' array to avoid losing its internal representation, jpayne@69: # which contains the name resolution. jpayne@69: jpayne@69: set procName formatproc'$format'$locale jpayne@69: set procName [namespace current]::[string map {: {\:} \\ {\\}} $procName] jpayne@69: if {[info exists FormatProc($procName)]} { jpayne@69: set procName $FormatProc($procName) jpayne@69: } else { jpayne@69: set FormatProc($procName) \ jpayne@69: [ParseClockFormatFormat $procName $format $locale] jpayne@69: } jpayne@69: jpayne@69: return [$procName $clockval $timezone] jpayne@69: jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ParseClockFormatFormat -- jpayne@69: # jpayne@69: # Builds and caches a procedure that formats a time value. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # format -- Format string to use jpayne@69: # locale -- Locale in which the format string is to be interpreted jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the name of the newly-built procedure. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::ParseClockFormatFormat {procName format locale} { jpayne@69: jpayne@69: if {[namespace which $procName] ne {}} { jpayne@69: return $procName jpayne@69: } jpayne@69: jpayne@69: # Map away the locale-dependent composite format groups jpayne@69: jpayne@69: EnterLocale $locale jpayne@69: jpayne@69: # Change locale if a fresh locale has been given on the command line. jpayne@69: jpayne@69: try { jpayne@69: return [ParseClockFormatFormat2 $format $locale $procName] jpayne@69: } trap CLOCK {result opts} { jpayne@69: dict unset opts -errorinfo jpayne@69: return -options $opts $result jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: proc ::tcl::clock::ParseClockFormatFormat2 {format locale procName} { jpayne@69: set didLocaleEra 0 jpayne@69: set didLocaleNumerals 0 jpayne@69: set preFormatCode \ jpayne@69: [string map [list @GREGORIAN_CHANGE_DATE@ \ jpayne@69: [mc GREGORIAN_CHANGE_DATE]] \ jpayne@69: { jpayne@69: variable TZData jpayne@69: set date [GetDateFields $clockval \ jpayne@69: $TZData($timezone) \ jpayne@69: @GREGORIAN_CHANGE_DATE@] jpayne@69: }] jpayne@69: set formatString {} jpayne@69: set substituents {} jpayne@69: set state {} jpayne@69: jpayne@69: set format [LocalizeFormat $locale $format] jpayne@69: jpayne@69: foreach char [split $format {}] { jpayne@69: switch -exact -- $state { jpayne@69: {} { jpayne@69: if { [string equal % $char] } { jpayne@69: set state percent jpayne@69: } else { jpayne@69: append formatString $char jpayne@69: } jpayne@69: } jpayne@69: percent { # Character following a '%' character jpayne@69: set state {} jpayne@69: switch -exact -- $char { jpayne@69: % { # A literal character, '%' jpayne@69: append formatString %% jpayne@69: } jpayne@69: a { # Day of week, abbreviated jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: [string map \ jpayne@69: [list @DAYS_OF_WEEK_ABBREV@ \ jpayne@69: [list [mc DAYS_OF_WEEK_ABBREV]]] \ jpayne@69: { [lindex @DAYS_OF_WEEK_ABBREV@ \ jpayne@69: [expr {[dict get $date dayOfWeek] \ jpayne@69: % 7}]]}] jpayne@69: } jpayne@69: A { # Day of week, spelt out. jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: [string map \ jpayne@69: [list @DAYS_OF_WEEK_FULL@ \ jpayne@69: [list [mc DAYS_OF_WEEK_FULL]]] \ jpayne@69: { [lindex @DAYS_OF_WEEK_FULL@ \ jpayne@69: [expr {[dict get $date dayOfWeek] \ jpayne@69: % 7}]]}] jpayne@69: } jpayne@69: b - h { # Name of month, abbreviated. jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: [string map \ jpayne@69: [list @MONTHS_ABBREV@ \ jpayne@69: [list [mc MONTHS_ABBREV]]] \ jpayne@69: { [lindex @MONTHS_ABBREV@ \ jpayne@69: [expr {[dict get $date month]-1}]]}] jpayne@69: } jpayne@69: B { # Name of month, spelt out jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: [string map \ jpayne@69: [list @MONTHS_FULL@ \ jpayne@69: [list [mc MONTHS_FULL]]] \ jpayne@69: { [lindex @MONTHS_FULL@ \ jpayne@69: [expr {[dict get $date month]-1}]]}] jpayne@69: } jpayne@69: C { # Century number jpayne@69: append formatString %02d jpayne@69: append substituents \ jpayne@69: { [expr {[dict get $date year] / 100}]} jpayne@69: } jpayne@69: d { # Day of month, with leading zero jpayne@69: append formatString %02d jpayne@69: append substituents { [dict get $date dayOfMonth]} jpayne@69: } jpayne@69: e { # Day of month, without leading zero jpayne@69: append formatString %2d jpayne@69: append substituents { [dict get $date dayOfMonth]} jpayne@69: } jpayne@69: E { # Format group in a locale-dependent jpayne@69: # alternative era jpayne@69: set state percentE jpayne@69: if {!$didLocaleEra} { jpayne@69: append preFormatCode \ jpayne@69: [string map \ jpayne@69: [list @LOCALE_ERAS@ \ jpayne@69: [list [mc LOCALE_ERAS]]] \ jpayne@69: { jpayne@69: set date [GetLocaleEra \ jpayne@69: $date[set date {}] \ jpayne@69: @LOCALE_ERAS@]}] \n jpayne@69: set didLocaleEra 1 jpayne@69: } jpayne@69: if {!$didLocaleNumerals} { jpayne@69: append preFormatCode \ jpayne@69: [list set localeNumerals \ jpayne@69: [mc LOCALE_NUMERALS]] \n jpayne@69: set didLocaleNumerals 1 jpayne@69: } jpayne@69: } jpayne@69: g { # Two-digit year relative to ISO8601 jpayne@69: # week number jpayne@69: append formatString %02d jpayne@69: append substituents \ jpayne@69: { [expr { [dict get $date iso8601Year] % 100 }]} jpayne@69: } jpayne@69: G { # Four-digit year relative to ISO8601 jpayne@69: # week number jpayne@69: append formatString %02d jpayne@69: append substituents { [dict get $date iso8601Year]} jpayne@69: } jpayne@69: H { # Hour in the 24-hour day, leading zero jpayne@69: append formatString %02d jpayne@69: append substituents \ jpayne@69: { [expr { [dict get $date localSeconds] \ jpayne@69: / 3600 % 24}]} jpayne@69: } jpayne@69: I { # Hour AM/PM, with leading zero jpayne@69: append formatString %02d jpayne@69: append substituents \ jpayne@69: { [expr { ( ( ( [dict get $date localSeconds] \ jpayne@69: % 86400 ) \ jpayne@69: + 86400 \ jpayne@69: - 3600 ) \ jpayne@69: / 3600 ) \ jpayne@69: % 12 + 1 }] } jpayne@69: } jpayne@69: j { # Day of year (001-366) jpayne@69: append formatString %03d jpayne@69: append substituents { [dict get $date dayOfYear]} jpayne@69: } jpayne@69: J { # Julian Day Number jpayne@69: append formatString %07ld jpayne@69: append substituents { [dict get $date julianDay]} jpayne@69: } jpayne@69: k { # Hour (0-23), no leading zero jpayne@69: append formatString %2d jpayne@69: append substituents \ jpayne@69: { [expr { [dict get $date localSeconds] jpayne@69: / 3600 jpayne@69: % 24 }]} jpayne@69: } jpayne@69: l { # Hour (12-11), no leading zero jpayne@69: append formatString %2d jpayne@69: append substituents \ jpayne@69: { [expr { ( ( ( [dict get $date localSeconds] jpayne@69: % 86400 ) jpayne@69: + 86400 jpayne@69: - 3600 ) jpayne@69: / 3600 ) jpayne@69: % 12 + 1 }]} jpayne@69: } jpayne@69: m { # Month number, leading zero jpayne@69: append formatString %02d jpayne@69: append substituents { [dict get $date month]} jpayne@69: } jpayne@69: M { # Minute of the hour, leading zero jpayne@69: append formatString %02d jpayne@69: append substituents \ jpayne@69: { [expr { [dict get $date localSeconds] jpayne@69: / 60 jpayne@69: % 60 }]} jpayne@69: } jpayne@69: n { # A literal newline jpayne@69: append formatString \n jpayne@69: } jpayne@69: N { # Month number, no leading zero jpayne@69: append formatString %2d jpayne@69: append substituents { [dict get $date month]} jpayne@69: } jpayne@69: O { # A format group in the locale's jpayne@69: # alternative numerals jpayne@69: set state percentO jpayne@69: if {!$didLocaleNumerals} { jpayne@69: append preFormatCode \ jpayne@69: [list set localeNumerals \ jpayne@69: [mc LOCALE_NUMERALS]] \n jpayne@69: set didLocaleNumerals 1 jpayne@69: } jpayne@69: } jpayne@69: p { # Localized 'AM' or 'PM' indicator jpayne@69: # converted to uppercase jpayne@69: append formatString %s jpayne@69: append preFormatCode \ jpayne@69: [list set AM [string toupper [mc AM]]] \n \ jpayne@69: [list set PM [string toupper [mc PM]]] \n jpayne@69: append substituents \ jpayne@69: { [expr {(([dict get $date localSeconds] jpayne@69: % 86400) < 43200) ? jpayne@69: $AM : $PM}]} jpayne@69: } jpayne@69: P { # Localized 'AM' or 'PM' indicator jpayne@69: append formatString %s jpayne@69: append preFormatCode \ jpayne@69: [list set am [mc AM]] \n \ jpayne@69: [list set pm [mc PM]] \n jpayne@69: append substituents \ jpayne@69: { [expr {(([dict get $date localSeconds] jpayne@69: % 86400) < 43200) ? jpayne@69: $am : $pm}]} jpayne@69: jpayne@69: } jpayne@69: Q { # Hi, Jeff! jpayne@69: append formatString %s jpayne@69: append substituents { [FormatStarDate $date]} jpayne@69: } jpayne@69: s { # Seconds from the Posix Epoch jpayne@69: append formatString %s jpayne@69: append substituents { [dict get $date seconds]} jpayne@69: } jpayne@69: S { # Second of the minute, with jpayne@69: # leading zero jpayne@69: append formatString %02d jpayne@69: append substituents \ jpayne@69: { [expr { [dict get $date localSeconds] jpayne@69: % 60 }]} jpayne@69: } jpayne@69: t { # A literal tab character jpayne@69: append formatString \t jpayne@69: } jpayne@69: u { # Day of the week (1-Monday, 7-Sunday) jpayne@69: append formatString %1d jpayne@69: append substituents { [dict get $date dayOfWeek]} jpayne@69: } jpayne@69: U { # Week of the year (00-53). The jpayne@69: # first Sunday of the year is the jpayne@69: # first day of week 01 jpayne@69: append formatString %02d jpayne@69: append preFormatCode { jpayne@69: set dow [dict get $date dayOfWeek] jpayne@69: if { $dow == 7 } { jpayne@69: set dow 0 jpayne@69: } jpayne@69: incr dow jpayne@69: set UweekNumber \ jpayne@69: [expr { ( [dict get $date dayOfYear] jpayne@69: - $dow + 7 ) jpayne@69: / 7 }] jpayne@69: } jpayne@69: append substituents { $UweekNumber} jpayne@69: } jpayne@69: V { # The ISO8601 week number jpayne@69: append formatString %02d jpayne@69: append substituents { [dict get $date iso8601Week]} jpayne@69: } jpayne@69: w { # Day of the week (0-Sunday, jpayne@69: # 6-Saturday) jpayne@69: append formatString %1d jpayne@69: append substituents \ jpayne@69: { [expr { [dict get $date dayOfWeek] % 7 }]} jpayne@69: } jpayne@69: W { # Week of the year (00-53). The first jpayne@69: # Monday of the year is the first day jpayne@69: # of week 01. jpayne@69: append preFormatCode { jpayne@69: set WweekNumber \ jpayne@69: [expr { ( [dict get $date dayOfYear] jpayne@69: - [dict get $date dayOfWeek] jpayne@69: + 7 ) jpayne@69: / 7 }] jpayne@69: } jpayne@69: append formatString %02d jpayne@69: append substituents { $WweekNumber} jpayne@69: } jpayne@69: y { # The two-digit year of the century jpayne@69: append formatString %02d jpayne@69: append substituents \ jpayne@69: { [expr { [dict get $date year] % 100 }]} jpayne@69: } jpayne@69: Y { # The four-digit year jpayne@69: append formatString %04d jpayne@69: append substituents { [dict get $date year]} jpayne@69: } jpayne@69: z { # The time zone as hours and minutes jpayne@69: # east (+) or west (-) of Greenwich jpayne@69: append formatString %s jpayne@69: append substituents { [FormatNumericTimeZone \ jpayne@69: [dict get $date tzOffset]]} jpayne@69: } jpayne@69: Z { # The name of the time zone jpayne@69: append formatString %s jpayne@69: append substituents { [dict get $date tzName]} jpayne@69: } jpayne@69: % { # A literal percent character jpayne@69: append formatString %% jpayne@69: } jpayne@69: default { # An unknown escape sequence jpayne@69: append formatString %% $char jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: percentE { # Character following %E jpayne@69: set state {} jpayne@69: switch -exact -- $char { jpayne@69: E { jpayne@69: append formatString %s jpayne@69: append substituents { } \ jpayne@69: [string map \ jpayne@69: [list @BCE@ [list [mc BCE]] \ jpayne@69: @CE@ [list [mc CE]]] \ jpayne@69: {[dict get {BCE @BCE@ CE @CE@} \ jpayne@69: [dict get $date era]]}] jpayne@69: } jpayne@69: C { # Locale-dependent era jpayne@69: append formatString %s jpayne@69: append substituents { [dict get $date localeEra]} jpayne@69: } jpayne@69: y { # Locale-dependent year of the era jpayne@69: append preFormatCode { jpayne@69: set y [dict get $date localeYear] jpayne@69: if { $y >= 0 && $y < 100 } { jpayne@69: set Eyear [lindex $localeNumerals $y] jpayne@69: } else { jpayne@69: set Eyear $y jpayne@69: } jpayne@69: } jpayne@69: append formatString %s jpayne@69: append substituents { $Eyear} jpayne@69: } jpayne@69: default { # Unknown %E format group jpayne@69: append formatString %%E $char jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: percentO { # Character following %O jpayne@69: set state {} jpayne@69: switch -exact -- $char { jpayne@69: d - e { # Day of the month in alternative jpayne@69: # numerals jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: { [lindex $localeNumerals \ jpayne@69: [dict get $date dayOfMonth]]} jpayne@69: } jpayne@69: H - k { # Hour of the day in alternative jpayne@69: # numerals jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: { [lindex $localeNumerals \ jpayne@69: [expr { [dict get $date localSeconds] jpayne@69: / 3600 jpayne@69: % 24 }]]} jpayne@69: } jpayne@69: I - l { # Hour (12-11) AM/PM in alternative jpayne@69: # numerals jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: { [lindex $localeNumerals \ jpayne@69: [expr { ( ( ( [dict get $date localSeconds] jpayne@69: % 86400 ) jpayne@69: + 86400 jpayne@69: - 3600 ) jpayne@69: / 3600 ) jpayne@69: % 12 + 1 }]]} jpayne@69: } jpayne@69: m { # Month number in alternative numerals jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: { [lindex $localeNumerals [dict get $date month]]} jpayne@69: } jpayne@69: M { # Minute of the hour in alternative jpayne@69: # numerals jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: { [lindex $localeNumerals \ jpayne@69: [expr { [dict get $date localSeconds] jpayne@69: / 60 jpayne@69: % 60 }]]} jpayne@69: } jpayne@69: S { # Second of the minute in alternative jpayne@69: # numerals jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: { [lindex $localeNumerals \ jpayne@69: [expr { [dict get $date localSeconds] jpayne@69: % 60 }]]} jpayne@69: } jpayne@69: u { # Day of the week (Monday=1,Sunday=7) jpayne@69: # in alternative numerals jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: { [lindex $localeNumerals \ jpayne@69: [dict get $date dayOfWeek]]} jpayne@69: } jpayne@69: w { # Day of the week (Sunday=0,Saturday=6) jpayne@69: # in alternative numerals jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: { [lindex $localeNumerals \ jpayne@69: [expr { [dict get $date dayOfWeek] % 7 }]]} jpayne@69: } jpayne@69: y { # Year of the century in alternative jpayne@69: # numerals jpayne@69: append formatString %s jpayne@69: append substituents \ jpayne@69: { [lindex $localeNumerals \ jpayne@69: [expr { [dict get $date year] % 100 }]]} jpayne@69: } jpayne@69: default { # Unknown format group jpayne@69: append formatString %%O $char jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Clean up any improperly terminated groups jpayne@69: jpayne@69: switch -exact -- $state { jpayne@69: percent { jpayne@69: append formatString %% jpayne@69: } jpayne@69: percentE { jpayne@69: append retval %%E jpayne@69: } jpayne@69: percentO { jpayne@69: append retval %%O jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: proc $procName {clockval timezone} " jpayne@69: $preFormatCode jpayne@69: return \[::format [list $formatString] $substituents\] jpayne@69: " jpayne@69: jpayne@69: # puts [list $procName [info args $procName] [info body $procName]] jpayne@69: jpayne@69: return $procName jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # clock scan -- jpayne@69: # jpayne@69: # Inputs a count of seconds since the Posix Epoch as a time of day. jpayne@69: # jpayne@69: # The 'clock format' command scans times of day on input. Refer to the user jpayne@69: # documentation to see what it does. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::scan { args } { jpayne@69: jpayne@69: set format {} jpayne@69: jpayne@69: # Check the count of args jpayne@69: jpayne@69: if { [llength $args] < 1 || [llength $args] % 2 != 1 } { jpayne@69: set cmdName "clock scan" jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK wrongNumArgs] \ jpayne@69: "wrong \# args: should be\ jpayne@69: \"$cmdName string\ jpayne@69: ?-base seconds?\ jpayne@69: ?-format string? ?-gmt boolean?\ jpayne@69: ?-locale LOCALE? ?-timezone ZONE?\"" jpayne@69: } jpayne@69: jpayne@69: # Set defaults jpayne@69: jpayne@69: set base [clock seconds] jpayne@69: set string [lindex $args 0] jpayne@69: set format {} jpayne@69: set gmt 0 jpayne@69: set locale c jpayne@69: set timezone [GetSystemTimeZone] jpayne@69: jpayne@69: # Pick up command line options. jpayne@69: jpayne@69: foreach { flag value } [lreplace $args 0 0] { jpayne@69: set saw($flag) {} jpayne@69: switch -exact -- $flag { jpayne@69: -b - -ba - -bas - -base { jpayne@69: set base $value jpayne@69: } jpayne@69: -f - -fo - -for - -form - -forma - -format { jpayne@69: set format $value jpayne@69: } jpayne@69: -g - -gm - -gmt { jpayne@69: set gmt $value jpayne@69: } jpayne@69: -l - -lo - -loc - -loca - -local - -locale { jpayne@69: set locale [string tolower $value] jpayne@69: } jpayne@69: -t - -ti - -tim - -time - -timez - -timezo - -timezon - -timezone { jpayne@69: set timezone $value jpayne@69: } jpayne@69: default { jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK badOption $flag] \ jpayne@69: "bad option \"$flag\",\ jpayne@69: must be -base, -format, -gmt, -locale or -timezone" jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Check options for validity jpayne@69: jpayne@69: if { [info exists saw(-gmt)] && [info exists saw(-timezone)] } { jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK gmtWithTimezone] \ jpayne@69: "cannot use -gmt and -timezone in same call" jpayne@69: } jpayne@69: if { [catch { expr { wide($base) } } result] } { jpayne@69: return -code error "expected integer but got \"$base\"" jpayne@69: } jpayne@69: if { ![string is boolean -strict $gmt] } { jpayne@69: return -code error "expected boolean value but got \"$gmt\"" jpayne@69: } elseif { $gmt } { jpayne@69: set timezone :GMT jpayne@69: } jpayne@69: jpayne@69: if { ![info exists saw(-format)] } { jpayne@69: # Perhaps someday we'll localize the legacy code. Right now, it's not jpayne@69: # localized. jpayne@69: if { [info exists saw(-locale)] } { jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK flagWithLegacyFormat] \ jpayne@69: "legacy \[clock scan\] does not support -locale" jpayne@69: jpayne@69: } jpayne@69: return [FreeScan $string $base $timezone $locale] jpayne@69: } jpayne@69: jpayne@69: # Change locale if a fresh locale has been given on the command line. jpayne@69: jpayne@69: EnterLocale $locale jpayne@69: jpayne@69: try { jpayne@69: # Map away the locale-dependent composite format groups jpayne@69: jpayne@69: set scanner [ParseClockScanFormat $format $locale] jpayne@69: return [$scanner $string $base $timezone] jpayne@69: } trap CLOCK {result opts} { jpayne@69: # Conceal location of generation of expected errors jpayne@69: dict unset opts -errorinfo jpayne@69: return -options $opts $result jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # FreeScan -- jpayne@69: # jpayne@69: # Scans a time in free format jpayne@69: # jpayne@69: # Parameters: jpayne@69: # string - String containing the time to scan jpayne@69: # base - Base time, expressed in seconds from the Epoch jpayne@69: # timezone - Default time zone in which the time will be expressed jpayne@69: # locale - (Unused) Name of the locale where the time will be scanned. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the date and time extracted from the string in seconds from jpayne@69: # the epoch jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::FreeScan { string base timezone locale } { jpayne@69: jpayne@69: variable TZData jpayne@69: jpayne@69: # Get the data for time changes in the given zone jpayne@69: jpayne@69: try { jpayne@69: SetupTimeZone $timezone jpayne@69: } on error {retval opts} { jpayne@69: dict unset opts -errorinfo jpayne@69: return -options $opts $retval jpayne@69: } jpayne@69: jpayne@69: # Extract year, month and day from the base time for the parser to use as jpayne@69: # defaults jpayne@69: jpayne@69: set date [GetDateFields $base $TZData($timezone) 2361222] jpayne@69: dict set date secondOfDay [expr { jpayne@69: [dict get $date localSeconds] % 86400 jpayne@69: }] jpayne@69: jpayne@69: # Parse the date. The parser will return a list comprising date, time, jpayne@69: # time zone, relative month/day/seconds, relative weekday, ordinal month. jpayne@69: jpayne@69: try { jpayne@69: set scanned [Oldscan $string \ jpayne@69: [dict get $date year] \ jpayne@69: [dict get $date month] \ jpayne@69: [dict get $date dayOfMonth]] jpayne@69: lassign $scanned \ jpayne@69: parseDate parseTime parseZone parseRel \ jpayne@69: parseWeekday parseOrdinalMonth jpayne@69: } on error message { jpayne@69: return -code error \ jpayne@69: "unable to convert date-time string \"$string\": $message" jpayne@69: } jpayne@69: jpayne@69: # If the caller supplied a date in the string, update the 'date' dict with jpayne@69: # the value. If the caller didn't specify a time with the date, default to jpayne@69: # midnight. jpayne@69: jpayne@69: if { [llength $parseDate] > 0 } { jpayne@69: lassign $parseDate y m d jpayne@69: if { $y < 100 } { jpayne@69: if { $y >= 39 } { jpayne@69: incr y 1900 jpayne@69: } else { jpayne@69: incr y 2000 jpayne@69: } jpayne@69: } jpayne@69: dict set date era CE jpayne@69: dict set date year $y jpayne@69: dict set date month $m jpayne@69: dict set date dayOfMonth $d jpayne@69: if { $parseTime eq {} } { jpayne@69: set parseTime 0 jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # If the caller supplied a time zone in the string, it comes back as a jpayne@69: # two-element list; the first element is the number of minutes east of jpayne@69: # Greenwich, and the second is a Daylight Saving Time indicator (1 == yes, jpayne@69: # 0 == no, -1 == unknown). We make it into a time zone indicator of jpayne@69: # +-hhmm. jpayne@69: jpayne@69: if { [llength $parseZone] > 0 } { jpayne@69: lassign $parseZone minEast dstFlag jpayne@69: set timezone [FormatNumericTimeZone \ jpayne@69: [expr { 60 * $minEast + 3600 * $dstFlag }]] jpayne@69: SetupTimeZone $timezone jpayne@69: } jpayne@69: dict set date tzName $timezone jpayne@69: jpayne@69: # Assemble date, time, zone into seconds-from-epoch jpayne@69: jpayne@69: set date [GetJulianDayFromEraYearMonthDay $date[set date {}] 2361222] jpayne@69: if { $parseTime ne {} } { jpayne@69: dict set date secondOfDay $parseTime jpayne@69: } elseif { [llength $parseWeekday] != 0 jpayne@69: || [llength $parseOrdinalMonth] != 0 jpayne@69: || ( [llength $parseRel] != 0 jpayne@69: && ( [lindex $parseRel 0] != 0 jpayne@69: || [lindex $parseRel 1] != 0 ) ) } { jpayne@69: dict set date secondOfDay 0 jpayne@69: } jpayne@69: jpayne@69: dict set date localSeconds [expr { jpayne@69: -210866803200 jpayne@69: + ( 86400 * wide([dict get $date julianDay]) ) jpayne@69: + [dict get $date secondOfDay] jpayne@69: }] jpayne@69: dict set date tzName $timezone jpayne@69: set date [ConvertLocalToUTC $date[set date {}] $TZData($timezone) 2361222] jpayne@69: set seconds [dict get $date seconds] jpayne@69: jpayne@69: # Do relative times jpayne@69: jpayne@69: if { [llength $parseRel] > 0 } { jpayne@69: lassign $parseRel relMonth relDay relSecond jpayne@69: set seconds [add $seconds \ jpayne@69: $relMonth months $relDay days $relSecond seconds \ jpayne@69: -timezone $timezone -locale $locale] jpayne@69: } jpayne@69: jpayne@69: # Do relative weekday jpayne@69: jpayne@69: if { [llength $parseWeekday] > 0 } { jpayne@69: lassign $parseWeekday dayOrdinal dayOfWeek jpayne@69: set date2 [GetDateFields $seconds $TZData($timezone) 2361222] jpayne@69: dict set date2 era CE jpayne@69: set jdwkday [WeekdayOnOrBefore $dayOfWeek [expr { jpayne@69: [dict get $date2 julianDay] + 6 jpayne@69: }]] jpayne@69: incr jdwkday [expr { 7 * $dayOrdinal }] jpayne@69: if { $dayOrdinal > 0 } { jpayne@69: incr jdwkday -7 jpayne@69: } jpayne@69: dict set date2 secondOfDay \ jpayne@69: [expr { [dict get $date2 localSeconds] % 86400 }] jpayne@69: dict set date2 julianDay $jdwkday jpayne@69: dict set date2 localSeconds [expr { jpayne@69: -210866803200 jpayne@69: + ( 86400 * wide([dict get $date2 julianDay]) ) jpayne@69: + [dict get $date secondOfDay] jpayne@69: }] jpayne@69: dict set date2 tzName $timezone jpayne@69: set date2 [ConvertLocalToUTC $date2[set date2 {}] $TZData($timezone) \ jpayne@69: 2361222] jpayne@69: set seconds [dict get $date2 seconds] jpayne@69: jpayne@69: } jpayne@69: jpayne@69: # Do relative month jpayne@69: jpayne@69: if { [llength $parseOrdinalMonth] > 0 } { jpayne@69: lassign $parseOrdinalMonth monthOrdinal monthNumber jpayne@69: if { $monthOrdinal > 0 } { jpayne@69: set monthDiff [expr { $monthNumber - [dict get $date month] }] jpayne@69: if { $monthDiff <= 0 } { jpayne@69: incr monthDiff 12 jpayne@69: } jpayne@69: incr monthOrdinal -1 jpayne@69: } else { jpayne@69: set monthDiff [expr { [dict get $date month] - $monthNumber }] jpayne@69: if { $monthDiff >= 0 } { jpayne@69: incr monthDiff -12 jpayne@69: } jpayne@69: incr monthOrdinal jpayne@69: } jpayne@69: set seconds [add $seconds $monthOrdinal years $monthDiff months \ jpayne@69: -timezone $timezone -locale $locale] jpayne@69: } jpayne@69: jpayne@69: return $seconds jpayne@69: } jpayne@69: jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ParseClockScanFormat -- jpayne@69: # jpayne@69: # Parses a format string given to [clock scan -format] jpayne@69: # jpayne@69: # Parameters: jpayne@69: # formatString - The format being parsed jpayne@69: # locale - The current locale jpayne@69: # jpayne@69: # Results: jpayne@69: # Constructs and returns a procedure that accepts the string being jpayne@69: # scanned, the base time, and the time zone. The procedure will either jpayne@69: # return the scanned time or else throw an error that should be rethrown jpayne@69: # to the caller of [clock scan] jpayne@69: # jpayne@69: # Side effects: jpayne@69: # The given procedure is defined in the ::tcl::clock namespace. Scan jpayne@69: # procedures are not deleted once installed. jpayne@69: # jpayne@69: # Why do we parse dates by defining a procedure to parse them? The reason is jpayne@69: # that by doing so, we have one convenient place to cache all the information: jpayne@69: # the regular expressions that match the patterns (which will be compiled), jpayne@69: # the code that assembles the date information, everything lands in one place. jpayne@69: # In this way, when a given format is reused at run time, all the information jpayne@69: # of how to apply it is available in a single place. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::ParseClockScanFormat {formatString locale} { jpayne@69: # Check whether the format has been parsed previously, and return the jpayne@69: # existing recognizer if it has. jpayne@69: jpayne@69: set procName scanproc'$formatString'$locale jpayne@69: set procName [namespace current]::[string map {: {\:} \\ {\\}} $procName] jpayne@69: if { [namespace which $procName] != {} } { jpayne@69: return $procName jpayne@69: } jpayne@69: jpayne@69: variable DateParseActions jpayne@69: variable TimeParseActions jpayne@69: jpayne@69: # Localize the %x, %X, etc. groups jpayne@69: jpayne@69: set formatString [LocalizeFormat $locale $formatString] jpayne@69: jpayne@69: # Condense whitespace jpayne@69: jpayne@69: regsub -all {[[:space:]]+} $formatString { } formatString jpayne@69: jpayne@69: # Walk through the groups of the format string. In this loop, we jpayne@69: # accumulate: jpayne@69: # - a regular expression that matches the string, jpayne@69: # - the count of capturing brackets in the regexp jpayne@69: # - a set of code that post-processes the fields captured by the regexp, jpayne@69: # - a dictionary whose keys are the names of fields that are present jpayne@69: # in the format string. jpayne@69: jpayne@69: set re {^[[:space:]]*} jpayne@69: set captureCount 0 jpayne@69: set postcode {} jpayne@69: set fieldSet [dict create] jpayne@69: set fieldCount 0 jpayne@69: set postSep {} jpayne@69: set state {} jpayne@69: jpayne@69: foreach c [split $formatString {}] { jpayne@69: switch -exact -- $state { jpayne@69: {} { jpayne@69: if { $c eq "%" } { jpayne@69: set state % jpayne@69: } elseif { $c eq " " } { jpayne@69: append re {[[:space:]]+} jpayne@69: } else { jpayne@69: if { ! [string is alnum $c] } { jpayne@69: append re "\\" jpayne@69: } jpayne@69: append re $c jpayne@69: } jpayne@69: } jpayne@69: % { jpayne@69: set state {} jpayne@69: switch -exact -- $c { jpayne@69: % { jpayne@69: append re % jpayne@69: } jpayne@69: { } { jpayne@69: append re "\[\[:space:\]\]*" jpayne@69: } jpayne@69: a - A { # Day of week, in words jpayne@69: set l {} jpayne@69: foreach \ jpayne@69: i {7 1 2 3 4 5 6} \ jpayne@69: abr [mc DAYS_OF_WEEK_ABBREV] \ jpayne@69: full [mc DAYS_OF_WEEK_FULL] { jpayne@69: dict set l [string tolower $abr] $i jpayne@69: dict set l [string tolower $full] $i jpayne@69: incr i jpayne@69: } jpayne@69: lassign [UniquePrefixRegexp $l] regex lookup jpayne@69: append re ( $regex ) jpayne@69: dict set fieldSet dayOfWeek [incr fieldCount] jpayne@69: append postcode "dict set date dayOfWeek \[" \ jpayne@69: "dict get " [list $lookup] " " \ jpayne@69: \[ {string tolower $field} [incr captureCount] \] \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: b - B - h { # Name of month jpayne@69: set i 0 jpayne@69: set l {} jpayne@69: foreach \ jpayne@69: abr [mc MONTHS_ABBREV] \ jpayne@69: full [mc MONTHS_FULL] { jpayne@69: incr i jpayne@69: dict set l [string tolower $abr] $i jpayne@69: dict set l [string tolower $full] $i jpayne@69: } jpayne@69: lassign [UniquePrefixRegexp $l] regex lookup jpayne@69: append re ( $regex ) jpayne@69: dict set fieldSet month [incr fieldCount] jpayne@69: append postcode "dict set date month \[" \ jpayne@69: "dict get " [list $lookup] \ jpayne@69: " " \[ {string tolower $field} \ jpayne@69: [incr captureCount] \] \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: C { # Gregorian century jpayne@69: append re \\s*(\\d\\d?) jpayne@69: dict set fieldSet century [incr fieldCount] jpayne@69: append postcode "dict set date century \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: d - e { # Day of month jpayne@69: append re \\s*(\\d\\d?) jpayne@69: dict set fieldSet dayOfMonth [incr fieldCount] jpayne@69: append postcode "dict set date dayOfMonth \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: E { # Prefix for locale-specific codes jpayne@69: set state %E jpayne@69: } jpayne@69: g { # ISO8601 2-digit year jpayne@69: append re \\s*(\\d\\d) jpayne@69: dict set fieldSet iso8601YearOfCentury \ jpayne@69: [incr fieldCount] jpayne@69: append postcode \ jpayne@69: "dict set date iso8601YearOfCentury \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: G { # ISO8601 4-digit year jpayne@69: append re \\s*(\\d\\d)(\\d\\d) jpayne@69: dict set fieldSet iso8601Century [incr fieldCount] jpayne@69: dict set fieldSet iso8601YearOfCentury \ jpayne@69: [incr fieldCount] jpayne@69: append postcode \ jpayne@69: "dict set date iso8601Century \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" \ jpayne@69: "dict set date iso8601YearOfCentury \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: H - k { # Hour of day jpayne@69: append re \\s*(\\d\\d?) jpayne@69: dict set fieldSet hour [incr fieldCount] jpayne@69: append postcode "dict set date hour \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: I - l { # Hour, AM/PM jpayne@69: append re \\s*(\\d\\d?) jpayne@69: dict set fieldSet hourAMPM [incr fieldCount] jpayne@69: append postcode "dict set date hourAMPM \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: j { # Day of year jpayne@69: append re \\s*(\\d\\d?\\d?) jpayne@69: dict set fieldSet dayOfYear [incr fieldCount] jpayne@69: append postcode "dict set date dayOfYear \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: J { # Julian Day Number jpayne@69: append re \\s*(\\d+) jpayne@69: dict set fieldSet julianDay [incr fieldCount] jpayne@69: append postcode "dict set date julianDay \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %ld" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: m - N { # Month number jpayne@69: append re \\s*(\\d\\d?) jpayne@69: dict set fieldSet month [incr fieldCount] jpayne@69: append postcode "dict set date month \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: M { # Minute jpayne@69: append re \\s*(\\d\\d?) jpayne@69: dict set fieldSet minute [incr fieldCount] jpayne@69: append postcode "dict set date minute \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: n { # Literal newline jpayne@69: append re \\n jpayne@69: } jpayne@69: O { # Prefix for locale numerics jpayne@69: set state %O jpayne@69: } jpayne@69: p - P { # AM/PM indicator jpayne@69: set l [list [string tolower [mc AM]] 0 \ jpayne@69: [string tolower [mc PM]] 1] jpayne@69: lassign [UniquePrefixRegexp $l] regex lookup jpayne@69: append re ( $regex ) jpayne@69: dict set fieldSet amPmIndicator [incr fieldCount] jpayne@69: append postcode "dict set date amPmIndicator \[" \ jpayne@69: "dict get " [list $lookup] " \[string tolower " \ jpayne@69: "\$field" \ jpayne@69: [incr captureCount] \ jpayne@69: "\]\]\n" jpayne@69: } jpayne@69: Q { # Hi, Jeff! jpayne@69: append re {Stardate\s+([-+]?\d+)(\d\d\d)[.](\d)} jpayne@69: incr captureCount jpayne@69: dict set fieldSet seconds [incr fieldCount] jpayne@69: append postcode {dict set date seconds } \[ \ jpayne@69: {ParseStarDate $field} [incr captureCount] \ jpayne@69: { $field} [incr captureCount] \ jpayne@69: { $field} [incr captureCount] \ jpayne@69: \] \n jpayne@69: } jpayne@69: s { # Seconds from Posix Epoch jpayne@69: # This next case is insanely difficult, because it's jpayne@69: # problematic to determine whether the field is jpayne@69: # actually within the range of a wide integer. jpayne@69: append re {\s*([-+]?\d+)} jpayne@69: dict set fieldSet seconds [incr fieldCount] jpayne@69: append postcode {dict set date seconds } \[ \ jpayne@69: {ScanWide $field} [incr captureCount] \] \n jpayne@69: } jpayne@69: S { # Second jpayne@69: append re \\s*(\\d\\d?) jpayne@69: dict set fieldSet second [incr fieldCount] jpayne@69: append postcode "dict set date second \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: t { # Literal tab character jpayne@69: append re \\t jpayne@69: } jpayne@69: u - w { # Day number within week, 0 or 7 == Sun jpayne@69: # 1=Mon, 6=Sat jpayne@69: append re \\s*(\\d) jpayne@69: dict set fieldSet dayOfWeek [incr fieldCount] jpayne@69: append postcode {::scan $field} [incr captureCount] \ jpayne@69: { %d dow} \n \ jpayne@69: { jpayne@69: if { $dow == 0 } { jpayne@69: set dow 7 jpayne@69: } elseif { $dow > 7 } { jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK badDayOfWeek] \ jpayne@69: "day of week is greater than 7" jpayne@69: } jpayne@69: dict set date dayOfWeek $dow jpayne@69: } jpayne@69: } jpayne@69: U { # Week of year. The first Sunday of jpayne@69: # the year is the first day of week jpayne@69: # 01. No scan rule uses this group. jpayne@69: append re \\s*\\d\\d? jpayne@69: } jpayne@69: V { # Week of ISO8601 year jpayne@69: jpayne@69: append re \\s*(\\d\\d?) jpayne@69: dict set fieldSet iso8601Week [incr fieldCount] jpayne@69: append postcode "dict set date iso8601Week \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: W { # Week of the year (00-53). The first jpayne@69: # Monday of the year is the first day jpayne@69: # of week 01. No scan rule uses this jpayne@69: # group. jpayne@69: append re \\s*\\d\\d? jpayne@69: } jpayne@69: y { # Two-digit Gregorian year jpayne@69: append re \\s*(\\d\\d?) jpayne@69: dict set fieldSet yearOfCentury [incr fieldCount] jpayne@69: append postcode "dict set date yearOfCentury \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: Y { # 4-digit Gregorian year jpayne@69: append re \\s*(\\d\\d)(\\d\\d) jpayne@69: dict set fieldSet century [incr fieldCount] jpayne@69: dict set fieldSet yearOfCentury [incr fieldCount] jpayne@69: append postcode \ jpayne@69: "dict set date century \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" \ jpayne@69: "dict set date yearOfCentury \[" \ jpayne@69: "::scan \$field" [incr captureCount] " %d" \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: z - Z { # Time zone name jpayne@69: append re {(?:([-+]\d\d(?::?\d\d(?::?\d\d)?)?)|([[:alnum:]]{1,4}))} jpayne@69: dict set fieldSet tzName [incr fieldCount] jpayne@69: append postcode \ jpayne@69: {if } \{ { $field} [incr captureCount] \ jpayne@69: { ne "" } \} { } \{ \n \ jpayne@69: {dict set date tzName $field} \ jpayne@69: $captureCount \n \ jpayne@69: \} { else } \{ \n \ jpayne@69: {dict set date tzName } \[ \ jpayne@69: {ConvertLegacyTimeZone $field} \ jpayne@69: [incr captureCount] \] \n \ jpayne@69: \} \n \ jpayne@69: } jpayne@69: % { # Literal percent character jpayne@69: append re % jpayne@69: } jpayne@69: default { jpayne@69: append re % jpayne@69: if { ! [string is alnum $c] } { jpayne@69: append re \\ jpayne@69: } jpayne@69: append re $c jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: %E { jpayne@69: switch -exact -- $c { jpayne@69: C { # Locale-dependent era jpayne@69: set d {} jpayne@69: foreach triple [mc LOCALE_ERAS] { jpayne@69: lassign $triple t symbol year jpayne@69: dict set d [string tolower $symbol] $year jpayne@69: } jpayne@69: lassign [UniquePrefixRegexp $d] regex lookup jpayne@69: append re (?: $regex ) jpayne@69: } jpayne@69: E { jpayne@69: set l {} jpayne@69: dict set l [string tolower [mc BCE]] BCE jpayne@69: dict set l [string tolower [mc CE]] CE jpayne@69: dict set l b.c.e. BCE jpayne@69: dict set l c.e. CE jpayne@69: dict set l b.c. BCE jpayne@69: dict set l a.d. CE jpayne@69: lassign [UniquePrefixRegexp $l] regex lookup jpayne@69: append re ( $regex ) jpayne@69: dict set fieldSet era [incr fieldCount] jpayne@69: append postcode "dict set date era \["\ jpayne@69: "dict get " [list $lookup] \ jpayne@69: { } \[ {string tolower $field} \ jpayne@69: [incr captureCount] \] \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: y { # Locale-dependent year of the era jpayne@69: lassign [LocaleNumeralMatcher $locale] regex lookup jpayne@69: append re $regex jpayne@69: incr captureCount jpayne@69: } jpayne@69: default { jpayne@69: append re %E jpayne@69: if { ! [string is alnum $c] } { jpayne@69: append re \\ jpayne@69: } jpayne@69: append re $c jpayne@69: } jpayne@69: } jpayne@69: set state {} jpayne@69: } jpayne@69: %O { jpayne@69: switch -exact -- $c { jpayne@69: d - e { jpayne@69: lassign [LocaleNumeralMatcher $locale] regex lookup jpayne@69: append re $regex jpayne@69: dict set fieldSet dayOfMonth [incr fieldCount] jpayne@69: append postcode "dict set date dayOfMonth \[" \ jpayne@69: "dict get " [list $lookup] " \$field" \ jpayne@69: [incr captureCount] \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: H - k { jpayne@69: lassign [LocaleNumeralMatcher $locale] regex lookup jpayne@69: append re $regex jpayne@69: dict set fieldSet hour [incr fieldCount] jpayne@69: append postcode "dict set date hour \[" \ jpayne@69: "dict get " [list $lookup] " \$field" \ jpayne@69: [incr captureCount] \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: I - l { jpayne@69: lassign [LocaleNumeralMatcher $locale] regex lookup jpayne@69: append re $regex jpayne@69: dict set fieldSet hourAMPM [incr fieldCount] jpayne@69: append postcode "dict set date hourAMPM \[" \ jpayne@69: "dict get " [list $lookup] " \$field" \ jpayne@69: [incr captureCount] \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: m { jpayne@69: lassign [LocaleNumeralMatcher $locale] regex lookup jpayne@69: append re $regex jpayne@69: dict set fieldSet month [incr fieldCount] jpayne@69: append postcode "dict set date month \[" \ jpayne@69: "dict get " [list $lookup] " \$field" \ jpayne@69: [incr captureCount] \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: M { jpayne@69: lassign [LocaleNumeralMatcher $locale] regex lookup jpayne@69: append re $regex jpayne@69: dict set fieldSet minute [incr fieldCount] jpayne@69: append postcode "dict set date minute \[" \ jpayne@69: "dict get " [list $lookup] " \$field" \ jpayne@69: [incr captureCount] \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: S { jpayne@69: lassign [LocaleNumeralMatcher $locale] regex lookup jpayne@69: append re $regex jpayne@69: dict set fieldSet second [incr fieldCount] jpayne@69: append postcode "dict set date second \[" \ jpayne@69: "dict get " [list $lookup] " \$field" \ jpayne@69: [incr captureCount] \ jpayne@69: "\]\n" jpayne@69: } jpayne@69: u - w { jpayne@69: lassign [LocaleNumeralMatcher $locale] regex lookup jpayne@69: append re $regex jpayne@69: dict set fieldSet dayOfWeek [incr fieldCount] jpayne@69: append postcode "set dow \[dict get " [list $lookup] \ jpayne@69: { $field} [incr captureCount] \] \n \ jpayne@69: { jpayne@69: if { $dow == 0 } { jpayne@69: set dow 7 jpayne@69: } elseif { $dow > 7 } { jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK badDayOfWeek] \ jpayne@69: "day of week is greater than 7" jpayne@69: } jpayne@69: dict set date dayOfWeek $dow jpayne@69: } jpayne@69: } jpayne@69: y { jpayne@69: lassign [LocaleNumeralMatcher $locale] regex lookup jpayne@69: append re $regex jpayne@69: dict set fieldSet yearOfCentury [incr fieldCount] jpayne@69: append postcode {dict set date yearOfCentury } \[ \ jpayne@69: {dict get } [list $lookup] { $field} \ jpayne@69: [incr captureCount] \] \n jpayne@69: } jpayne@69: default { jpayne@69: append re %O jpayne@69: if { ! [string is alnum $c] } { jpayne@69: append re \\ jpayne@69: } jpayne@69: append re $c jpayne@69: } jpayne@69: } jpayne@69: set state {} jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Clean up any unfinished format groups jpayne@69: jpayne@69: append re $state \\s*\$ jpayne@69: jpayne@69: # Build the procedure jpayne@69: jpayne@69: set procBody {} jpayne@69: append procBody "variable ::tcl::clock::TZData" \n jpayne@69: append procBody "if \{ !\[ regexp -nocase [list $re] \$string ->" jpayne@69: for { set i 1 } { $i <= $captureCount } { incr i } { jpayne@69: append procBody " " field $i jpayne@69: } jpayne@69: append procBody "\] \} \{" \n jpayne@69: append procBody { jpayne@69: return -code error -errorcode [list CLOCK badInputString] \ jpayne@69: {input string does not match supplied format} jpayne@69: } jpayne@69: append procBody \}\n jpayne@69: append procBody "set date \[dict create\]" \n jpayne@69: append procBody {dict set date tzName $timeZone} \n jpayne@69: append procBody $postcode jpayne@69: append procBody [list set changeover [mc GREGORIAN_CHANGE_DATE]] \n jpayne@69: jpayne@69: # Set up the time zone before doing anything with a default base date jpayne@69: # that might need a timezone to interpret it. jpayne@69: jpayne@69: if { ![dict exists $fieldSet seconds] jpayne@69: && ![dict exists $fieldSet starDate] } { jpayne@69: if { [dict exists $fieldSet tzName] } { jpayne@69: append procBody { jpayne@69: set timeZone [dict get $date tzName] jpayne@69: } jpayne@69: } jpayne@69: append procBody { jpayne@69: ::tcl::clock::SetupTimeZone $timeZone jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Add code that gets Julian Day Number from the fields. jpayne@69: jpayne@69: append procBody [MakeParseCodeFromFields $fieldSet $DateParseActions] jpayne@69: jpayne@69: # Get time of day jpayne@69: jpayne@69: append procBody [MakeParseCodeFromFields $fieldSet $TimeParseActions] jpayne@69: jpayne@69: # Assemble seconds from the Julian day and second of the day. jpayne@69: # Convert to local time unless epoch seconds or stardate are jpayne@69: # being processed - they're always absolute jpayne@69: jpayne@69: if { ![dict exists $fieldSet seconds] jpayne@69: && ![dict exists $fieldSet starDate] } { jpayne@69: append procBody { jpayne@69: if { [dict get $date julianDay] > 5373484 } { jpayne@69: return -code error -errorcode [list CLOCK dateTooLarge] \ jpayne@69: "requested date too large to represent" jpayne@69: } jpayne@69: dict set date localSeconds [expr { jpayne@69: -210866803200 jpayne@69: + ( 86400 * wide([dict get $date julianDay]) ) jpayne@69: + [dict get $date secondOfDay] jpayne@69: }] jpayne@69: } jpayne@69: jpayne@69: # Finally, convert the date to local time jpayne@69: jpayne@69: append procBody { jpayne@69: set date [::tcl::clock::ConvertLocalToUTC $date[set date {}] \ jpayne@69: $TZData($timeZone) $changeover] jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Return result jpayne@69: jpayne@69: append procBody {return [dict get $date seconds]} \n jpayne@69: jpayne@69: proc $procName { string baseTime timeZone } $procBody jpayne@69: jpayne@69: # puts [list proc $procName [list string baseTime timeZone] $procBody] jpayne@69: jpayne@69: return $procName jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # LocaleNumeralMatcher -- jpayne@69: # jpayne@69: # Composes a regexp that captures the numerals in the given locale, and jpayne@69: # a dictionary to map them to conventional numerals. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # locale - Name of the current locale jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns a two-element list comprising the regexp and the dictionary. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # Caches the result. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::LocaleNumeralMatcher {l} { jpayne@69: variable LocaleNumeralCache jpayne@69: jpayne@69: if { ![dict exists $LocaleNumeralCache $l] } { jpayne@69: set d {} jpayne@69: set i 0 jpayne@69: set sep \( jpayne@69: foreach n [mc LOCALE_NUMERALS] { jpayne@69: dict set d $n $i jpayne@69: regsub -all {[^[:alnum:]]} $n \\\\& subex jpayne@69: append re $sep $subex jpayne@69: set sep | jpayne@69: incr i jpayne@69: } jpayne@69: append re \) jpayne@69: dict set LocaleNumeralCache $l [list $re $d] jpayne@69: } jpayne@69: return [dict get $LocaleNumeralCache $l] jpayne@69: } jpayne@69: jpayne@69: jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # UniquePrefixRegexp -- jpayne@69: # jpayne@69: # Composes a regexp that performs unique-prefix matching. The RE jpayne@69: # matches one of a supplied set of strings, or any unique prefix jpayne@69: # thereof. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # data - List of alternating match-strings and values. jpayne@69: # Match-strings with distinct values are considered jpayne@69: # distinct. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns a two-element list. The first is a regexp that matches any jpayne@69: # unique prefix of any of the strings. The second is a dictionary whose jpayne@69: # keys are match values from the regexp and whose values are the jpayne@69: # corresponding values from 'data'. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::UniquePrefixRegexp { data } { jpayne@69: # The 'successors' dictionary will contain, for each string that is a jpayne@69: # prefix of any key, all characters that may follow that prefix. The jpayne@69: # 'prefixMapping' dictionary will have keys that are prefixes of keys and jpayne@69: # values that correspond to the keys. jpayne@69: jpayne@69: set prefixMapping [dict create] jpayne@69: set successors [dict create {} {}] jpayne@69: jpayne@69: # Walk the key-value pairs jpayne@69: jpayne@69: foreach { key value } $data { jpayne@69: # Construct all prefixes of the key; jpayne@69: jpayne@69: set prefix {} jpayne@69: foreach char [split $key {}] { jpayne@69: set oldPrefix $prefix jpayne@69: dict set successors $oldPrefix $char {} jpayne@69: append prefix $char jpayne@69: jpayne@69: # Put the prefixes in the 'prefixMapping' and 'successors' jpayne@69: # dictionaries jpayne@69: jpayne@69: dict lappend prefixMapping $prefix $value jpayne@69: if { ![dict exists $successors $prefix] } { jpayne@69: dict set successors $prefix {} jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Identify those prefixes that designate unique values, and those that are jpayne@69: # the full keys jpayne@69: jpayne@69: set uniquePrefixMapping {} jpayne@69: dict for { key valueList } $prefixMapping { jpayne@69: if { [llength $valueList] == 1 } { jpayne@69: dict set uniquePrefixMapping $key [lindex $valueList 0] jpayne@69: } jpayne@69: } jpayne@69: foreach { key value } $data { jpayne@69: dict set uniquePrefixMapping $key $value jpayne@69: } jpayne@69: jpayne@69: # Construct the re. jpayne@69: jpayne@69: return [list \ jpayne@69: [MakeUniquePrefixRegexp $successors $uniquePrefixMapping {}] \ jpayne@69: $uniquePrefixMapping] jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # MakeUniquePrefixRegexp -- jpayne@69: # jpayne@69: # Service procedure for 'UniquePrefixRegexp' that constructs a regular jpayne@69: # expresison that matches the unique prefixes. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # successors - Dictionary whose keys are all prefixes jpayne@69: # of keys passed to 'UniquePrefixRegexp' and whose jpayne@69: # values are dictionaries whose keys are the characters jpayne@69: # that may follow those prefixes. jpayne@69: # uniquePrefixMapping - Dictionary whose keys are the unique jpayne@69: # prefixes and whose values are not examined. jpayne@69: # prefixString - Current prefix being processed. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns a constructed regular expression that matches the set of jpayne@69: # unique prefixes beginning with the 'prefixString'. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::MakeUniquePrefixRegexp { successors jpayne@69: uniquePrefixMapping jpayne@69: prefixString } { jpayne@69: jpayne@69: # Get the characters that may follow the current prefix string jpayne@69: jpayne@69: set schars [lsort -ascii [dict keys [dict get $successors $prefixString]]] jpayne@69: if { [llength $schars] == 0 } { jpayne@69: return {} jpayne@69: } jpayne@69: jpayne@69: # If there is more than one successor character, or if the current prefix jpayne@69: # is a unique prefix, surround the generated re with non-capturing jpayne@69: # parentheses. jpayne@69: jpayne@69: set re {} jpayne@69: if { jpayne@69: [dict exists $uniquePrefixMapping $prefixString] jpayne@69: || [llength $schars] > 1 jpayne@69: } then { jpayne@69: append re "(?:" jpayne@69: } jpayne@69: jpayne@69: # Generate a regexp that matches the successors. jpayne@69: jpayne@69: set sep "" jpayne@69: foreach { c } $schars { jpayne@69: set nextPrefix $prefixString$c jpayne@69: regsub -all {[^[:alnum:]]} $c \\\\& rechar jpayne@69: append re $sep $rechar \ jpayne@69: [MakeUniquePrefixRegexp \ jpayne@69: $successors $uniquePrefixMapping $nextPrefix] jpayne@69: set sep | jpayne@69: } jpayne@69: jpayne@69: # If the current prefix is a unique prefix, make all following text jpayne@69: # optional. Otherwise, if there is more than one successor character, jpayne@69: # close the non-capturing parentheses. jpayne@69: jpayne@69: if { [dict exists $uniquePrefixMapping $prefixString] } { jpayne@69: append re ")?" jpayne@69: } elseif { [llength $schars] > 1 } { jpayne@69: append re ")" jpayne@69: } jpayne@69: jpayne@69: return $re jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # MakeParseCodeFromFields -- jpayne@69: # jpayne@69: # Composes Tcl code to extract the Julian Day Number from a dictionary jpayne@69: # containing date fields. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # dateFields -- Dictionary whose keys are fields of the date, jpayne@69: # and whose values are the rightmost positions jpayne@69: # at which those fields appear. jpayne@69: # parseActions -- List of triples: field set, priority, and jpayne@69: # code to emit. Smaller priorities are better, and jpayne@69: # the list must be in ascending order by priority jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns a burst of code that extracts the day number from the given jpayne@69: # date. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::MakeParseCodeFromFields { dateFields parseActions } { jpayne@69: jpayne@69: set currPrio 999 jpayne@69: set currFieldPos [list] jpayne@69: set currCodeBurst { jpayne@69: error "in ::tcl::clock::MakeParseCodeFromFields: can't happen" jpayne@69: } jpayne@69: jpayne@69: foreach { fieldSet prio parseAction } $parseActions { jpayne@69: # If we've found an answer that's better than any that follow, quit jpayne@69: # now. jpayne@69: jpayne@69: if { $prio > $currPrio } { jpayne@69: break jpayne@69: } jpayne@69: jpayne@69: # Accumulate the field positions that are used in the current field jpayne@69: # grouping. jpayne@69: jpayne@69: set fieldPos [list] jpayne@69: set ok true jpayne@69: foreach field $fieldSet { jpayne@69: if { ! [dict exists $dateFields $field] } { jpayne@69: set ok 0 jpayne@69: break jpayne@69: } jpayne@69: lappend fieldPos [dict get $dateFields $field] jpayne@69: } jpayne@69: jpayne@69: # Quit if we don't have a complete set of fields jpayne@69: if { !$ok } { jpayne@69: continue jpayne@69: } jpayne@69: jpayne@69: # Determine whether the current answer is better than the last. jpayne@69: jpayne@69: set fPos [lsort -integer -decreasing $fieldPos] jpayne@69: jpayne@69: if { $prio == $currPrio } { jpayne@69: foreach currPos $currFieldPos newPos $fPos { jpayne@69: if { jpayne@69: ![string is integer $newPos] jpayne@69: || ![string is integer $currPos] jpayne@69: || $newPos > $currPos jpayne@69: } then { jpayne@69: break jpayne@69: } jpayne@69: if { $newPos < $currPos } { jpayne@69: set ok 0 jpayne@69: break jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: if { !$ok } { jpayne@69: continue jpayne@69: } jpayne@69: jpayne@69: # Remember the best possibility for extracting date information jpayne@69: jpayne@69: set currPrio $prio jpayne@69: set currFieldPos $fPos jpayne@69: set currCodeBurst $parseAction jpayne@69: } jpayne@69: jpayne@69: return $currCodeBurst jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # EnterLocale -- jpayne@69: # jpayne@69: # Switch [mclocale] to a given locale if necessary jpayne@69: # jpayne@69: # Parameters: jpayne@69: # locale -- Desired locale jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the locale that was previously current. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # Does [mclocale]. If necessary, loades the designated locale's files. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::EnterLocale { locale } { jpayne@69: if { $locale eq {system} } { jpayne@69: if { $::tcl_platform(platform) ne {windows} } { jpayne@69: # On a non-windows platform, the 'system' locale is the same as jpayne@69: # the 'current' locale jpayne@69: jpayne@69: set locale current jpayne@69: } else { jpayne@69: # On a windows platform, the 'system' locale is adapted from the jpayne@69: # 'current' locale by applying the date and time formats from the jpayne@69: # Control Panel. First, load the 'current' locale if it's not yet jpayne@69: # loaded jpayne@69: jpayne@69: mcpackagelocale set [mclocale] jpayne@69: jpayne@69: # Make a new locale string for the system locale, and get the jpayne@69: # Control Panel information jpayne@69: jpayne@69: set locale [mclocale]_windows jpayne@69: if { ! [mcpackagelocale present $locale] } { jpayne@69: LoadWindowsDateTimeFormats $locale jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: if { $locale eq {current}} { jpayne@69: set locale [mclocale] jpayne@69: } jpayne@69: # Eventually load the locale jpayne@69: mcpackagelocale set $locale jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # LoadWindowsDateTimeFormats -- jpayne@69: # jpayne@69: # Load the date/time formats from the Control Panel in Windows and jpayne@69: # convert them so that they're usable by Tcl. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # locale - Name of the locale in whose message catalog jpayne@69: # the converted formats are to be stored. jpayne@69: # jpayne@69: # Results: jpayne@69: # None. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # Updates the given message catalog with the locale strings. jpayne@69: # jpayne@69: # Presumes that on entry, [mclocale] is set to the current locale, so that jpayne@69: # default strings can be obtained if the Registry query fails. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::LoadWindowsDateTimeFormats { locale } { jpayne@69: # Bail out if we can't find the Registry jpayne@69: jpayne@69: variable NoRegistry jpayne@69: if { [info exists NoRegistry] } return jpayne@69: jpayne@69: if { ![catch { jpayne@69: registry get "HKEY_CURRENT_USER\\Control Panel\\International" \ jpayne@69: sShortDate jpayne@69: } string] } { jpayne@69: set quote {} jpayne@69: set datefmt {} jpayne@69: foreach { unquoted quoted } [split $string '] { jpayne@69: append datefmt $quote [string map { jpayne@69: dddd %A jpayne@69: ddd %a jpayne@69: dd %d jpayne@69: d %e jpayne@69: MMMM %B jpayne@69: MMM %b jpayne@69: MM %m jpayne@69: M %N jpayne@69: yyyy %Y jpayne@69: yy %y jpayne@69: y %y jpayne@69: gg {} jpayne@69: } $unquoted] jpayne@69: if { $quoted eq {} } { jpayne@69: set quote ' jpayne@69: } else { jpayne@69: set quote $quoted jpayne@69: } jpayne@69: } jpayne@69: ::msgcat::mcset $locale DATE_FORMAT $datefmt jpayne@69: } jpayne@69: jpayne@69: if { ![catch { jpayne@69: registry get "HKEY_CURRENT_USER\\Control Panel\\International" \ jpayne@69: sLongDate jpayne@69: } string] } { jpayne@69: set quote {} jpayne@69: set ldatefmt {} jpayne@69: foreach { unquoted quoted } [split $string '] { jpayne@69: append ldatefmt $quote [string map { jpayne@69: dddd %A jpayne@69: ddd %a jpayne@69: dd %d jpayne@69: d %e jpayne@69: MMMM %B jpayne@69: MMM %b jpayne@69: MM %m jpayne@69: M %N jpayne@69: yyyy %Y jpayne@69: yy %y jpayne@69: y %y jpayne@69: gg {} jpayne@69: } $unquoted] jpayne@69: if { $quoted eq {} } { jpayne@69: set quote ' jpayne@69: } else { jpayne@69: set quote $quoted jpayne@69: } jpayne@69: } jpayne@69: ::msgcat::mcset $locale LOCALE_DATE_FORMAT $ldatefmt jpayne@69: } jpayne@69: jpayne@69: if { ![catch { jpayne@69: registry get "HKEY_CURRENT_USER\\Control Panel\\International" \ jpayne@69: sTimeFormat jpayne@69: } string] } { jpayne@69: set quote {} jpayne@69: set timefmt {} jpayne@69: foreach { unquoted quoted } [split $string '] { jpayne@69: append timefmt $quote [string map { jpayne@69: HH %H jpayne@69: H %k jpayne@69: hh %I jpayne@69: h %l jpayne@69: mm %M jpayne@69: m %M jpayne@69: ss %S jpayne@69: s %S jpayne@69: tt %p jpayne@69: t %p jpayne@69: } $unquoted] jpayne@69: if { $quoted eq {} } { jpayne@69: set quote ' jpayne@69: } else { jpayne@69: set quote $quoted jpayne@69: } jpayne@69: } jpayne@69: ::msgcat::mcset $locale TIME_FORMAT $timefmt jpayne@69: } jpayne@69: jpayne@69: catch { jpayne@69: ::msgcat::mcset $locale DATE_TIME_FORMAT "$datefmt $timefmt" jpayne@69: } jpayne@69: catch { jpayne@69: ::msgcat::mcset $locale LOCALE_DATE_TIME_FORMAT "$ldatefmt $timefmt" jpayne@69: } jpayne@69: jpayne@69: return jpayne@69: jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # LocalizeFormat -- jpayne@69: # jpayne@69: # Map away locale-dependent format groups in a clock format. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # locale -- Current [mclocale] locale, supplied to avoid jpayne@69: # an extra call jpayne@69: # format -- Format supplied to [clock scan] or [clock format] jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the string with locale-dependent composite format groups jpayne@69: # substituted out. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::LocalizeFormat { locale format } { jpayne@69: jpayne@69: # message catalog key to cache this format jpayne@69: set key FORMAT_$format jpayne@69: jpayne@69: if { [::msgcat::mcexists -exactlocale -exactnamespace $key] } { jpayne@69: return [mc $key] jpayne@69: } jpayne@69: # Handle locale-dependent format groups by mapping them out of the format jpayne@69: # string. Note that the order of the [string map] operations is jpayne@69: # significant because later formats can refer to later ones; for example jpayne@69: # %c can refer to %X, which in turn can refer to %T. jpayne@69: jpayne@69: set list { jpayne@69: %% %% jpayne@69: %D %m/%d/%Y jpayne@69: %+ {%a %b %e %H:%M:%S %Z %Y} jpayne@69: } jpayne@69: lappend list %EY [string map $list [mc LOCALE_YEAR_FORMAT]] jpayne@69: lappend list %T [string map $list [mc TIME_FORMAT_24_SECS]] jpayne@69: lappend list %R [string map $list [mc TIME_FORMAT_24]] jpayne@69: lappend list %r [string map $list [mc TIME_FORMAT_12]] jpayne@69: lappend list %X [string map $list [mc TIME_FORMAT]] jpayne@69: lappend list %EX [string map $list [mc LOCALE_TIME_FORMAT]] jpayne@69: lappend list %x [string map $list [mc DATE_FORMAT]] jpayne@69: lappend list %Ex [string map $list [mc LOCALE_DATE_FORMAT]] jpayne@69: lappend list %c [string map $list [mc DATE_TIME_FORMAT]] jpayne@69: lappend list %Ec [string map $list [mc LOCALE_DATE_TIME_FORMAT]] jpayne@69: set format [string map $list $format] jpayne@69: jpayne@69: ::msgcat::mcset $locale $key $format jpayne@69: return $format jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # FormatNumericTimeZone -- jpayne@69: # jpayne@69: # Formats a time zone as +hhmmss jpayne@69: # jpayne@69: # Parameters: jpayne@69: # z - Time zone in seconds east of Greenwich jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the time zone formatted in a numeric form jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::FormatNumericTimeZone { z } { jpayne@69: if { $z < 0 } { jpayne@69: set z [expr { - $z }] jpayne@69: set retval - jpayne@69: } else { jpayne@69: set retval + jpayne@69: } jpayne@69: append retval [::format %02d [expr { $z / 3600 }]] jpayne@69: set z [expr { $z % 3600 }] jpayne@69: append retval [::format %02d [expr { $z / 60 }]] jpayne@69: set z [expr { $z % 60 }] jpayne@69: if { $z != 0 } { jpayne@69: append retval [::format %02d $z] jpayne@69: } jpayne@69: return $retval jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # FormatStarDate -- jpayne@69: # jpayne@69: # Formats a date as a StarDate. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date - Dictionary containing 'year', 'dayOfYear', and jpayne@69: # 'localSeconds' fields. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the given date formatted as a StarDate. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: # Jeff Hobbs put this in to support an atrocious pun about Tcl being jpayne@69: # "Enterprise ready." Now we're stuck with it. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::FormatStarDate { date } { jpayne@69: variable Roddenberry jpayne@69: jpayne@69: # Get day of year, zero based jpayne@69: jpayne@69: set doy [expr { [dict get $date dayOfYear] - 1 }] jpayne@69: jpayne@69: # Determine whether the year is a leap year jpayne@69: jpayne@69: set lp [IsGregorianLeapYear $date] jpayne@69: jpayne@69: # Convert day of year to a fractional year jpayne@69: jpayne@69: if { $lp } { jpayne@69: set fractYear [expr { 1000 * $doy / 366 }] jpayne@69: } else { jpayne@69: set fractYear [expr { 1000 * $doy / 365 }] jpayne@69: } jpayne@69: jpayne@69: # Put together the StarDate jpayne@69: jpayne@69: return [::format "Stardate %02d%03d.%1d" \ jpayne@69: [expr { [dict get $date year] - $Roddenberry }] \ jpayne@69: $fractYear \ jpayne@69: [expr { [dict get $date localSeconds] % 86400 jpayne@69: / ( 86400 / 10 ) }]] jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ParseStarDate -- jpayne@69: # jpayne@69: # Parses a StarDate jpayne@69: # jpayne@69: # Parameters: jpayne@69: # year - Year from the Roddenberry epoch jpayne@69: # fractYear - Fraction of a year specifiying the day of year. jpayne@69: # fractDay - Fraction of a day jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns a count of seconds from the Posix epoch. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: # Jeff Hobbs put this in to support an atrocious pun about Tcl being jpayne@69: # "Enterprise ready." Now we're stuck with it. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::ParseStarDate { year fractYear fractDay } { jpayne@69: variable Roddenberry jpayne@69: jpayne@69: # Build a tentative date from year and fraction. jpayne@69: jpayne@69: set date [dict create \ jpayne@69: gregorian 1 \ jpayne@69: era CE \ jpayne@69: year [expr { $year + $Roddenberry }] \ jpayne@69: dayOfYear [expr { $fractYear * 365 / 1000 + 1 }]] jpayne@69: set date [GetJulianDayFromGregorianEraYearDay $date[set date {}]] jpayne@69: jpayne@69: # Determine whether the given year is a leap year jpayne@69: jpayne@69: set lp [IsGregorianLeapYear $date] jpayne@69: jpayne@69: # Reconvert the fractional year according to whether the given year is a jpayne@69: # leap year jpayne@69: jpayne@69: if { $lp } { jpayne@69: dict set date dayOfYear \ jpayne@69: [expr { $fractYear * 366 / 1000 + 1 }] jpayne@69: } else { jpayne@69: dict set date dayOfYear \ jpayne@69: [expr { $fractYear * 365 / 1000 + 1 }] jpayne@69: } jpayne@69: dict unset date julianDay jpayne@69: dict unset date gregorian jpayne@69: set date [GetJulianDayFromGregorianEraYearDay $date[set date {}]] jpayne@69: jpayne@69: return [expr { jpayne@69: 86400 * [dict get $date julianDay] jpayne@69: - 210866803200 jpayne@69: + ( 86400 / 10 ) * $fractDay jpayne@69: }] jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ScanWide -- jpayne@69: # jpayne@69: # Scans a wide integer from an input jpayne@69: # jpayne@69: # Parameters: jpayne@69: # str - String containing a decimal wide integer jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the string as a pure wide integer. Throws an error if the jpayne@69: # string is misformatted or out of range. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::ScanWide { str } { jpayne@69: set count [::scan $str {%ld %c} result junk] jpayne@69: if { $count != 1 } { jpayne@69: return -code error -errorcode [list CLOCK notAnInteger $str] \ jpayne@69: "\"$str\" is not an integer" jpayne@69: } jpayne@69: if { [incr result 0] != $str } { jpayne@69: return -code error -errorcode [list CLOCK integervalueTooLarge] \ jpayne@69: "integer value too large to represent" jpayne@69: } jpayne@69: return $result jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # InterpretTwoDigitYear -- jpayne@69: # jpayne@69: # Given a date that contains only the year of the century, determines jpayne@69: # the target value of a two-digit year. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date - Dictionary containing fields of the date. jpayne@69: # baseTime - Base time relative to which the date is expressed. jpayne@69: # twoDigitField - Name of the field that stores the two-digit year. jpayne@69: # Default is 'yearOfCentury' jpayne@69: # fourDigitField - Name of the field that will receive the four-digit jpayne@69: # year. Default is 'year' jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the dictionary augmented with the four-digit year, stored in jpayne@69: # the given key. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: # The current rule for interpreting a two-digit year is that the year shall be jpayne@69: # between 1937 and 2037, thus staying within the range of a 32-bit signed jpayne@69: # value for time. This rule may change to a sliding window in future jpayne@69: # versions, so the 'baseTime' parameter (which is currently ignored) is jpayne@69: # provided in the procedure signature. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::InterpretTwoDigitYear { date baseTime jpayne@69: { twoDigitField yearOfCentury } jpayne@69: { fourDigitField year } } { jpayne@69: set yr [dict get $date $twoDigitField] jpayne@69: if { $yr <= 37 } { jpayne@69: dict set date $fourDigitField [expr { $yr + 2000 }] jpayne@69: } else { jpayne@69: dict set date $fourDigitField [expr { $yr + 1900 }] jpayne@69: } jpayne@69: return $date jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # AssignBaseYear -- jpayne@69: # jpayne@69: # Places the number of the current year into a dictionary. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date - Dictionary value to update jpayne@69: # baseTime - Base time from which to extract the year, expressed jpayne@69: # in seconds from the Posix epoch jpayne@69: # timezone - the time zone in which the date is being scanned jpayne@69: # changeover - the Julian Day on which the Gregorian calendar jpayne@69: # was adopted in the target locale. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the dictionary with the current year assigned. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::AssignBaseYear { date baseTime timezone changeover } { jpayne@69: variable TZData jpayne@69: jpayne@69: # Find the Julian Day Number corresponding to the base time, and jpayne@69: # find the Gregorian year corresponding to that Julian Day. jpayne@69: jpayne@69: set date2 [GetDateFields $baseTime $TZData($timezone) $changeover] jpayne@69: jpayne@69: # Store the converted year jpayne@69: jpayne@69: dict set date era [dict get $date2 era] jpayne@69: dict set date year [dict get $date2 year] jpayne@69: jpayne@69: return $date jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # AssignBaseIso8601Year -- jpayne@69: # jpayne@69: # Determines the base year in the ISO8601 fiscal calendar. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date - Dictionary containing the fields of the date that jpayne@69: # is to be augmented with the base year. jpayne@69: # baseTime - Base time expressed in seconds from the Posix epoch. jpayne@69: # timeZone - Target time zone jpayne@69: # changeover - Julian Day of adoption of the Gregorian calendar in jpayne@69: # the target locale. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the given date with "iso8601Year" set to the jpayne@69: # base year. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::AssignBaseIso8601Year {date baseTime timeZone changeover} { jpayne@69: variable TZData jpayne@69: jpayne@69: # Find the Julian Day Number corresponding to the base time jpayne@69: jpayne@69: set date2 [GetDateFields $baseTime $TZData($timeZone) $changeover] jpayne@69: jpayne@69: # Calculate the ISO8601 date and transfer the year jpayne@69: jpayne@69: dict set date era CE jpayne@69: dict set date iso8601Year [dict get $date2 iso8601Year] jpayne@69: return $date jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # AssignBaseMonth -- jpayne@69: # jpayne@69: # Places the number of the current year and month into a jpayne@69: # dictionary. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date - Dictionary value to update jpayne@69: # baseTime - Time from which the year and month are to be jpayne@69: # obtained, expressed in seconds from the Posix epoch. jpayne@69: # timezone - Name of the desired time zone jpayne@69: # changeover - Julian Day on which the Gregorian calendar was adopted. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the dictionary with the base year and month assigned. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::AssignBaseMonth {date baseTime timezone changeover} { jpayne@69: variable TZData jpayne@69: jpayne@69: # Find the year and month corresponding to the base time jpayne@69: jpayne@69: set date2 [GetDateFields $baseTime $TZData($timezone) $changeover] jpayne@69: dict set date era [dict get $date2 era] jpayne@69: dict set date year [dict get $date2 year] jpayne@69: dict set date month [dict get $date2 month] jpayne@69: return $date jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # AssignBaseWeek -- jpayne@69: # jpayne@69: # Determines the base year and week in the ISO8601 fiscal calendar. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date - Dictionary containing the fields of the date that jpayne@69: # is to be augmented with the base year and week. jpayne@69: # baseTime - Base time expressed in seconds from the Posix epoch. jpayne@69: # changeover - Julian Day on which the Gregorian calendar was adopted jpayne@69: # in the target locale. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the given date with "iso8601Year" set to the jpayne@69: # base year and "iso8601Week" to the week number. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::AssignBaseWeek {date baseTime timeZone changeover} { jpayne@69: variable TZData jpayne@69: jpayne@69: # Find the Julian Day Number corresponding to the base time jpayne@69: jpayne@69: set date2 [GetDateFields $baseTime $TZData($timeZone) $changeover] jpayne@69: jpayne@69: # Calculate the ISO8601 date and transfer the year jpayne@69: jpayne@69: dict set date era CE jpayne@69: dict set date iso8601Year [dict get $date2 iso8601Year] jpayne@69: dict set date iso8601Week [dict get $date2 iso8601Week] jpayne@69: return $date jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # AssignBaseJulianDay -- jpayne@69: # jpayne@69: # Determines the base day for a time-of-day conversion. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date - Dictionary that is to get the base day jpayne@69: # baseTime - Base time expressed in seconds from the Posix epoch jpayne@69: # changeover - Julian day on which the Gregorian calendar was jpayne@69: # adpoted in the target locale. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the given dictionary augmented with a 'julianDay' field jpayne@69: # that contains the base day. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::AssignBaseJulianDay { date baseTime timeZone changeover } { jpayne@69: variable TZData jpayne@69: jpayne@69: # Find the Julian Day Number corresponding to the base time jpayne@69: jpayne@69: set date2 [GetDateFields $baseTime $TZData($timeZone) $changeover] jpayne@69: dict set date julianDay [dict get $date2 julianDay] jpayne@69: jpayne@69: return $date jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # InterpretHMSP -- jpayne@69: # jpayne@69: # Interprets a time in the form "hh:mm:ss am". jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date -- Dictionary containing "hourAMPM", "minute", "second" jpayne@69: # and "amPmIndicator" fields. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the number of seconds from local midnight. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::InterpretHMSP { date } { jpayne@69: set hr [dict get $date hourAMPM] jpayne@69: if { $hr == 12 } { jpayne@69: set hr 0 jpayne@69: } jpayne@69: if { [dict get $date amPmIndicator] } { jpayne@69: incr hr 12 jpayne@69: } jpayne@69: dict set date hour $hr jpayne@69: return [InterpretHMS $date[set date {}]] jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # InterpretHMS -- jpayne@69: # jpayne@69: # Interprets a 24-hour time "hh:mm:ss" jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date -- Dictionary containing the "hour", "minute" and "second" jpayne@69: # fields. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the given dictionary augmented with a "secondOfDay" jpayne@69: # field containing the number of seconds from local midnight. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::InterpretHMS { date } { jpayne@69: return [expr { jpayne@69: ( [dict get $date hour] * 60 jpayne@69: + [dict get $date minute] ) * 60 jpayne@69: + [dict get $date second] jpayne@69: }] jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # GetSystemTimeZone -- jpayne@69: # jpayne@69: # Determines the system time zone, which is the default for the jpayne@69: # 'clock' command if no other zone is supplied. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # None. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the system time zone. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # Stores the sustem time zone in the 'CachedSystemTimeZone' jpayne@69: # variable, since determining it may be an expensive process. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::GetSystemTimeZone {} { jpayne@69: variable CachedSystemTimeZone jpayne@69: variable TimeZoneBad jpayne@69: jpayne@69: if {[set result [getenv TCL_TZ]] ne {}} { jpayne@69: set timezone $result jpayne@69: } elseif {[set result [getenv TZ]] ne {}} { jpayne@69: set timezone $result jpayne@69: } else { jpayne@69: # Cache the time zone only if it was detected by one of the jpayne@69: # expensive methods. jpayne@69: if { [info exists CachedSystemTimeZone] } { jpayne@69: set timezone $CachedSystemTimeZone jpayne@69: } elseif { $::tcl_platform(platform) eq {windows} } { jpayne@69: set timezone [GuessWindowsTimeZone] jpayne@69: } elseif { [file exists /etc/localtime] jpayne@69: && ![catch {ReadZoneinfoFile \ jpayne@69: Tcl/Localtime /etc/localtime}] } { jpayne@69: set timezone :Tcl/Localtime jpayne@69: } else { jpayne@69: set timezone :localtime jpayne@69: } jpayne@69: set CachedSystemTimeZone $timezone jpayne@69: } jpayne@69: if { ![dict exists $TimeZoneBad $timezone] } { jpayne@69: dict set TimeZoneBad $timezone [catch {SetupTimeZone $timezone}] jpayne@69: } jpayne@69: if { [dict get $TimeZoneBad $timezone] } { jpayne@69: return :localtime jpayne@69: } else { jpayne@69: return $timezone jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ConvertLegacyTimeZone -- jpayne@69: # jpayne@69: # Given an alphanumeric time zone identifier and the system time zone, jpayne@69: # convert the alphanumeric identifier to an unambiguous time zone. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # tzname - Name of the time zone to convert jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns a time zone name corresponding to tzname, but in an jpayne@69: # unambiguous form, generally +hhmm. jpayne@69: # jpayne@69: # This procedure is implemented primarily to allow the parsing of RFC822 jpayne@69: # date/time strings. Processing a time zone name on input is not recommended jpayne@69: # practice, because there is considerable room for ambiguity; for instance, is jpayne@69: # BST Brazilian Standard Time, or British Summer Time? jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::ConvertLegacyTimeZone { tzname } { jpayne@69: variable LegacyTimeZone jpayne@69: jpayne@69: set tzname [string tolower $tzname] jpayne@69: if { ![dict exists $LegacyTimeZone $tzname] } { jpayne@69: return -code error -errorcode [list CLOCK badTZName $tzname] \ jpayne@69: "time zone \"$tzname\" not found" jpayne@69: } jpayne@69: return [dict get $LegacyTimeZone $tzname] jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # SetupTimeZone -- jpayne@69: # jpayne@69: # Given the name or specification of a time zone, sets up its in-memory jpayne@69: # data. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # tzname - Name of a time zone jpayne@69: # jpayne@69: # Results: jpayne@69: # Unless the time zone is ':localtime', sets the TZData array to contain jpayne@69: # the lookup table for local<->UTC conversion. Returns an error if the jpayne@69: # time zone cannot be parsed. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::SetupTimeZone { timezone } { jpayne@69: variable TZData jpayne@69: jpayne@69: if {! [info exists TZData($timezone)] } { jpayne@69: variable MINWIDE jpayne@69: if { $timezone eq {:localtime} } { jpayne@69: # Nothing to do, we'll convert using the localtime function jpayne@69: jpayne@69: } elseif { jpayne@69: [regexp {^([-+])(\d\d)(?::?(\d\d)(?::?(\d\d))?)?} $timezone \ jpayne@69: -> s hh mm ss] jpayne@69: } then { jpayne@69: # Make a fixed offset jpayne@69: jpayne@69: ::scan $hh %d hh jpayne@69: if { $mm eq {} } { jpayne@69: set mm 0 jpayne@69: } else { jpayne@69: ::scan $mm %d mm jpayne@69: } jpayne@69: if { $ss eq {} } { jpayne@69: set ss 0 jpayne@69: } else { jpayne@69: ::scan $ss %d ss jpayne@69: } jpayne@69: set offset [expr { ( $hh * 60 + $mm ) * 60 + $ss }] jpayne@69: if { $s eq {-} } { jpayne@69: set offset [expr { - $offset }] jpayne@69: } jpayne@69: set TZData($timezone) [list [list $MINWIDE $offset -1 $timezone]] jpayne@69: jpayne@69: } elseif { [string index $timezone 0] eq {:} } { jpayne@69: # Convert using a time zone file jpayne@69: jpayne@69: if { jpayne@69: [catch { jpayne@69: LoadTimeZoneFile [string range $timezone 1 end] jpayne@69: }] && [catch { jpayne@69: LoadZoneinfoFile [string range $timezone 1 end] jpayne@69: }] jpayne@69: } then { jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK badTimeZone $timezone] \ jpayne@69: "time zone \"$timezone\" not found" jpayne@69: } jpayne@69: } elseif { ![catch {ParsePosixTimeZone $timezone} tzfields] } { jpayne@69: # This looks like a POSIX time zone - try to process it jpayne@69: jpayne@69: if { [catch {ProcessPosixTimeZone $tzfields} data opts] } { jpayne@69: if { [lindex [dict get $opts -errorcode] 0] eq {CLOCK} } { jpayne@69: dict unset opts -errorinfo jpayne@69: } jpayne@69: return -options $opts $data jpayne@69: } else { jpayne@69: set TZData($timezone) $data jpayne@69: } jpayne@69: jpayne@69: } else { jpayne@69: # We couldn't parse this as a POSIX time zone. Try again with a jpayne@69: # time zone file - this time without a colon jpayne@69: jpayne@69: if { [catch { LoadTimeZoneFile $timezone }] jpayne@69: && [catch { LoadZoneinfoFile $timezone } - opts] } { jpayne@69: dict unset opts -errorinfo jpayne@69: return -options $opts "time zone $timezone not found" jpayne@69: } jpayne@69: set TZData($timezone) $TZData(:$timezone) jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: return jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # GuessWindowsTimeZone -- jpayne@69: # jpayne@69: # Determines the system time zone on windows. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # None. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns a time zone specifier that corresponds to the system time zone jpayne@69: # information found in the Registry. jpayne@69: # jpayne@69: # Bugs: jpayne@69: # Fixed dates for DST change are unimplemented at present, because no jpayne@69: # time zone information supplied with Windows actually uses them! jpayne@69: # jpayne@69: # On a Windows system where neither $env(TCL_TZ) nor $env(TZ) is specified, jpayne@69: # GuessWindowsTimeZone looks in the Registry for the system time zone jpayne@69: # information. It then attempts to find an entry in WinZoneInfo for a time jpayne@69: # zone that uses the same rules. If it finds one, it returns it; otherwise, jpayne@69: # it constructs a Posix-style time zone string and returns that. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::GuessWindowsTimeZone {} { jpayne@69: variable WinZoneInfo jpayne@69: variable NoRegistry jpayne@69: variable TimeZoneBad jpayne@69: jpayne@69: if { [info exists NoRegistry] } { jpayne@69: return :localtime jpayne@69: } jpayne@69: jpayne@69: # Dredge time zone information out of the registry jpayne@69: jpayne@69: if { [catch { jpayne@69: set rpath HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\TimeZoneInformation jpayne@69: set data [list \ jpayne@69: [expr { -60 jpayne@69: * [registry get $rpath Bias] }] \ jpayne@69: [expr { -60 jpayne@69: * [registry get $rpath StandardBias] }] \ jpayne@69: [expr { -60 \ jpayne@69: * [registry get $rpath DaylightBias] }]] jpayne@69: set stdtzi [registry get $rpath StandardStart] jpayne@69: foreach ind {0 2 14 4 6 8 10 12} { jpayne@69: binary scan $stdtzi @${ind}s val jpayne@69: lappend data $val jpayne@69: } jpayne@69: set daytzi [registry get $rpath DaylightStart] jpayne@69: foreach ind {0 2 14 4 6 8 10 12} { jpayne@69: binary scan $daytzi @${ind}s val jpayne@69: lappend data $val jpayne@69: } jpayne@69: }] } { jpayne@69: # Missing values in the Registry - bail out jpayne@69: jpayne@69: return :localtime jpayne@69: } jpayne@69: jpayne@69: # Make up a Posix time zone specifier if we can't find one. Check here jpayne@69: # that the tzdata file exists, in case we're running in an environment jpayne@69: # (e.g. starpack) where tzdata is incomplete. (Bug 1237907) jpayne@69: jpayne@69: if { [dict exists $WinZoneInfo $data] } { jpayne@69: set tzname [dict get $WinZoneInfo $data] jpayne@69: if { ! [dict exists $TimeZoneBad $tzname] } { jpayne@69: dict set TimeZoneBad $tzname [catch {SetupTimeZone $tzname}] jpayne@69: } jpayne@69: } else { jpayne@69: set tzname {} jpayne@69: } jpayne@69: if { $tzname eq {} || [dict get $TimeZoneBad $tzname] } { jpayne@69: lassign $data \ jpayne@69: bias stdBias dstBias \ jpayne@69: stdYear stdMonth stdDayOfWeek stdDayOfMonth \ jpayne@69: stdHour stdMinute stdSecond stdMillisec \ jpayne@69: dstYear dstMonth dstDayOfWeek dstDayOfMonth \ jpayne@69: dstHour dstMinute dstSecond dstMillisec jpayne@69: set stdDelta [expr { $bias + $stdBias }] jpayne@69: set dstDelta [expr { $bias + $dstBias }] jpayne@69: if { $stdDelta <= 0 } { jpayne@69: set stdSignum + jpayne@69: set stdDelta [expr { - $stdDelta }] jpayne@69: set dispStdSignum - jpayne@69: } else { jpayne@69: set stdSignum - jpayne@69: set dispStdSignum + jpayne@69: } jpayne@69: set hh [::format %02d [expr { $stdDelta / 3600 }]] jpayne@69: set mm [::format %02d [expr { ($stdDelta / 60 ) % 60 }]] jpayne@69: set ss [::format %02d [expr { $stdDelta % 60 }]] jpayne@69: set tzname {} jpayne@69: append tzname < $dispStdSignum $hh $mm > $stdSignum $hh : $mm : $ss jpayne@69: if { $stdMonth >= 0 } { jpayne@69: if { $dstDelta <= 0 } { jpayne@69: set dstSignum + jpayne@69: set dstDelta [expr { - $dstDelta }] jpayne@69: set dispDstSignum - jpayne@69: } else { jpayne@69: set dstSignum - jpayne@69: set dispDstSignum + jpayne@69: } jpayne@69: set hh [::format %02d [expr { $dstDelta / 3600 }]] jpayne@69: set mm [::format %02d [expr { ($dstDelta / 60 ) % 60 }]] jpayne@69: set ss [::format %02d [expr { $dstDelta % 60 }]] jpayne@69: append tzname < $dispDstSignum $hh $mm > $dstSignum $hh : $mm : $ss jpayne@69: if { $dstYear == 0 } { jpayne@69: append tzname ,M $dstMonth . $dstDayOfMonth . $dstDayOfWeek jpayne@69: } else { jpayne@69: # I have not been able to find any locale on which Windows jpayne@69: # converts time zone on a fixed day of the year, hence don't jpayne@69: # know how to interpret the fields. If someone can inform me, jpayne@69: # I'd be glad to code it up. For right now, we bail out in jpayne@69: # such a case. jpayne@69: return :localtime jpayne@69: } jpayne@69: append tzname / [::format %02d $dstHour] \ jpayne@69: : [::format %02d $dstMinute] \ jpayne@69: : [::format %02d $dstSecond] jpayne@69: if { $stdYear == 0 } { jpayne@69: append tzname ,M $stdMonth . $stdDayOfMonth . $stdDayOfWeek jpayne@69: } else { jpayne@69: # I have not been able to find any locale on which Windows jpayne@69: # converts time zone on a fixed day of the year, hence don't jpayne@69: # know how to interpret the fields. If someone can inform me, jpayne@69: # I'd be glad to code it up. For right now, we bail out in jpayne@69: # such a case. jpayne@69: return :localtime jpayne@69: } jpayne@69: append tzname / [::format %02d $stdHour] \ jpayne@69: : [::format %02d $stdMinute] \ jpayne@69: : [::format %02d $stdSecond] jpayne@69: } jpayne@69: dict set WinZoneInfo $data $tzname jpayne@69: } jpayne@69: jpayne@69: return [dict get $WinZoneInfo $data] jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # LoadTimeZoneFile -- jpayne@69: # jpayne@69: # Load the data file that specifies the conversion between a jpayne@69: # given time zone and Greenwich. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # fileName -- Name of the file to load jpayne@69: # jpayne@69: # Results: jpayne@69: # None. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # TZData(:fileName) contains the time zone data jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::LoadTimeZoneFile { fileName } { jpayne@69: variable DataDir jpayne@69: variable TZData jpayne@69: jpayne@69: if { [info exists TZData($fileName)] } { jpayne@69: return jpayne@69: } jpayne@69: jpayne@69: # Since an unsafe interp uses the [clock] command in the parent, this code jpayne@69: # is security sensitive. Make sure that the path name cannot escape the jpayne@69: # given directory. jpayne@69: jpayne@69: if { ![regexp {^[[.-.][:alpha:]_]+(?:/[[.-.][:alpha:]_]+)*$} $fileName] } { jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK badTimeZone $:fileName] \ jpayne@69: "time zone \":$fileName\" not valid" jpayne@69: } jpayne@69: try { jpayne@69: source -encoding utf-8 [file join $DataDir $fileName] jpayne@69: } on error {} { jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK badTimeZone :$fileName] \ jpayne@69: "time zone \":$fileName\" not found" jpayne@69: } jpayne@69: return jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # LoadZoneinfoFile -- jpayne@69: # jpayne@69: # Loads a binary time zone information file in Olson format. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # fileName - Relative path name of the file to load. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns an empty result normally; returns an error if no Olson file jpayne@69: # was found or the file was malformed in some way. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # TZData(:fileName) contains the time zone data jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::LoadZoneinfoFile { fileName } { jpayne@69: variable ZoneinfoPaths jpayne@69: jpayne@69: # Since an unsafe interp uses the [clock] command in the parent, this code jpayne@69: # is security sensitive. Make sure that the path name cannot escape the jpayne@69: # given directory. jpayne@69: jpayne@69: if { ![regexp {^[[.-.][:alpha:]_]+(?:/[[.-.][:alpha:]_]+)*$} $fileName] } { jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK badTimeZone $:fileName] \ jpayne@69: "time zone \":$fileName\" not valid" jpayne@69: } jpayne@69: foreach d $ZoneinfoPaths { jpayne@69: set fname [file join $d $fileName] jpayne@69: if { [file readable $fname] && [file isfile $fname] } { jpayne@69: break jpayne@69: } jpayne@69: unset fname jpayne@69: } jpayne@69: ReadZoneinfoFile $fileName $fname jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ReadZoneinfoFile -- jpayne@69: # jpayne@69: # Loads a binary time zone information file in Olson format. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # fileName - Name of the time zone (relative path name of the jpayne@69: # file). jpayne@69: # fname - Absolute path name of the file. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns an empty result normally; returns an error if no Olson file jpayne@69: # was found or the file was malformed in some way. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # TZData(:fileName) contains the time zone data jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::ReadZoneinfoFile {fileName fname} { jpayne@69: variable MINWIDE jpayne@69: variable TZData jpayne@69: if { ![file exists $fname] } { jpayne@69: return -code error "$fileName not found" jpayne@69: } jpayne@69: jpayne@69: if { [file size $fname] > 262144 } { jpayne@69: return -code error "$fileName too big" jpayne@69: } jpayne@69: jpayne@69: # Suck in all the data from the file jpayne@69: jpayne@69: set f [open $fname r] jpayne@69: fconfigure $f -translation binary jpayne@69: set d [read $f] jpayne@69: close $f jpayne@69: jpayne@69: # The file begins with a magic number, sixteen reserved bytes, and then jpayne@69: # six 4-byte integers giving counts of fileds in the file. jpayne@69: jpayne@69: binary scan $d a4a1x15IIIIII \ jpayne@69: magic version nIsGMT nIsStd nLeap nTime nType nChar jpayne@69: set seek 44 jpayne@69: set ilen 4 jpayne@69: set iformat I jpayne@69: if { $magic != {TZif} } { jpayne@69: return -code error "$fileName not a time zone information file" jpayne@69: } jpayne@69: if { $nType > 255 } { jpayne@69: return -code error "$fileName contains too many time types" jpayne@69: } jpayne@69: # Accept only Posix-style zoneinfo. Sorry, 'leaps' bigots. jpayne@69: if { $nLeap != 0 } { jpayne@69: return -code error "$fileName contains leap seconds" jpayne@69: } jpayne@69: jpayne@69: # In a version 2 file, we use the second part of the file, which contains jpayne@69: # 64-bit transition times. jpayne@69: jpayne@69: if {$version eq "2"} { jpayne@69: set seek [expr { jpayne@69: 44 jpayne@69: + 5 * $nTime jpayne@69: + 6 * $nType jpayne@69: + 4 * $nLeap jpayne@69: + $nIsStd jpayne@69: + $nIsGMT jpayne@69: + $nChar jpayne@69: }] jpayne@69: binary scan $d @${seek}a4a1x15IIIIII \ jpayne@69: magic version nIsGMT nIsStd nLeap nTime nType nChar jpayne@69: if {$magic ne {TZif}} { jpayne@69: return -code error "seek address $seek miscomputed, magic = $magic" jpayne@69: } jpayne@69: set iformat W jpayne@69: set ilen 8 jpayne@69: incr seek 44 jpayne@69: } jpayne@69: jpayne@69: # Next come ${nTime} transition times, followed by ${nTime} time type jpayne@69: # codes. The type codes are unsigned 1-byte quantities. We insert an jpayne@69: # arbitrary start time in front of the transitions. jpayne@69: jpayne@69: binary scan $d @${seek}${iformat}${nTime}c${nTime} times tempCodes jpayne@69: incr seek [expr { ($ilen + 1) * $nTime }] jpayne@69: set times [linsert $times 0 $MINWIDE] jpayne@69: set codes {} jpayne@69: foreach c $tempCodes { jpayne@69: lappend codes [expr { $c & 0xFF }] jpayne@69: } jpayne@69: set codes [linsert $codes 0 0] jpayne@69: jpayne@69: # Next come ${nType} time type descriptions, each of which has an offset jpayne@69: # (seconds east of GMT), a DST indicator, and an index into the jpayne@69: # abbreviation text. jpayne@69: jpayne@69: for { set i 0 } { $i < $nType } { incr i } { jpayne@69: binary scan $d @${seek}Icc gmtOff isDst abbrInd jpayne@69: lappend types [list $gmtOff $isDst $abbrInd] jpayne@69: incr seek 6 jpayne@69: } jpayne@69: jpayne@69: # Next come $nChar characters of time zone name abbreviations, which are jpayne@69: # null-terminated. jpayne@69: # We build them up into a dictionary indexed by character index, because jpayne@69: # that's what's in the indices above. jpayne@69: jpayne@69: binary scan $d @${seek}a${nChar} abbrs jpayne@69: incr seek ${nChar} jpayne@69: set abbrList [split $abbrs \0] jpayne@69: set i 0 jpayne@69: set abbrevs {} jpayne@69: foreach a $abbrList { jpayne@69: for {set j 0} {$j <= [string length $a]} {incr j} { jpayne@69: dict set abbrevs $i [string range $a $j end] jpayne@69: incr i jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Package up a list of tuples, each of which contains transition time, jpayne@69: # seconds east of Greenwich, DST flag and time zone abbreviation. jpayne@69: jpayne@69: set r {} jpayne@69: set lastTime $MINWIDE jpayne@69: foreach t $times c $codes { jpayne@69: if { $t < $lastTime } { jpayne@69: return -code error "$fileName has times out of order" jpayne@69: } jpayne@69: set lastTime $t jpayne@69: lassign [lindex $types $c] gmtoff isDst abbrInd jpayne@69: set abbrev [dict get $abbrevs $abbrInd] jpayne@69: lappend r [list $t $gmtoff $isDst $abbrev] jpayne@69: } jpayne@69: jpayne@69: # In a version 2 file, there is also a POSIX-style time zone description jpayne@69: # at the very end of the file. To get to it, skip over nLeap leap second jpayne@69: # values (8 bytes each), jpayne@69: # nIsStd standard/DST indicators and nIsGMT UTC/local indicators. jpayne@69: jpayne@69: if {$version eq {2}} { jpayne@69: set seek [expr {$seek + 8 * $nLeap + $nIsStd + $nIsGMT + 1}] jpayne@69: set last [string first \n $d $seek] jpayne@69: set posix [string range $d $seek [expr {$last-1}]] jpayne@69: if {[llength $posix] > 0} { jpayne@69: set posixFields [ParsePosixTimeZone $posix] jpayne@69: foreach tuple [ProcessPosixTimeZone $posixFields] { jpayne@69: lassign $tuple t gmtoff isDst abbrev jpayne@69: if {$t > $lastTime} { jpayne@69: lappend r $tuple jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: set TZData(:$fileName) $r jpayne@69: jpayne@69: return jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ParsePosixTimeZone -- jpayne@69: # jpayne@69: # Parses the TZ environment variable in Posix form jpayne@69: # jpayne@69: # Parameters: jpayne@69: # tz Time zone specifier to be interpreted jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns a dictionary whose values contain the various pieces of the jpayne@69: # time zone specification. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: # Errors: jpayne@69: # Throws an error if the syntax of the time zone is incorrect. jpayne@69: # jpayne@69: # The following keys are present in the dictionary: jpayne@69: # stdName - Name of the time zone when Daylight Saving Time jpayne@69: # is not in effect. jpayne@69: # stdSignum - Sign (+, -, or empty) of the offset from Greenwich jpayne@69: # to the given (non-DST) time zone. + and the empty jpayne@69: # string denote zones west of Greenwich, - denotes east jpayne@69: # of Greenwich; this is contrary to the ISO convention jpayne@69: # but follows Posix. jpayne@69: # stdHours - Hours part of the offset from Greenwich to the given jpayne@69: # (non-DST) time zone. jpayne@69: # stdMinutes - Minutes part of the offset from Greenwich to the jpayne@69: # given (non-DST) time zone. Empty denotes zero. jpayne@69: # stdSeconds - Seconds part of the offset from Greenwich to the jpayne@69: # given (non-DST) time zone. Empty denotes zero. jpayne@69: # dstName - Name of the time zone when DST is in effect, or the jpayne@69: # empty string if the time zone does not observe Daylight jpayne@69: # Saving Time. jpayne@69: # dstSignum, dstHours, dstMinutes, dstSeconds - jpayne@69: # Fields corresponding to stdSignum, stdHours, stdMinutes, jpayne@69: # stdSeconds for the Daylight Saving Time version of the jpayne@69: # time zone. If dstHours is empty, it is presumed to be 1. jpayne@69: # startDayOfYear - The ordinal number of the day of the year on which jpayne@69: # Daylight Saving Time begins. If this field is jpayne@69: # empty, then DST begins on a given month-week-day, jpayne@69: # as below. jpayne@69: # startJ - The letter J, or an empty string. If a J is present in jpayne@69: # this field, then startDayOfYear does not count February 29 jpayne@69: # even in leap years. jpayne@69: # startMonth - The number of the month in which Daylight Saving Time jpayne@69: # begins, supplied if startDayOfYear is empty. If both jpayne@69: # startDayOfYear and startMonth are empty, then US rules jpayne@69: # are presumed. jpayne@69: # startWeekOfMonth - The number of the week in the month in which jpayne@69: # Daylight Saving Time begins, in the range 1-5. jpayne@69: # 5 denotes the last week of the month even in a jpayne@69: # 4-week month. jpayne@69: # startDayOfWeek - The number of the day of the week (Sunday=0, jpayne@69: # Saturday=6) on which Daylight Saving Time begins. jpayne@69: # startHours - The hours part of the time of day at which Daylight jpayne@69: # Saving Time begins. An empty string is presumed to be 2. jpayne@69: # startMinutes - The minutes part of the time of day at which DST begins. jpayne@69: # An empty string is presumed zero. jpayne@69: # startSeconds - The seconds part of the time of day at which DST begins. jpayne@69: # An empty string is presumed zero. jpayne@69: # endDayOfYear, endJ, endMonth, endWeekOfMonth, endDayOfWeek, jpayne@69: # endHours, endMinutes, endSeconds - jpayne@69: # Specify the end of DST in the same way that the start* fields jpayne@69: # specify the beginning of DST. jpayne@69: # jpayne@69: # This procedure serves only to break the time specifier into fields. No jpayne@69: # attempt is made to canonicalize the fields or supply default values. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::ParsePosixTimeZone { tz } { jpayne@69: if {[regexp -expanded -nocase -- { jpayne@69: ^ jpayne@69: # 1 - Standard time zone name jpayne@69: ([[:alpha:]]+ | <[-+[:alnum:]]+>) jpayne@69: # 2 - Standard time zone offset, signum jpayne@69: ([-+]?) jpayne@69: # 3 - Standard time zone offset, hours jpayne@69: ([[:digit:]]{1,2}) jpayne@69: (?: jpayne@69: # 4 - Standard time zone offset, minutes jpayne@69: : ([[:digit:]]{1,2}) jpayne@69: (?: jpayne@69: # 5 - Standard time zone offset, seconds jpayne@69: : ([[:digit:]]{1,2} ) jpayne@69: )? jpayne@69: )? jpayne@69: (?: jpayne@69: # 6 - DST time zone name jpayne@69: ([[:alpha:]]+ | <[-+[:alnum:]]+>) jpayne@69: (?: jpayne@69: (?: jpayne@69: # 7 - DST time zone offset, signum jpayne@69: ([-+]?) jpayne@69: # 8 - DST time zone offset, hours jpayne@69: ([[:digit:]]{1,2}) jpayne@69: (?: jpayne@69: # 9 - DST time zone offset, minutes jpayne@69: : ([[:digit:]]{1,2}) jpayne@69: (?: jpayne@69: # 10 - DST time zone offset, seconds jpayne@69: : ([[:digit:]]{1,2}) jpayne@69: )? jpayne@69: )? jpayne@69: )? jpayne@69: (?: jpayne@69: , jpayne@69: (?: jpayne@69: # 11 - Optional J in n and Jn form 12 - Day of year jpayne@69: ( J ? ) ( [[:digit:]]+ ) jpayne@69: | M jpayne@69: # 13 - Month number 14 - Week of month 15 - Day of week jpayne@69: ( [[:digit:]] + ) jpayne@69: [.] ( [[:digit:]] + ) jpayne@69: [.] ( [[:digit:]] + ) jpayne@69: ) jpayne@69: (?: jpayne@69: # 16 - Start time of DST - hours jpayne@69: / ( [[:digit:]]{1,2} ) jpayne@69: (?: jpayne@69: # 17 - Start time of DST - minutes jpayne@69: : ( [[:digit:]]{1,2} ) jpayne@69: (?: jpayne@69: # 18 - Start time of DST - seconds jpayne@69: : ( [[:digit:]]{1,2} ) jpayne@69: )? jpayne@69: )? jpayne@69: )? jpayne@69: , jpayne@69: (?: jpayne@69: # 19 - Optional J in n and Jn form 20 - Day of year jpayne@69: ( J ? ) ( [[:digit:]]+ ) jpayne@69: | M jpayne@69: # 21 - Month number 22 - Week of month 23 - Day of week jpayne@69: ( [[:digit:]] + ) jpayne@69: [.] ( [[:digit:]] + ) jpayne@69: [.] ( [[:digit:]] + ) jpayne@69: ) jpayne@69: (?: jpayne@69: # 24 - End time of DST - hours jpayne@69: / ( [[:digit:]]{1,2} ) jpayne@69: (?: jpayne@69: # 25 - End time of DST - minutes jpayne@69: : ( [[:digit:]]{1,2} ) jpayne@69: (?: jpayne@69: # 26 - End time of DST - seconds jpayne@69: : ( [[:digit:]]{1,2} ) jpayne@69: )? jpayne@69: )? jpayne@69: )? jpayne@69: )? jpayne@69: )? jpayne@69: )? jpayne@69: $ jpayne@69: } $tz -> x(stdName) x(stdSignum) x(stdHours) x(stdMinutes) x(stdSeconds) \ jpayne@69: x(dstName) x(dstSignum) x(dstHours) x(dstMinutes) x(dstSeconds) \ jpayne@69: x(startJ) x(startDayOfYear) \ jpayne@69: x(startMonth) x(startWeekOfMonth) x(startDayOfWeek) \ jpayne@69: x(startHours) x(startMinutes) x(startSeconds) \ jpayne@69: x(endJ) x(endDayOfYear) \ jpayne@69: x(endMonth) x(endWeekOfMonth) x(endDayOfWeek) \ jpayne@69: x(endHours) x(endMinutes) x(endSeconds)] } { jpayne@69: # it's a good timezone jpayne@69: jpayne@69: return [array get x] jpayne@69: } jpayne@69: jpayne@69: return -code error\ jpayne@69: -errorcode [list CLOCK badTimeZone $tz] \ jpayne@69: "unable to parse time zone specification \"$tz\"" jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ProcessPosixTimeZone -- jpayne@69: # jpayne@69: # Handle a Posix time zone after it's been broken out into fields. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # z - Dictionary returned from 'ParsePosixTimeZone' jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns time zone information for the 'TZData' array. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::ProcessPosixTimeZone { z } { jpayne@69: variable MINWIDE jpayne@69: variable TZData jpayne@69: jpayne@69: # Determine the standard time zone name and seconds east of Greenwich jpayne@69: jpayne@69: set stdName [dict get $z stdName] jpayne@69: if { [string index $stdName 0] eq {<} } { jpayne@69: set stdName [string range $stdName 1 end-1] jpayne@69: } jpayne@69: if { [dict get $z stdSignum] eq {-} } { jpayne@69: set stdSignum +1 jpayne@69: } else { jpayne@69: set stdSignum -1 jpayne@69: } jpayne@69: set stdHours [lindex [::scan [dict get $z stdHours] %d] 0] jpayne@69: if { [dict get $z stdMinutes] ne {} } { jpayne@69: set stdMinutes [lindex [::scan [dict get $z stdMinutes] %d] 0] jpayne@69: } else { jpayne@69: set stdMinutes 0 jpayne@69: } jpayne@69: if { [dict get $z stdSeconds] ne {} } { jpayne@69: set stdSeconds [lindex [::scan [dict get $z stdSeconds] %d] 0] jpayne@69: } else { jpayne@69: set stdSeconds 0 jpayne@69: } jpayne@69: set stdOffset [expr { jpayne@69: (($stdHours * 60 + $stdMinutes) * 60 + $stdSeconds) * $stdSignum jpayne@69: }] jpayne@69: set data [list [list $MINWIDE $stdOffset 0 $stdName]] jpayne@69: jpayne@69: # If there's no daylight zone, we're done jpayne@69: jpayne@69: set dstName [dict get $z dstName] jpayne@69: if { $dstName eq {} } { jpayne@69: return $data jpayne@69: } jpayne@69: if { [string index $dstName 0] eq {<} } { jpayne@69: set dstName [string range $dstName 1 end-1] jpayne@69: } jpayne@69: jpayne@69: # Determine the daylight name jpayne@69: jpayne@69: if { [dict get $z dstSignum] eq {-} } { jpayne@69: set dstSignum +1 jpayne@69: } else { jpayne@69: set dstSignum -1 jpayne@69: } jpayne@69: if { [dict get $z dstHours] eq {} } { jpayne@69: set dstOffset [expr { 3600 + $stdOffset }] jpayne@69: } else { jpayne@69: set dstHours [lindex [::scan [dict get $z dstHours] %d] 0] jpayne@69: if { [dict get $z dstMinutes] ne {} } { jpayne@69: set dstMinutes [lindex [::scan [dict get $z dstMinutes] %d] 0] jpayne@69: } else { jpayne@69: set dstMinutes 0 jpayne@69: } jpayne@69: if { [dict get $z dstSeconds] ne {} } { jpayne@69: set dstSeconds [lindex [::scan [dict get $z dstSeconds] %d] 0] jpayne@69: } else { jpayne@69: set dstSeconds 0 jpayne@69: } jpayne@69: set dstOffset [expr { jpayne@69: (($dstHours*60 + $dstMinutes) * 60 + $dstSeconds) * $dstSignum jpayne@69: }] jpayne@69: } jpayne@69: jpayne@69: # Fill in defaults for European or US DST rules jpayne@69: # US start time is the second Sunday in March jpayne@69: # EU start time is the last Sunday in March jpayne@69: # US end time is the first Sunday in November. jpayne@69: # EU end time is the last Sunday in October jpayne@69: jpayne@69: if { jpayne@69: [dict get $z startDayOfYear] eq {} jpayne@69: && [dict get $z startMonth] eq {} jpayne@69: } then { jpayne@69: if {($stdSignum * $stdHours>=0) && ($stdSignum * $stdHours<=12)} { jpayne@69: # EU jpayne@69: dict set z startWeekOfMonth 5 jpayne@69: if {$stdHours>2} { jpayne@69: dict set z startHours 2 jpayne@69: } else { jpayne@69: dict set z startHours [expr {$stdHours+1}] jpayne@69: } jpayne@69: } else { jpayne@69: # US jpayne@69: dict set z startWeekOfMonth 2 jpayne@69: dict set z startHours 2 jpayne@69: } jpayne@69: dict set z startMonth 3 jpayne@69: dict set z startDayOfWeek 0 jpayne@69: dict set z startMinutes 0 jpayne@69: dict set z startSeconds 0 jpayne@69: } jpayne@69: if { jpayne@69: [dict get $z endDayOfYear] eq {} jpayne@69: && [dict get $z endMonth] eq {} jpayne@69: } then { jpayne@69: if {($stdSignum * $stdHours>=0) && ($stdSignum * $stdHours<=12)} { jpayne@69: # EU jpayne@69: dict set z endMonth 10 jpayne@69: dict set z endWeekOfMonth 5 jpayne@69: if {$stdHours>2} { jpayne@69: dict set z endHours 3 jpayne@69: } else { jpayne@69: dict set z endHours [expr {$stdHours+2}] jpayne@69: } jpayne@69: } else { jpayne@69: # US jpayne@69: dict set z endMonth 11 jpayne@69: dict set z endWeekOfMonth 1 jpayne@69: dict set z endHours 2 jpayne@69: } jpayne@69: dict set z endDayOfWeek 0 jpayne@69: dict set z endMinutes 0 jpayne@69: dict set z endSeconds 0 jpayne@69: } jpayne@69: jpayne@69: # Put DST in effect in all years from 1916 to 2099. jpayne@69: jpayne@69: for { set y 1916 } { $y < 2100 } { incr y } { jpayne@69: set startTime [DeterminePosixDSTTime $z start $y] jpayne@69: incr startTime [expr { - wide($stdOffset) }] jpayne@69: set endTime [DeterminePosixDSTTime $z end $y] jpayne@69: incr endTime [expr { - wide($dstOffset) }] jpayne@69: if { $startTime < $endTime } { jpayne@69: lappend data \ jpayne@69: [list $startTime $dstOffset 1 $dstName] \ jpayne@69: [list $endTime $stdOffset 0 $stdName] jpayne@69: } else { jpayne@69: lappend data \ jpayne@69: [list $endTime $stdOffset 0 $stdName] \ jpayne@69: [list $startTime $dstOffset 1 $dstName] jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: return $data jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # DeterminePosixDSTTime -- jpayne@69: # jpayne@69: # Determines the time that Daylight Saving Time starts or ends from a jpayne@69: # Posix time zone specification. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # z - Time zone data returned from ParsePosixTimeZone. jpayne@69: # Missing fields are expected to be filled in with jpayne@69: # default values. jpayne@69: # bound - The word 'start' or 'end' jpayne@69: # y - The year for which the transition time is to be determined. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the transition time as a count of seconds from the epoch. The jpayne@69: # time is relative to the wall clock, not UTC. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::DeterminePosixDSTTime { z bound y } { jpayne@69: jpayne@69: variable FEB_28 jpayne@69: jpayne@69: # Determine the start or end day of DST jpayne@69: jpayne@69: set date [dict create era CE year $y] jpayne@69: set doy [dict get $z ${bound}DayOfYear] jpayne@69: if { $doy ne {} } { jpayne@69: jpayne@69: # Time was specified as a day of the year jpayne@69: jpayne@69: if { [dict get $z ${bound}J] ne {} jpayne@69: && [IsGregorianLeapYear $y] jpayne@69: && ( $doy > $FEB_28 ) } { jpayne@69: incr doy jpayne@69: } jpayne@69: dict set date dayOfYear $doy jpayne@69: set date [GetJulianDayFromEraYearDay $date[set date {}] 2361222] jpayne@69: } else { jpayne@69: # Time was specified as a day of the week within a month jpayne@69: jpayne@69: dict set date month [dict get $z ${bound}Month] jpayne@69: dict set date dayOfWeek [dict get $z ${bound}DayOfWeek] jpayne@69: set dowim [dict get $z ${bound}WeekOfMonth] jpayne@69: if { $dowim >= 5 } { jpayne@69: set dowim -1 jpayne@69: } jpayne@69: dict set date dayOfWeekInMonth $dowim jpayne@69: set date [GetJulianDayFromEraYearMonthWeekDay $date[set date {}] 2361222] jpayne@69: jpayne@69: } jpayne@69: jpayne@69: set jd [dict get $date julianDay] jpayne@69: set seconds [expr { jpayne@69: wide($jd) * wide(86400) - wide(210866803200) jpayne@69: }] jpayne@69: jpayne@69: set h [dict get $z ${bound}Hours] jpayne@69: if { $h eq {} } { jpayne@69: set h 2 jpayne@69: } else { jpayne@69: set h [lindex [::scan $h %d] 0] jpayne@69: } jpayne@69: set m [dict get $z ${bound}Minutes] jpayne@69: if { $m eq {} } { jpayne@69: set m 0 jpayne@69: } else { jpayne@69: set m [lindex [::scan $m %d] 0] jpayne@69: } jpayne@69: set s [dict get $z ${bound}Seconds] jpayne@69: if { $s eq {} } { jpayne@69: set s 0 jpayne@69: } else { jpayne@69: set s [lindex [::scan $s %d] 0] jpayne@69: } jpayne@69: set tod [expr { ( $h * 60 + $m ) * 60 + $s }] jpayne@69: return [expr { $seconds + $tod }] jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # GetLocaleEra -- jpayne@69: # jpayne@69: # Given local time expressed in seconds from the Posix epoch, jpayne@69: # determine localized era and year within the era. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date - Dictionary that must contain the keys, 'localSeconds', jpayne@69: # whose value is expressed as the appropriate local time; jpayne@69: # and 'year', whose value is the Gregorian year. jpayne@69: # etable - Value of the LOCALE_ERAS key in the message catalogue jpayne@69: # for the target locale. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the dictionary, augmented with the keys, 'localeEra' and jpayne@69: # 'localeYear'. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::GetLocaleEra { date etable } { jpayne@69: set index [BSearch $etable [dict get $date localSeconds]] jpayne@69: if { $index < 0} { jpayne@69: dict set date localeEra \ jpayne@69: [::format %02d [expr { [dict get $date year] / 100 }]] jpayne@69: dict set date localeYear [expr { jpayne@69: [dict get $date year] % 100 jpayne@69: }] jpayne@69: } else { jpayne@69: dict set date localeEra [lindex $etable $index 1] jpayne@69: dict set date localeYear [expr { jpayne@69: [dict get $date year] - [lindex $etable $index 2] jpayne@69: }] jpayne@69: } jpayne@69: return $date jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # GetJulianDayFromEraYearDay -- jpayne@69: # jpayne@69: # Given a year, month and day on the Gregorian calendar, determines jpayne@69: # the Julian Day Number beginning at noon on that date. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date -- A dictionary in which the 'era', 'year', and jpayne@69: # 'dayOfYear' slots are populated. The calendar in use jpayne@69: # is determined by the date itself relative to: jpayne@69: # changeover -- Julian day on which the Gregorian calendar was jpayne@69: # adopted in the current locale. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the given dictionary augmented with a 'julianDay' key whose jpayne@69: # value is the desired Julian Day Number, and a 'gregorian' key that jpayne@69: # specifies whether the calendar is Gregorian (1) or Julian (0). jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: # Bugs: jpayne@69: # This code needs to be moved to the C layer. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::GetJulianDayFromEraYearDay {date changeover} { jpayne@69: # Get absolute year number from the civil year jpayne@69: jpayne@69: switch -exact -- [dict get $date era] { jpayne@69: BCE { jpayne@69: set year [expr { 1 - [dict get $date year] }] jpayne@69: } jpayne@69: CE { jpayne@69: set year [dict get $date year] jpayne@69: } jpayne@69: } jpayne@69: set ym1 [expr { $year - 1 }] jpayne@69: jpayne@69: # Try the Gregorian calendar first. jpayne@69: jpayne@69: dict set date gregorian 1 jpayne@69: set jd [expr { jpayne@69: 1721425 jpayne@69: + [dict get $date dayOfYear] jpayne@69: + ( 365 * $ym1 ) jpayne@69: + ( $ym1 / 4 ) jpayne@69: - ( $ym1 / 100 ) jpayne@69: + ( $ym1 / 400 ) jpayne@69: }] jpayne@69: jpayne@69: # If the date is before the Gregorian change, use the Julian calendar. jpayne@69: jpayne@69: if { $jd < $changeover } { jpayne@69: dict set date gregorian 0 jpayne@69: set jd [expr { jpayne@69: 1721423 jpayne@69: + [dict get $date dayOfYear] jpayne@69: + ( 365 * $ym1 ) jpayne@69: + ( $ym1 / 4 ) jpayne@69: }] jpayne@69: } jpayne@69: jpayne@69: dict set date julianDay $jd jpayne@69: return $date jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # GetJulianDayFromEraYearMonthWeekDay -- jpayne@69: # jpayne@69: # Determines the Julian Day number corresponding to the nth given jpayne@69: # day-of-the-week in a given month. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date - Dictionary containing the keys, 'era', 'year', 'month' jpayne@69: # 'weekOfMonth', 'dayOfWeek', and 'dayOfWeekInMonth'. jpayne@69: # changeover - Julian Day of adoption of the Gregorian calendar jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the given dictionary, augmented with a 'julianDay' key. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: # Bugs: jpayne@69: # This code needs to be moved to the C layer. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::GetJulianDayFromEraYearMonthWeekDay {date changeover} { jpayne@69: # Come up with a reference day; either the zeroeth day of the given month jpayne@69: # (dayOfWeekInMonth >= 0) or the seventh day of the following month jpayne@69: # (dayOfWeekInMonth < 0) jpayne@69: jpayne@69: set date2 $date jpayne@69: set week [dict get $date dayOfWeekInMonth] jpayne@69: if { $week >= 0 } { jpayne@69: dict set date2 dayOfMonth 0 jpayne@69: } else { jpayne@69: dict incr date2 month jpayne@69: dict set date2 dayOfMonth 7 jpayne@69: } jpayne@69: set date2 [GetJulianDayFromEraYearMonthDay $date2[set date2 {}] \ jpayne@69: $changeover] jpayne@69: set wd0 [WeekdayOnOrBefore [dict get $date dayOfWeek] \ jpayne@69: [dict get $date2 julianDay]] jpayne@69: dict set date julianDay [expr { $wd0 + 7 * $week }] jpayne@69: return $date jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # IsGregorianLeapYear -- jpayne@69: # jpayne@69: # Determines whether a given date represents a leap year in the jpayne@69: # Gregorian calendar. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # date -- The date to test. The fields, 'era', 'year' and 'gregorian' jpayne@69: # must be set. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns 1 if the year is a leap year, 0 otherwise. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::IsGregorianLeapYear { date } { jpayne@69: switch -exact -- [dict get $date era] { jpayne@69: BCE { jpayne@69: set year [expr { 1 - [dict get $date year]}] jpayne@69: } jpayne@69: CE { jpayne@69: set year [dict get $date year] jpayne@69: } jpayne@69: } jpayne@69: if { $year % 4 != 0 } { jpayne@69: return 0 jpayne@69: } elseif { ![dict get $date gregorian] } { jpayne@69: return 1 jpayne@69: } elseif { $year % 400 == 0 } { jpayne@69: return 1 jpayne@69: } elseif { $year % 100 == 0 } { jpayne@69: return 0 jpayne@69: } else { jpayne@69: return 1 jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # WeekdayOnOrBefore -- jpayne@69: # jpayne@69: # Determine the nearest day of week (given by the 'weekday' parameter, jpayne@69: # Sunday==0) on or before a given Julian Day. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # weekday -- Day of the week jpayne@69: # j -- Julian Day number jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the Julian Day Number of the desired date. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::WeekdayOnOrBefore { weekday j } { jpayne@69: set k [expr { ( $weekday + 6 ) % 7 }] jpayne@69: return [expr { $j - ( $j - $k ) % 7 }] jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # BSearch -- jpayne@69: # jpayne@69: # Service procedure that does binary search in several places inside the jpayne@69: # 'clock' command. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # list - List of lists, sorted in ascending order by the jpayne@69: # first elements jpayne@69: # key - Value to search for jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the index of the greatest element in $list that is less than jpayne@69: # or equal to $key. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::BSearch { list key } { jpayne@69: if {[llength $list] == 0} { jpayne@69: return -1 jpayne@69: } jpayne@69: if { $key < [lindex $list 0 0] } { jpayne@69: return -1 jpayne@69: } jpayne@69: jpayne@69: set l 0 jpayne@69: set u [expr { [llength $list] - 1 }] jpayne@69: jpayne@69: while { $l < $u } { jpayne@69: # At this point, we know that jpayne@69: # $k >= [lindex $list $l 0] jpayne@69: # Either $u == [llength $list] or else $k < [lindex $list $u+1 0] jpayne@69: # We find the midpoint of the interval {l,u} rounded UP, compare jpayne@69: # against it, and set l or u to maintain the invariant. Note that the jpayne@69: # interval shrinks at each step, guaranteeing convergence. jpayne@69: jpayne@69: set m [expr { ( $l + $u + 1 ) / 2 }] jpayne@69: if { $key >= [lindex $list $m 0] } { jpayne@69: set l $m jpayne@69: } else { jpayne@69: set u [expr { $m - 1 }] jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: return $l jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # clock add -- jpayne@69: # jpayne@69: # Adds an offset to a given time. jpayne@69: # jpayne@69: # Syntax: jpayne@69: # clock add clockval ?count unit?... ?-option value? jpayne@69: # jpayne@69: # Parameters: jpayne@69: # clockval -- Starting time value jpayne@69: # count -- Amount of a unit of time to add jpayne@69: # unit -- Unit of time to add, must be one of: jpayne@69: # years year months month weeks week jpayne@69: # days day hours hour minutes minute jpayne@69: # seconds second jpayne@69: # jpayne@69: # Options: jpayne@69: # -gmt BOOLEAN jpayne@69: # (Deprecated) Flag synonymous with '-timezone :GMT' jpayne@69: # -timezone ZONE jpayne@69: # Name of the time zone in which calculations are to be done. jpayne@69: # -locale NAME jpayne@69: # Name of the locale in which calculations are to be done. jpayne@69: # Used to determine the Gregorian change date. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the given time adjusted by the given offset(s) in jpayne@69: # order. jpayne@69: # jpayne@69: # Notes: jpayne@69: # It is possible that adding a number of months or years will adjust the jpayne@69: # day of the month as well. For instance, the time at one month after jpayne@69: # 31 January is either 28 or 29 February, because February has fewer jpayne@69: # than 31 days. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::add { clockval args } { jpayne@69: if { [llength $args] % 2 != 0 } { jpayne@69: set cmdName "clock add" jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK wrongNumArgs] \ jpayne@69: "wrong \# args: should be\ jpayne@69: \"$cmdName clockval ?number units?...\ jpayne@69: ?-gmt boolean? ?-locale LOCALE? ?-timezone ZONE?\"" jpayne@69: } jpayne@69: if { [catch { expr {wide($clockval)} } result] } { jpayne@69: return -code error $result jpayne@69: } jpayne@69: jpayne@69: set offsets {} jpayne@69: set gmt 0 jpayne@69: set locale c jpayne@69: set timezone [GetSystemTimeZone] jpayne@69: jpayne@69: foreach { a b } $args { jpayne@69: if { [string is integer -strict $a] } { jpayne@69: lappend offsets $a $b jpayne@69: } else { jpayne@69: switch -exact -- $a { jpayne@69: -g - -gm - -gmt { jpayne@69: set gmt $b jpayne@69: } jpayne@69: -l - -lo - -loc - -loca - -local - -locale { jpayne@69: set locale [string tolower $b] jpayne@69: } jpayne@69: -t - -ti - -tim - -time - -timez - -timezo - -timezon - jpayne@69: -timezone { jpayne@69: set timezone $b jpayne@69: } jpayne@69: default { jpayne@69: throw [list CLOCK badOption $a] \ jpayne@69: "bad option \"$a\",\ jpayne@69: must be -gmt, -locale or -timezone" jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # Check options for validity jpayne@69: jpayne@69: if { [info exists saw(-gmt)] && [info exists saw(-timezone)] } { jpayne@69: return -code error \ jpayne@69: -errorcode [list CLOCK gmtWithTimezone] \ jpayne@69: "cannot use -gmt and -timezone in same call" jpayne@69: } jpayne@69: if { [catch { expr { wide($clockval) } } result] } { jpayne@69: return -code error "expected integer but got \"$clockval\"" jpayne@69: } jpayne@69: if { ![string is boolean -strict $gmt] } { jpayne@69: return -code error "expected boolean value but got \"$gmt\"" jpayne@69: } elseif { $gmt } { jpayne@69: set timezone :GMT jpayne@69: } jpayne@69: jpayne@69: EnterLocale $locale jpayne@69: jpayne@69: set changeover [mc GREGORIAN_CHANGE_DATE] jpayne@69: jpayne@69: if {[catch {SetupTimeZone $timezone} retval opts]} { jpayne@69: dict unset opts -errorinfo jpayne@69: return -options $opts $retval jpayne@69: } jpayne@69: jpayne@69: try { jpayne@69: foreach { quantity unit } $offsets { jpayne@69: switch -exact -- $unit { jpayne@69: years - year { jpayne@69: set clockval [AddMonths [expr { 12 * $quantity }] \ jpayne@69: $clockval $timezone $changeover] jpayne@69: } jpayne@69: months - month { jpayne@69: set clockval [AddMonths $quantity $clockval $timezone \ jpayne@69: $changeover] jpayne@69: } jpayne@69: jpayne@69: weeks - week { jpayne@69: set clockval [AddDays [expr { 7 * $quantity }] \ jpayne@69: $clockval $timezone $changeover] jpayne@69: } jpayne@69: days - day { jpayne@69: set clockval [AddDays $quantity $clockval $timezone \ jpayne@69: $changeover] jpayne@69: } jpayne@69: jpayne@69: hours - hour { jpayne@69: set clockval [expr { 3600 * $quantity + $clockval }] jpayne@69: } jpayne@69: minutes - minute { jpayne@69: set clockval [expr { 60 * $quantity + $clockval }] jpayne@69: } jpayne@69: seconds - second { jpayne@69: set clockval [expr { $quantity + $clockval }] jpayne@69: } jpayne@69: jpayne@69: default { jpayne@69: throw [list CLOCK badUnit $unit] \ jpayne@69: "unknown unit \"$unit\", must be \ jpayne@69: years, months, weeks, days, hours, minutes or seconds" jpayne@69: } jpayne@69: } jpayne@69: } jpayne@69: return $clockval jpayne@69: } trap CLOCK {result opts} { jpayne@69: # Conceal the innards of [clock] when it's an expected error jpayne@69: dict unset opts -errorinfo jpayne@69: return -options $opts $result jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # AddMonths -- jpayne@69: # jpayne@69: # Add a given number of months to a given clock value in a given jpayne@69: # time zone. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # months - Number of months to add (may be negative) jpayne@69: # clockval - Seconds since the epoch before the operation jpayne@69: # timezone - Time zone in which the operation is to be performed jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the new clock value as a number of seconds since jpayne@69: # the epoch. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::AddMonths { months clockval timezone changeover } { jpayne@69: variable DaysInRomanMonthInCommonYear jpayne@69: variable DaysInRomanMonthInLeapYear jpayne@69: variable TZData jpayne@69: jpayne@69: # Convert the time to year, month, day, and fraction of day. jpayne@69: jpayne@69: set date [GetDateFields $clockval $TZData($timezone) $changeover] jpayne@69: dict set date secondOfDay [expr { jpayne@69: [dict get $date localSeconds] % 86400 jpayne@69: }] jpayne@69: dict set date tzName $timezone jpayne@69: jpayne@69: # Add the requisite number of months jpayne@69: jpayne@69: set m [dict get $date month] jpayne@69: incr m $months jpayne@69: incr m -1 jpayne@69: set delta [expr { $m / 12 }] jpayne@69: set mm [expr { $m % 12 }] jpayne@69: dict set date month [expr { $mm + 1 }] jpayne@69: dict incr date year $delta jpayne@69: jpayne@69: # If the date doesn't exist in the current month, repair it jpayne@69: jpayne@69: if { [IsGregorianLeapYear $date] } { jpayne@69: set hath [lindex $DaysInRomanMonthInLeapYear $mm] jpayne@69: } else { jpayne@69: set hath [lindex $DaysInRomanMonthInCommonYear $mm] jpayne@69: } jpayne@69: if { [dict get $date dayOfMonth] > $hath } { jpayne@69: dict set date dayOfMonth $hath jpayne@69: } jpayne@69: jpayne@69: # Reconvert to a number of seconds jpayne@69: jpayne@69: set date [GetJulianDayFromEraYearMonthDay \ jpayne@69: $date[set date {}]\ jpayne@69: $changeover] jpayne@69: dict set date localSeconds [expr { jpayne@69: -210866803200 jpayne@69: + ( 86400 * wide([dict get $date julianDay]) ) jpayne@69: + [dict get $date secondOfDay] jpayne@69: }] jpayne@69: set date [ConvertLocalToUTC $date[set date {}] $TZData($timezone) \ jpayne@69: $changeover] jpayne@69: jpayne@69: return [dict get $date seconds] jpayne@69: jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # AddDays -- jpayne@69: # jpayne@69: # Add a given number of days to a given clock value in a given time jpayne@69: # zone. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # days - Number of days to add (may be negative) jpayne@69: # clockval - Seconds since the epoch before the operation jpayne@69: # timezone - Time zone in which the operation is to be performed jpayne@69: # changeover - Julian Day on which the Gregorian calendar was adopted jpayne@69: # in the target locale. jpayne@69: # jpayne@69: # Results: jpayne@69: # Returns the new clock value as a number of seconds since the epoch. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # None. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::AddDays { days clockval timezone changeover } { jpayne@69: variable TZData jpayne@69: jpayne@69: # Convert the time to Julian Day jpayne@69: jpayne@69: set date [GetDateFields $clockval $TZData($timezone) $changeover] jpayne@69: dict set date secondOfDay [expr { jpayne@69: [dict get $date localSeconds] % 86400 jpayne@69: }] jpayne@69: dict set date tzName $timezone jpayne@69: jpayne@69: # Add the requisite number of days jpayne@69: jpayne@69: dict incr date julianDay $days jpayne@69: jpayne@69: # Reconvert to a number of seconds jpayne@69: jpayne@69: dict set date localSeconds [expr { jpayne@69: -210866803200 jpayne@69: + ( 86400 * wide([dict get $date julianDay]) ) jpayne@69: + [dict get $date secondOfDay] jpayne@69: }] jpayne@69: set date [ConvertLocalToUTC $date[set date {}] $TZData($timezone) \ jpayne@69: $changeover] jpayne@69: jpayne@69: return [dict get $date seconds] jpayne@69: jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ChangeCurrentLocale -- jpayne@69: # jpayne@69: # The global locale was changed within msgcat. jpayne@69: # Clears the buffered parse functions of the current locale. jpayne@69: # jpayne@69: # Parameters: jpayne@69: # loclist (ignored) jpayne@69: # jpayne@69: # Results: jpayne@69: # None. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # Buffered parse functions are cleared. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::ChangeCurrentLocale {args} { jpayne@69: variable FormatProc jpayne@69: variable LocaleNumeralCache jpayne@69: variable CachedSystemTimeZone jpayne@69: variable TimeZoneBad jpayne@69: jpayne@69: foreach p [info procs [namespace current]::scanproc'*'current] { jpayne@69: rename $p {} jpayne@69: } jpayne@69: foreach p [info procs [namespace current]::formatproc'*'current] { jpayne@69: rename $p {} jpayne@69: } jpayne@69: jpayne@69: catch {array unset FormatProc *'current} jpayne@69: set LocaleNumeralCache {} jpayne@69: } jpayne@69: jpayne@69: #---------------------------------------------------------------------- jpayne@69: # jpayne@69: # ClearCaches -- jpayne@69: # jpayne@69: # Clears all caches to reclaim the memory used in [clock] jpayne@69: # jpayne@69: # Parameters: jpayne@69: # None. jpayne@69: # jpayne@69: # Results: jpayne@69: # None. jpayne@69: # jpayne@69: # Side effects: jpayne@69: # Caches are cleared. jpayne@69: # jpayne@69: #---------------------------------------------------------------------- jpayne@69: jpayne@69: proc ::tcl::clock::ClearCaches {} { jpayne@69: variable FormatProc jpayne@69: variable LocaleNumeralCache jpayne@69: variable CachedSystemTimeZone jpayne@69: variable TimeZoneBad jpayne@69: jpayne@69: foreach p [info procs [namespace current]::scanproc'*] { jpayne@69: rename $p {} jpayne@69: } jpayne@69: foreach p [info procs [namespace current]::formatproc'*] { jpayne@69: rename $p {} jpayne@69: } jpayne@69: jpayne@69: catch {unset FormatProc} jpayne@69: set LocaleNumeralCache {} jpayne@69: catch {unset CachedSystemTimeZone} jpayne@69: set TimeZoneBad {} jpayne@69: InitTZData jpayne@69: }