| 1 |
#---------------------------------------------------------------------- |
| 2 |
# |
| 3 |
# clock.tcl -- |
| 4 |
# |
| 5 |
# This file implements the portions of the [clock] ensemble that are |
| 6 |
# coded in Tcl. Refer to the users' manual to see the description of |
| 7 |
# the [clock] command and its subcommands. |
| 8 |
# |
| 9 |
# |
| 10 |
#---------------------------------------------------------------------- |
| 11 |
# |
| 12 |
# Copyright (c) 2004-2007 Kevin B. Kenny |
| 13 |
# See the file "license.terms" for information on usage and redistribution |
| 14 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 15 |
# |
| 16 |
#---------------------------------------------------------------------- |
| 17 |
|
| 18 |
# We must have message catalogs that support the root locale, and we need |
| 19 |
# access to the Registry on Windows systems. |
| 20 |
|
| 21 |
uplevel \#0 { |
| 22 |
package require msgcat 1.6 |
| 23 |
if { $::tcl_platform(platform) eq {windows} } { |
| 24 |
if { [catch { package require registry 1.1 }] } { |
| 25 |
namespace eval ::tcl::clock [list variable NoRegistry {}] |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
# Put the library directory into the namespace for the ensemble so that the |
| 31 |
# library code can find message catalogs and time zone definition files. |
| 32 |
|
| 33 |
namespace eval ::tcl::clock \ |
| 34 |
[list variable LibDir [info library]] |
| 35 |
|
| 36 |
#---------------------------------------------------------------------- |
| 37 |
# |
| 38 |
# clock -- |
| 39 |
# |
| 40 |
# Manipulate times. |
| 41 |
# |
| 42 |
# The 'clock' command manipulates time. Refer to the user documentation for |
| 43 |
# the available subcommands and what they do. |
| 44 |
# |
| 45 |
#---------------------------------------------------------------------- |
| 46 |
|
| 47 |
namespace eval ::tcl::clock { |
| 48 |
|
| 49 |
# Export the subcommands |
| 50 |
|
| 51 |
namespace export format |
| 52 |
namespace export clicks |
| 53 |
namespace export microseconds |
| 54 |
namespace export milliseconds |
| 55 |
namespace export scan |
| 56 |
namespace export seconds |
| 57 |
namespace export add |
| 58 |
|
| 59 |
# Import the message catalog commands that we use. |
| 60 |
|
| 61 |
namespace import ::msgcat::mcload |
| 62 |
namespace import ::msgcat::mclocale |
| 63 |
namespace import ::msgcat::mc |
| 64 |
namespace import ::msgcat::mcpackagelocale |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
#---------------------------------------------------------------------- |
| 69 |
# |
| 70 |
# ::tcl::clock::Initialize -- |
| 71 |
# |
| 72 |
# Finish initializing the 'clock' subsystem |
| 73 |
# |
| 74 |
# Results: |
| 75 |
# None. |
| 76 |
# |
| 77 |
# Side effects: |
| 78 |
# Namespace variable in the 'clock' subsystem are initialized. |
| 79 |
# |
| 80 |
# The '::tcl::clock::Initialize' procedure initializes the namespace variables |
| 81 |
# and root locale message catalog for the 'clock' subsystem. It is broken |
| 82 |
# into a procedure rather than simply evaluated as a script so that it will be |
| 83 |
# able to use local variables, avoiding the dangers of 'creative writing' as |
| 84 |
# in Bug 1185933. |
| 85 |
# |
| 86 |
#---------------------------------------------------------------------- |
| 87 |
|
| 88 |
proc ::tcl::clock::Initialize {} { |
| 89 |
|
| 90 |
rename ::tcl::clock::Initialize {} |
| 91 |
|
| 92 |
variable LibDir |
| 93 |
|
| 94 |
# Define the Greenwich time zone |
| 95 |
|
| 96 |
proc InitTZData {} { |
| 97 |
variable TZData |
| 98 |
array unset TZData |
| 99 |
set TZData(:Etc/GMT) { |
| 100 |
{-9223372036854775808 0 0 GMT} |
| 101 |
} |
| 102 |
set TZData(:GMT) $TZData(:Etc/GMT) |
| 103 |
set TZData(:Etc/UTC) { |
| 104 |
{-9223372036854775808 0 0 UTC} |
| 105 |
} |
| 106 |
set TZData(:UTC) $TZData(:Etc/UTC) |
| 107 |
set TZData(:localtime) {} |
| 108 |
} |
| 109 |
InitTZData |
| 110 |
|
| 111 |
mcpackagelocale set {} |
| 112 |
::msgcat::mcpackageconfig set mcfolder [file join $LibDir msgs] |
| 113 |
::msgcat::mcpackageconfig set unknowncmd "" |
| 114 |
::msgcat::mcpackageconfig set changecmd ChangeCurrentLocale |
| 115 |
|
| 116 |
# Define the message catalog for the root locale. |
| 117 |
|
| 118 |
::msgcat::mcmset {} { |
| 119 |
AM {am} |
| 120 |
BCE {B.C.E.} |
| 121 |
CE {C.E.} |
| 122 |
DATE_FORMAT {%m/%d/%Y} |
| 123 |
DATE_TIME_FORMAT {%a %b %e %H:%M:%S %Y} |
| 124 |
DAYS_OF_WEEK_ABBREV { |
| 125 |
Sun Mon Tue Wed Thu Fri Sat |
| 126 |
} |
| 127 |
DAYS_OF_WEEK_FULL { |
| 128 |
Sunday Monday Tuesday Wednesday Thursday Friday Saturday |
| 129 |
} |
| 130 |
GREGORIAN_CHANGE_DATE 2299161 |
| 131 |
LOCALE_DATE_FORMAT {%m/%d/%Y} |
| 132 |
LOCALE_DATE_TIME_FORMAT {%a %b %e %H:%M:%S %Y} |
| 133 |
LOCALE_ERAS {} |
| 134 |
LOCALE_NUMERALS { |
| 135 |
00 01 02 03 04 05 06 07 08 09 |
| 136 |
10 11 12 13 14 15 16 17 18 19 |
| 137 |
20 21 22 23 24 25 26 27 28 29 |
| 138 |
30 31 32 33 34 35 36 37 38 39 |
| 139 |
40 41 42 43 44 45 46 47 48 49 |
| 140 |
50 51 52 53 54 55 56 57 58 59 |
| 141 |
60 61 62 63 64 65 66 67 68 69 |
| 142 |
70 71 72 73 74 75 76 77 78 79 |
| 143 |
80 81 82 83 84 85 86 87 88 89 |
| 144 |
90 91 92 93 94 95 96 97 98 99 |
| 145 |
} |
| 146 |
LOCALE_TIME_FORMAT {%H:%M:%S} |
| 147 |
LOCALE_YEAR_FORMAT {%EC%Ey} |
| 148 |
MONTHS_ABBREV { |
| 149 |
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec |
| 150 |
} |
| 151 |
MONTHS_FULL { |
| 152 |
January February March |
| 153 |
April May June |
| 154 |
July August September |
| 155 |
October November December |
| 156 |
} |
| 157 |
PM {pm} |
| 158 |
TIME_FORMAT {%H:%M:%S} |
| 159 |
TIME_FORMAT_12 {%I:%M:%S %P} |
| 160 |
TIME_FORMAT_24 {%H:%M} |
| 161 |
TIME_FORMAT_24_SECS {%H:%M:%S} |
| 162 |
} |
| 163 |
|
| 164 |
# Define a few Gregorian change dates for other locales. In most cases |
| 165 |
# the change date follows a language, because a nation's colonies changed |
| 166 |
# at the same time as the nation itself. In many cases, different |
| 167 |
# national boundaries existed; the dominating rule is to follow the |
| 168 |
# nation's capital. |
| 169 |
|
| 170 |
# Italy, Spain, Portugal, Poland |
| 171 |
|
| 172 |
::msgcat::mcset it GREGORIAN_CHANGE_DATE 2299161 |
| 173 |
::msgcat::mcset es GREGORIAN_CHANGE_DATE 2299161 |
| 174 |
::msgcat::mcset pt GREGORIAN_CHANGE_DATE 2299161 |
| 175 |
::msgcat::mcset pl GREGORIAN_CHANGE_DATE 2299161 |
| 176 |
|
| 177 |
# France, Austria |
| 178 |
|
| 179 |
::msgcat::mcset fr GREGORIAN_CHANGE_DATE 2299227 |
| 180 |
|
| 181 |
# For Belgium, we follow Southern Netherlands; Liege Diocese changed |
| 182 |
# several weeks later. |
| 183 |
|
| 184 |
::msgcat::mcset fr_BE GREGORIAN_CHANGE_DATE 2299238 |
| 185 |
::msgcat::mcset nl_BE GREGORIAN_CHANGE_DATE 2299238 |
| 186 |
|
| 187 |
# Austria |
| 188 |
|
| 189 |
::msgcat::mcset de_AT GREGORIAN_CHANGE_DATE 2299527 |
| 190 |
|
| 191 |
# Hungary |
| 192 |
|
| 193 |
::msgcat::mcset hu GREGORIAN_CHANGE_DATE 2301004 |
| 194 |
|
| 195 |
# Germany, Norway, Denmark (Catholic Germany changed earlier) |
| 196 |
|
| 197 |
::msgcat::mcset de_DE GREGORIAN_CHANGE_DATE 2342032 |
| 198 |
::msgcat::mcset nb GREGORIAN_CHANGE_DATE 2342032 |
| 199 |
::msgcat::mcset nn GREGORIAN_CHANGE_DATE 2342032 |
| 200 |
::msgcat::mcset no GREGORIAN_CHANGE_DATE 2342032 |
| 201 |
::msgcat::mcset da GREGORIAN_CHANGE_DATE 2342032 |
| 202 |
|
| 203 |
# Holland (Brabant, Gelderland, Flanders, Friesland, etc. changed at |
| 204 |
# various times) |
| 205 |
|
| 206 |
::msgcat::mcset nl GREGORIAN_CHANGE_DATE 2342165 |
| 207 |
|
| 208 |
# Protestant Switzerland (Catholic cantons changed earlier) |
| 209 |
|
| 210 |
::msgcat::mcset fr_CH GREGORIAN_CHANGE_DATE 2361342 |
| 211 |
::msgcat::mcset it_CH GREGORIAN_CHANGE_DATE 2361342 |
| 212 |
::msgcat::mcset de_CH GREGORIAN_CHANGE_DATE 2361342 |
| 213 |
|
| 214 |
# English speaking countries |
| 215 |
|
| 216 |
::msgcat::mcset en GREGORIAN_CHANGE_DATE 2361222 |
| 217 |
|
| 218 |
# Sweden (had several changes onto and off of the Gregorian calendar) |
| 219 |
|
| 220 |
::msgcat::mcset sv GREGORIAN_CHANGE_DATE 2361390 |
| 221 |
|
| 222 |
# Russia |
| 223 |
|
| 224 |
::msgcat::mcset ru GREGORIAN_CHANGE_DATE 2421639 |
| 225 |
|
| 226 |
# Romania (Transylvania changed earlier - perhaps de_RO should show the |
| 227 |
# earlier date?) |
| 228 |
|
| 229 |
::msgcat::mcset ro GREGORIAN_CHANGE_DATE 2422063 |
| 230 |
|
| 231 |
# Greece |
| 232 |
|
| 233 |
::msgcat::mcset el GREGORIAN_CHANGE_DATE 2423480 |
| 234 |
|
| 235 |
#------------------------------------------------------------------ |
| 236 |
# |
| 237 |
# CONSTANTS |
| 238 |
# |
| 239 |
#------------------------------------------------------------------ |
| 240 |
|
| 241 |
# Paths at which binary time zone data for the Olson libraries are known |
| 242 |
# to reside on various operating systems |
| 243 |
|
| 244 |
variable ZoneinfoPaths {} |
| 245 |
foreach path { |
| 246 |
/usr/share/zoneinfo |
| 247 |
/usr/share/lib/zoneinfo |
| 248 |
/usr/lib/zoneinfo |
| 249 |
/usr/local/etc/zoneinfo |
| 250 |
} { |
| 251 |
if { [file isdirectory $path] } { |
| 252 |
lappend ZoneinfoPaths $path |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
# Define the directories for time zone data and message catalogs. |
| 257 |
|
| 258 |
variable DataDir [file join $LibDir tzdata] |
| 259 |
|
| 260 |
# Number of days in the months, in common years and leap years. |
| 261 |
|
| 262 |
variable DaysInRomanMonthInCommonYear \ |
| 263 |
{ 31 28 31 30 31 30 31 31 30 31 30 31 } |
| 264 |
variable DaysInRomanMonthInLeapYear \ |
| 265 |
{ 31 29 31 30 31 30 31 31 30 31 30 31 } |
| 266 |
variable DaysInPriorMonthsInCommonYear [list 0] |
| 267 |
variable DaysInPriorMonthsInLeapYear [list 0] |
| 268 |
set i 0 |
| 269 |
foreach j $DaysInRomanMonthInCommonYear { |
| 270 |
lappend DaysInPriorMonthsInCommonYear [incr i $j] |
| 271 |
} |
| 272 |
set i 0 |
| 273 |
foreach j $DaysInRomanMonthInLeapYear { |
| 274 |
lappend DaysInPriorMonthsInLeapYear [incr i $j] |
| 275 |
} |
| 276 |
|
| 277 |
# Another epoch (Hi, Jeff!) |
| 278 |
|
| 279 |
variable Roddenberry 1946 |
| 280 |
|
| 281 |
# Integer ranges |
| 282 |
|
| 283 |
variable MINWIDE -9223372036854775808 |
| 284 |
variable MAXWIDE 9223372036854775807 |
| 285 |
|
| 286 |
# Day before Leap Day |
| 287 |
|
| 288 |
variable FEB_28 58 |
| 289 |
|
| 290 |
# Translation table to map Windows TZI onto cities, so that the Olson |
| 291 |
# rules can apply. In some cases the mapping is ambiguous, so it's wise |
| 292 |
# to specify $::env(TCL_TZ) rather than simply depending on the system |
| 293 |
# time zone. |
| 294 |
|
| 295 |
# The keys are long lists of values obtained from the time zone |
| 296 |
# information in the Registry. In order, the list elements are: |
| 297 |
# Bias StandardBias DaylightBias |
| 298 |
# StandardDate.wYear StandardDate.wMonth StandardDate.wDayOfWeek |
| 299 |
# StandardDate.wDay StandardDate.wHour StandardDate.wMinute |
| 300 |
# StandardDate.wSecond StandardDate.wMilliseconds |
| 301 |
# DaylightDate.wYear DaylightDate.wMonth DaylightDate.wDayOfWeek |
| 302 |
# DaylightDate.wDay DaylightDate.wHour DaylightDate.wMinute |
| 303 |
# DaylightDate.wSecond DaylightDate.wMilliseconds |
| 304 |
# The values are the names of time zones where those rules apply. There |
| 305 |
# is considerable ambiguity in certain zones; an attempt has been made to |
| 306 |
# make a reasonable guess, but this table needs to be taken with a grain |
| 307 |
# of salt. |
| 308 |
|
| 309 |
variable WinZoneInfo [dict create {*}{ |
| 310 |
{-43200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Kwajalein |
| 311 |
{-39600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Midway |
| 312 |
{-36000 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Honolulu |
| 313 |
{-32400 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/Anchorage |
| 314 |
{-28800 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/Los_Angeles |
| 315 |
{-28800 0 3600 0 10 0 5 2 0 0 0 0 4 0 1 2 0 0 0} :America/Tijuana |
| 316 |
{-25200 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/Denver |
| 317 |
{-25200 0 3600 0 10 0 5 2 0 0 0 0 4 0 1 2 0 0 0} :America/Chihuahua |
| 318 |
{-25200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :America/Phoenix |
| 319 |
{-21600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :America/Regina |
| 320 |
{-21600 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/Chicago |
| 321 |
{-21600 0 3600 0 10 0 5 2 0 0 0 0 4 0 1 2 0 0 0} :America/Mexico_City |
| 322 |
{-18000 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/New_York |
| 323 |
{-18000 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :America/Indianapolis |
| 324 |
{-14400 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :America/Caracas |
| 325 |
{-14400 0 3600 0 3 6 2 23 59 59 999 0 10 6 2 23 59 59 999} |
| 326 |
:America/Santiago |
| 327 |
{-14400 0 3600 0 2 0 5 2 0 0 0 0 11 0 1 2 0 0 0} :America/Manaus |
| 328 |
{-14400 0 3600 0 11 0 1 2 0 0 0 0 3 0 2 2 0 0 0} :America/Halifax |
| 329 |
{-12600 0 3600 0 10 0 5 2 0 0 0 0 4 0 1 2 0 0 0} :America/St_Johns |
| 330 |
{-10800 0 3600 0 2 0 2 2 0 0 0 0 10 0 3 2 0 0 0} :America/Sao_Paulo |
| 331 |
{-10800 0 3600 0 10 0 5 2 0 0 0 0 4 0 1 2 0 0 0} :America/Godthab |
| 332 |
{-10800 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :America/Buenos_Aires |
| 333 |
{-10800 0 3600 0 2 0 5 2 0 0 0 0 11 0 1 2 0 0 0} :America/Bahia |
| 334 |
{-10800 0 3600 0 3 0 2 2 0 0 0 0 10 0 1 2 0 0 0} :America/Montevideo |
| 335 |
{-7200 0 3600 0 9 0 5 2 0 0 0 0 3 0 5 2 0 0 0} :America/Noronha |
| 336 |
{-3600 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Atlantic/Azores |
| 337 |
{-3600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Atlantic/Cape_Verde |
| 338 |
{0 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :UTC |
| 339 |
{0 0 3600 0 10 0 5 2 0 0 0 0 3 0 5 1 0 0 0} :Europe/London |
| 340 |
{3600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Africa/Kinshasa |
| 341 |
{3600 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :CET |
| 342 |
{7200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Africa/Harare |
| 343 |
{7200 0 3600 0 9 4 5 23 59 59 0 0 4 4 5 23 59 59 0} |
| 344 |
:Africa/Cairo |
| 345 |
{7200 0 3600 0 10 0 5 4 0 0 0 0 3 0 5 3 0 0 0} :Europe/Helsinki |
| 346 |
{7200 0 3600 0 9 0 3 2 0 0 0 0 3 5 5 2 0 0 0} :Asia/Jerusalem |
| 347 |
{7200 0 3600 0 9 0 5 1 0 0 0 0 3 0 5 0 0 0 0} :Europe/Bucharest |
| 348 |
{7200 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Europe/Athens |
| 349 |
{7200 0 3600 0 9 5 5 1 0 0 0 0 3 4 5 0 0 0 0} :Asia/Amman |
| 350 |
{7200 0 3600 0 10 6 5 23 59 59 999 0 3 0 5 0 0 0 0} |
| 351 |
:Asia/Beirut |
| 352 |
{7200 0 -3600 0 4 0 1 2 0 0 0 0 9 0 1 2 0 0 0} :Africa/Windhoek |
| 353 |
{10800 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Riyadh |
| 354 |
{10800 0 3600 0 10 0 1 4 0 0 0 0 4 0 1 3 0 0 0} :Asia/Baghdad |
| 355 |
{10800 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Europe/Moscow |
| 356 |
{12600 0 3600 0 9 2 4 2 0 0 0 0 3 0 1 2 0 0 0} :Asia/Tehran |
| 357 |
{14400 0 3600 0 10 0 5 5 0 0 0 0 3 0 5 4 0 0 0} :Asia/Baku |
| 358 |
{14400 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Muscat |
| 359 |
{14400 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Tbilisi |
| 360 |
{16200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Kabul |
| 361 |
{18000 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Karachi |
| 362 |
{18000 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Yekaterinburg |
| 363 |
{19800 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Calcutta |
| 364 |
{20700 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Katmandu |
| 365 |
{21600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Dhaka |
| 366 |
{21600 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Novosibirsk |
| 367 |
{23400 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Rangoon |
| 368 |
{25200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Bangkok |
| 369 |
{25200 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Krasnoyarsk |
| 370 |
{28800 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Chongqing |
| 371 |
{28800 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Irkutsk |
| 372 |
{32400 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Asia/Tokyo |
| 373 |
{32400 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Yakutsk |
| 374 |
{34200 0 3600 0 3 0 5 3 0 0 0 0 10 0 5 2 0 0 0} :Australia/Adelaide |
| 375 |
{34200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Australia/Darwin |
| 376 |
{36000 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Australia/Brisbane |
| 377 |
{36000 0 3600 0 10 0 5 3 0 0 0 0 3 0 5 2 0 0 0} :Asia/Vladivostok |
| 378 |
{36000 0 3600 0 3 0 5 3 0 0 0 0 10 0 1 2 0 0 0} :Australia/Hobart |
| 379 |
{36000 0 3600 0 3 0 5 3 0 0 0 0 10 0 5 2 0 0 0} :Australia/Sydney |
| 380 |
{39600 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Noumea |
| 381 |
{43200 0 3600 0 3 0 3 3 0 0 0 0 10 0 1 2 0 0 0} :Pacific/Auckland |
| 382 |
{43200 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Fiji |
| 383 |
{46800 0 3600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0} :Pacific/Tongatapu |
| 384 |
}] |
| 385 |
|
| 386 |
# Groups of fields that specify the date, priorities, and code bursts that |
| 387 |
# determine Julian Day Number given those groups. The code in [clock |
| 388 |
# scan] will choose the highest priority (lowest numbered) set of fields |
| 389 |
# that determines the date. |
| 390 |
|
| 391 |
variable DateParseActions { |
| 392 |
|
| 393 |
{ seconds } 0 {} |
| 394 |
|
| 395 |
{ julianDay } 1 {} |
| 396 |
|
| 397 |
{ era century yearOfCentury month dayOfMonth } 2 { |
| 398 |
dict set date year [expr { 100 * [dict get $date century] |
| 399 |
+ [dict get $date yearOfCentury] }] |
| 400 |
set date [GetJulianDayFromEraYearMonthDay $date[set date {}] \ |
| 401 |
$changeover] |
| 402 |
} |
| 403 |
{ era century yearOfCentury dayOfYear } 2 { |
| 404 |
dict set date year [expr { 100 * [dict get $date century] |
| 405 |
+ [dict get $date yearOfCentury] }] |
| 406 |
set date [GetJulianDayFromEraYearDay $date[set date {}] \ |
| 407 |
$changeover] |
| 408 |
} |
| 409 |
|
| 410 |
{ century yearOfCentury month dayOfMonth } 3 { |
| 411 |
dict set date era CE |
| 412 |
dict set date year [expr { 100 * [dict get $date century] |
| 413 |
+ [dict get $date yearOfCentury] }] |
| 414 |
set date [GetJulianDayFromEraYearMonthDay $date[set date {}] \ |
| 415 |
$changeover] |
| 416 |
} |
| 417 |
{ century yearOfCentury dayOfYear } 3 { |
| 418 |
dict set date era CE |
| 419 |
dict set date year [expr { 100 * [dict get $date century] |
| 420 |
+ [dict get $date yearOfCentury] }] |
| 421 |
set date [GetJulianDayFromEraYearDay $date[set date {}] \ |
| 422 |
$changeover] |
| 423 |
} |
| 424 |
{ iso8601Century iso8601YearOfCentury iso8601Week dayOfWeek } 3 { |
| 425 |
dict set date era CE |
| 426 |
dict set date iso8601Year \ |
| 427 |
[expr { 100 * [dict get $date iso8601Century] |
| 428 |
+ [dict get $date iso8601YearOfCentury] }] |
| 429 |
set date [GetJulianDayFromEraYearWeekDay $date[set date {}] \ |
| 430 |
$changeover] |
| 431 |
} |
| 432 |
|
| 433 |
{ yearOfCentury month dayOfMonth } 4 { |
| 434 |
set date [InterpretTwoDigitYear $date[set date {}] $baseTime] |
| 435 |
dict set date era CE |
| 436 |
set date [GetJulianDayFromEraYearMonthDay $date[set date {}] \ |
| 437 |
$changeover] |
| 438 |
} |
| 439 |
{ yearOfCentury dayOfYear } 4 { |
| 440 |
set date [InterpretTwoDigitYear $date[set date {}] $baseTime] |
| 441 |
dict set date era CE |
| 442 |
set date [GetJulianDayFromEraYearDay $date[set date {}] \ |
| 443 |
$changeover] |
| 444 |
} |
| 445 |
{ iso8601YearOfCentury iso8601Week dayOfWeek } 4 { |
| 446 |
set date [InterpretTwoDigitYear \ |
| 447 |
$date[set date {}] $baseTime \ |
| 448 |
iso8601YearOfCentury iso8601Year] |
| 449 |
dict set date era CE |
| 450 |
set date [GetJulianDayFromEraYearWeekDay $date[set date {}] \ |
| 451 |
$changeover] |
| 452 |
} |
| 453 |
|
| 454 |
{ month dayOfMonth } 5 { |
| 455 |
set date [AssignBaseYear $date[set date {}] \ |
| 456 |
$baseTime $timeZone $changeover] |
| 457 |
set date [GetJulianDayFromEraYearMonthDay $date[set date {}] \ |
| 458 |
$changeover] |
| 459 |
} |
| 460 |
{ dayOfYear } 5 { |
| 461 |
set date [AssignBaseYear $date[set date {}] \ |
| 462 |
$baseTime $timeZone $changeover] |
| 463 |
set date [GetJulianDayFromEraYearDay $date[set date {}] \ |
| 464 |
$changeover] |
| 465 |
} |
| 466 |
{ iso8601Week dayOfWeek } 5 { |
| 467 |
set date [AssignBaseIso8601Year $date[set date {}] \ |
| 468 |
$baseTime $timeZone $changeover] |
| 469 |
set date [GetJulianDayFromEraYearWeekDay $date[set date {}] \ |
| 470 |
$changeover] |
| 471 |
} |
| 472 |
|
| 473 |
{ dayOfMonth } 6 { |
| 474 |
set date [AssignBaseMonth $date[set date {}] \ |
| 475 |
$baseTime $timeZone $changeover] |
| 476 |
set date [GetJulianDayFromEraYearMonthDay $date[set date {}] \ |
| 477 |
$changeover] |
| 478 |
} |
| 479 |
|
| 480 |
{ dayOfWeek } 7 { |
| 481 |
set date [AssignBaseWeek $date[set date {}] \ |
| 482 |
$baseTime $timeZone $changeover] |
| 483 |
set date [GetJulianDayFromEraYearWeekDay $date[set date {}] \ |
| 484 |
$changeover] |
| 485 |
} |
| 486 |
|
| 487 |
{} 8 { |
| 488 |
set date [AssignBaseJulianDay $date[set date {}] \ |
| 489 |
$baseTime $timeZone $changeover] |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
# Groups of fields that specify time of day, priorities, and code that |
| 494 |
# processes them |
| 495 |
|
| 496 |
variable TimeParseActions { |
| 497 |
|
| 498 |
seconds 1 {} |
| 499 |
|
| 500 |
{ hourAMPM minute second amPmIndicator } 2 { |
| 501 |
dict set date secondOfDay [InterpretHMSP $date] |
| 502 |
} |
| 503 |
{ hour minute second } 2 { |
| 504 |
dict set date secondOfDay [InterpretHMS $date] |
| 505 |
} |
| 506 |
|
| 507 |
{ hourAMPM minute amPmIndicator } 3 { |
| 508 |
dict set date second 0 |
| 509 |
dict set date secondOfDay [InterpretHMSP $date] |
| 510 |
} |
| 511 |
{ hour minute } 3 { |
| 512 |
dict set date second 0 |
| 513 |
dict set date secondOfDay [InterpretHMS $date] |
| 514 |
} |
| 515 |
|
| 516 |
{ hourAMPM amPmIndicator } 4 { |
| 517 |
dict set date minute 0 |
| 518 |
dict set date second 0 |
| 519 |
dict set date secondOfDay [InterpretHMSP $date] |
| 520 |
} |
| 521 |
{ hour } 4 { |
| 522 |
dict set date minute 0 |
| 523 |
dict set date second 0 |
| 524 |
dict set date secondOfDay [InterpretHMS $date] |
| 525 |
} |
| 526 |
|
| 527 |
{ } 5 { |
| 528 |
dict set date secondOfDay 0 |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
# Legacy time zones, used primarily for parsing RFC822 dates. |
| 533 |
|
| 534 |
variable LegacyTimeZone [dict create \ |
| 535 |
gmt +0000 \ |
| 536 |
ut +0000 \ |
| 537 |
utc +0000 \ |
| 538 |
bst +0100 \ |
| 539 |
wet +0000 \ |
| 540 |
wat -0100 \ |
| 541 |
at -0200 \ |
| 542 |
nft -0330 \ |
| 543 |
nst -0330 \ |
| 544 |
ndt -0230 \ |
| 545 |
ast -0400 \ |
| 546 |
adt -0300 \ |
| 547 |
est -0500 \ |
| 548 |
edt -0400 \ |
| 549 |
cst -0600 \ |
| 550 |
cdt -0500 \ |
| 551 |
mst -0700 \ |
| 552 |
mdt -0600 \ |
| 553 |
pst -0800 \ |
| 554 |
pdt -0700 \ |
| 555 |
yst -0900 \ |
| 556 |
ydt -0800 \ |
| 557 |
akst -0900 \ |
| 558 |
akdt -0800 \ |
| 559 |
hst -1000 \ |
| 560 |
hdt -0900 \ |
| 561 |
cat -1000 \ |
| 562 |
ahst -1000 \ |
| 563 |
nt -1100 \ |
| 564 |
idlw -1200 \ |
| 565 |
cet +0100 \ |
| 566 |
cest +0200 \ |
| 567 |
met +0100 \ |
| 568 |
mewt +0100 \ |
| 569 |
mest +0200 \ |
| 570 |
swt +0100 \ |
| 571 |
sst +0200 \ |
| 572 |
fwt +0100 \ |
| 573 |
fst +0200 \ |
| 574 |
eet +0200 \ |
| 575 |
eest +0300 \ |
| 576 |
bt +0300 \ |
| 577 |
it +0330 \ |
| 578 |
zp4 +0400 \ |
| 579 |
zp5 +0500 \ |
| 580 |
ist +0530 \ |
| 581 |
zp6 +0600 \ |
| 582 |
wast +0700 \ |
| 583 |
wadt +0800 \ |
| 584 |
jt +0730 \ |
| 585 |
cct +0800 \ |
| 586 |
jst +0900 \ |
| 587 |
kst +0900 \ |
| 588 |
cast +0930 \ |
| 589 |
jdt +1000 \ |
| 590 |
kdt +1000 \ |
| 591 |
cadt +1030 \ |
| 592 |
east +1000 \ |
| 593 |
eadt +1030 \ |
| 594 |
gst +1000 \ |
| 595 |
nzt +1200 \ |
| 596 |
nzst +1200 \ |
| 597 |
nzdt +1300 \ |
| 598 |
idle +1200 \ |
| 599 |
a +0100 \ |
| 600 |
b +0200 \ |
| 601 |
c +0300 \ |
| 602 |
d +0400 \ |
| 603 |
e +0500 \ |
| 604 |
f +0600 \ |
| 605 |
g +0700 \ |
| 606 |
h +0800 \ |
| 607 |
i +0900 \ |
| 608 |
k +1000 \ |
| 609 |
l +1100 \ |
| 610 |
m +1200 \ |
| 611 |
n -0100 \ |
| 612 |
o -0200 \ |
| 613 |
p -0300 \ |
| 614 |
q -0400 \ |
| 615 |
r -0500 \ |
| 616 |
s -0600 \ |
| 617 |
t -0700 \ |
| 618 |
u -0800 \ |
| 619 |
v -0900 \ |
| 620 |
w -1000 \ |
| 621 |
x -1100 \ |
| 622 |
y -1200 \ |
| 623 |
z +0000 \ |
| 624 |
] |
| 625 |
|
| 626 |
# Caches |
| 627 |
|
| 628 |
variable LocaleNumeralCache {}; # Dictionary whose keys are locale |
| 629 |
# names and whose values are pairs |
| 630 |
# comprising regexes matching numerals |
| 631 |
# in the given locales and dictionaries |
| 632 |
# mapping the numerals to their numeric |
| 633 |
# values. |
| 634 |
# variable CachedSystemTimeZone; # If 'CachedSystemTimeZone' exists, |
| 635 |
# it contains the value of the |
| 636 |
# system time zone, as determined from |
| 637 |
# the environment. |
| 638 |
variable TimeZoneBad {}; # Dictionary whose keys are time zone |
| 639 |
# names and whose values are 1 if |
| 640 |
# the time zone is unknown and 0 |
| 641 |
# if it is known. |
| 642 |
variable TZData; # Array whose keys are time zone names |
| 643 |
# and whose values are lists of quads |
| 644 |
# comprising start time, UTC offset, |
| 645 |
# Daylight Saving Time indicator, and |
| 646 |
# time zone abbreviation. |
| 647 |
variable FormatProc; # Array mapping format group |
| 648 |
# and locale to the name of a procedure |
| 649 |
# that renders the given format |
| 650 |
} |
| 651 |
::tcl::clock::Initialize |
| 652 |
|
| 653 |
#---------------------------------------------------------------------- |
| 654 |
# |
| 655 |
# clock format -- |
| 656 |
# |
| 657 |
# Formats a count of seconds since the Posix Epoch as a time of day. |
| 658 |
# |
| 659 |
# The 'clock format' command formats times of day for output. Refer to the |
| 660 |
# user documentation to see what it does. |
| 661 |
# |
| 662 |
#---------------------------------------------------------------------- |
| 663 |
|
| 664 |
proc ::tcl::clock::format { args } { |
| 665 |
|
| 666 |
variable FormatProc |
| 667 |
variable TZData |
| 668 |
|
| 669 |
lassign [ParseFormatArgs {*}$args] format locale timezone |
| 670 |
set locale [string tolower $locale] |
| 671 |
set clockval [lindex $args 0] |
| 672 |
|
| 673 |
# Get the data for time changes in the given zone |
| 674 |
|
| 675 |
if {$timezone eq ""} { |
| 676 |
set timezone [GetSystemTimeZone] |
| 677 |
} |
| 678 |
if {![info exists TZData($timezone)]} { |
| 679 |
if {[catch {SetupTimeZone $timezone} retval opts]} { |
| 680 |
dict unset opts -errorinfo |
| 681 |
return -options $opts $retval |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
# Build a procedure to format the result. Cache the built procedure's name |
| 686 |
# in the 'FormatProc' array to avoid losing its internal representation, |
| 687 |
# which contains the name resolution. |
| 688 |
|
| 689 |
set procName formatproc'$format'$locale |
| 690 |
set procName [namespace current]::[string map {: {\:} \\ {\\}} $procName] |
| 691 |
if {[info exists FormatProc($procName)]} { |
| 692 |
set procName $FormatProc($procName) |
| 693 |
} else { |
| 694 |
set FormatProc($procName) \ |
| 695 |
[ParseClockFormatFormat $procName $format $locale] |
| 696 |
} |
| 697 |
|
| 698 |
return [$procName $clockval $timezone] |
| 699 |
|
| 700 |
} |
| 701 |
|
| 702 |
#---------------------------------------------------------------------- |
| 703 |
# |
| 704 |
# ParseClockFormatFormat -- |
| 705 |
# |
| 706 |
# Builds and caches a procedure that formats a time value. |
| 707 |
# |
| 708 |
# Parameters: |
| 709 |
# format -- Format string to use |
| 710 |
# locale -- Locale in which the format string is to be interpreted |
| 711 |
# |
| 712 |
# Results: |
| 713 |
# Returns the name of the newly-built procedure. |
| 714 |
# |
| 715 |
#---------------------------------------------------------------------- |
| 716 |
|
| 717 |
proc ::tcl::clock::ParseClockFormatFormat {procName format locale} { |
| 718 |
|
| 719 |
if {[namespace which $procName] ne {}} { |
| 720 |
return $procName |
| 721 |
} |
| 722 |
|
| 723 |
# Map away the locale-dependent composite format groups |
| 724 |
|
| 725 |
EnterLocale $locale |
| 726 |
|
| 727 |
# Change locale if a fresh locale has been given on the command line. |
| 728 |
|
| 729 |
try { |
| 730 |
return [ParseClockFormatFormat2 $format $locale $procName] |
| 731 |
} trap CLOCK {result opts} { |
| 732 |
dict unset opts -errorinfo |
| 733 |
return -options $opts $result |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
proc ::tcl::clock::ParseClockFormatFormat2 {format locale procName} { |
| 738 |
set didLocaleEra 0 |
| 739 |
set didLocaleNumerals 0 |
| 740 |
set preFormatCode \ |
| 741 |
[string map [list @GREGORIAN_CHANGE_DATE@ \ |
| 742 |
[mc GREGORIAN_CHANGE_DATE]] \ |
| 743 |
{ |
| 744 |
variable TZData |
| 745 |
set date [GetDateFields $clockval \ |
| 746 |
$TZData($timezone) \ |
| 747 |
@GREGORIAN_CHANGE_DATE@] |
| 748 |
}] |
| 749 |
set formatString {} |
| 750 |
set substituents {} |
| 751 |
set state {} |
| 752 |
|
| 753 |
set format [LocalizeFormat $locale $format] |
| 754 |
|
| 755 |
foreach char [split $format {}] { |
| 756 |
switch -exact -- $state { |
| 757 |
{} { |
| 758 |
if { [string equal % $char] } { |
| 759 |
set state percent |
| 760 |
} else { |
| 761 |
append formatString $char |
| 762 |
} |
| 763 |
} |
| 764 |
percent { # Character following a '%' character |
| 765 |
set state {} |
| 766 |
switch -exact -- $char { |
| 767 |
% { # A literal character, '%' |
| 768 |
append formatString %% |
| 769 |
} |
| 770 |
a { # Day of week, abbreviated |
| 771 |
append formatString %s |
| 772 |
append substituents \ |
| 773 |
[string map \ |
| 774 |
[list @DAYS_OF_WEEK_ABBREV@ \ |
| 775 |
[list [mc DAYS_OF_WEEK_ABBREV]]] \ |
| 776 |
{ [lindex @DAYS_OF_WEEK_ABBREV@ \ |
| 777 |
[expr {[dict get $date dayOfWeek] \ |
| 778 |
% 7}]]}] |
| 779 |
} |
| 780 |
A { # Day of week, spelt out. |
| 781 |
append formatString %s |
| 782 |
append substituents \ |
| 783 |
[string map \ |
| 784 |
[list @DAYS_OF_WEEK_FULL@ \ |
| 785 |
[list [mc DAYS_OF_WEEK_FULL]]] \ |
| 786 |
{ [lindex @DAYS_OF_WEEK_FULL@ \ |
| 787 |
[expr {[dict get $date dayOfWeek] \ |
| 788 |
% 7}]]}] |
| 789 |
} |
| 790 |
b - h { # Name of month, abbreviated. |
| 791 |
append formatString %s |
| 792 |
append substituents \ |
| 793 |
[string map \ |
| 794 |
[list @MONTHS_ABBREV@ \ |
| 795 |
[list [mc MONTHS_ABBREV]]] \ |
| 796 |
{ [lindex @MONTHS_ABBREV@ \ |
| 797 |
[expr {[dict get $date month]-1}]]}] |
| 798 |
} |
| 799 |
B { # Name of month, spelt out |
| 800 |
append formatString %s |
| 801 |
append substituents \ |
| 802 |
[string map \ |
| 803 |
[list @MONTHS_FULL@ \ |
| 804 |
[list [mc MONTHS_FULL]]] \ |
| 805 |
{ [lindex @MONTHS_FULL@ \ |
| 806 |
[expr {[dict get $date month]-1}]]}] |
| 807 |
} |
| 808 |
C { # Century number |
| 809 |
append formatString %02d |
| 810 |
append substituents \ |
| 811 |
{ [expr {[dict get $date year] / 100}]} |
| 812 |
} |
| 813 |
d { # Day of month, with leading zero |
| 814 |
append formatString %02d |
| 815 |
append substituents { [dict get $date dayOfMonth]} |
| 816 |
} |
| 817 |
e { # Day of month, without leading zero |
| 818 |
append formatString %2d |
| 819 |
append substituents { [dict get $date dayOfMonth]} |
| 820 |
} |
| 821 |
E { # Format group in a locale-dependent |
| 822 |
# alternative era |
| 823 |
set state percentE |
| 824 |
if {!$didLocaleEra} { |
| 825 |
append preFormatCode \ |
| 826 |
[string map \ |
| 827 |
[list @LOCALE_ERAS@ \ |
| 828 |
[list [mc LOCALE_ERAS]]] \ |
| 829 |
{ |
| 830 |
set date [GetLocaleEra \ |
| 831 |
$date[set date {}] \ |
| 832 |
@LOCALE_ERAS@]}] \n |
| 833 |
set didLocaleEra 1 |
| 834 |
} |
| 835 |
if {!$didLocaleNumerals} { |
| 836 |
append preFormatCode \ |
| 837 |
[list set localeNumerals \ |
| 838 |
[mc LOCALE_NUMERALS]] \n |
| 839 |
set didLocaleNumerals 1 |
| 840 |
} |
| 841 |
} |
| 842 |
g { # Two-digit year relative to ISO8601 |
| 843 |
# week number |
| 844 |
append formatString %02d |
| 845 |
append substituents \ |
| 846 |
{ [expr { [dict get $date iso8601Year] % 100 }]} |
| 847 |
} |
| 848 |
G { # Four-digit year relative to ISO8601 |
| 849 |
# week number |
| 850 |
append formatString %02d |
| 851 |
append substituents { [dict get $date iso8601Year]} |
| 852 |
} |
| 853 |
H { # Hour in the 24-hour day, leading zero |
| 854 |
append formatString %02d |
| 855 |
append substituents \ |
| 856 |
{ [expr { [dict get $date localSeconds] \ |
| 857 |
/ 3600 % 24}]} |
| 858 |
} |
| 859 |
I { # Hour AM/PM, with leading zero |
| 860 |
append formatString %02d |
| 861 |
append substituents \ |
| 862 |
{ [expr { ( ( ( [dict get $date localSeconds] \ |
| 863 |
% 86400 ) \ |
| 864 |
+ 86400 \ |
| 865 |
- 3600 ) \ |
| 866 |
/ 3600 ) \ |
| 867 |
% 12 + 1 }] } |
| 868 |
} |
| 869 |
j { # Day of year (001-366) |
| 870 |
append formatString %03d |
| 871 |
append substituents { [dict get $date dayOfYear]} |
| 872 |
} |
| 873 |
J { # Julian Day Number |
| 874 |
append formatString %07ld |
| 875 |
append substituents { [dict get $date julianDay]} |
| 876 |
} |
| 877 |
k { # Hour (0-23), no leading zero |
| 878 |
append formatString %2d |
| 879 |
append substituents \ |
| 880 |
{ [expr { [dict get $date localSeconds] |
| 881 |
/ 3600 |
| 882 |
% 24 }]} |
| 883 |
} |
| 884 |
l { # Hour (12-11), no leading zero |
| 885 |
append formatString %2d |
| 886 |
append substituents \ |
| 887 |
{ [expr { ( ( ( [dict get $date localSeconds] |
| 888 |
% 86400 ) |
| 889 |
+ 86400 |
| 890 |
- 3600 ) |
| 891 |
/ 3600 ) |
| 892 |
% 12 + 1 }]} |
| 893 |
} |
| 894 |
m { # Month number, leading zero |
| 895 |
append formatString %02d |
| 896 |
append substituents { [dict get $date month]} |
| 897 |
} |
| 898 |
M { # Minute of the hour, leading zero |
| 899 |
append formatString %02d |
| 900 |
append substituents \ |
| 901 |
{ [expr { [dict get $date localSeconds] |
| 902 |
/ 60 |
| 903 |
% 60 }]} |
| 904 |
} |
| 905 |
n { # A literal newline |
| 906 |
append formatString \n |
| 907 |
} |
| 908 |
N { # Month number, no leading zero |
| 909 |
append formatString %2d |
| 910 |
append substituents { [dict get $date month]} |
| 911 |
} |
| 912 |
O { # A format group in the locale's |
| 913 |
# alternative numerals |
| 914 |
set state percentO |
| 915 |
if {!$didLocaleNumerals} { |
| 916 |
append preFormatCode \ |
| 917 |
[list set localeNumerals \ |
| 918 |
[mc LOCALE_NUMERALS]] \n |
| 919 |
set didLocaleNumerals 1 |
| 920 |
} |
| 921 |
} |
| 922 |
p { # Localized 'AM' or 'PM' indicator |
| 923 |
# converted to uppercase |
| 924 |
append formatString %s |
| 925 |
append preFormatCode \ |
| 926 |
[list set AM [string toupper [mc AM]]] \n \ |
| 927 |
[list set PM [string toupper [mc PM]]] \n |
| 928 |
append substituents \ |
| 929 |
{ [expr {(([dict get $date localSeconds] |
| 930 |
% 86400) < 43200) ? |
| 931 |
$AM : $PM}]} |
| 932 |
} |
| 933 |
P { # Localized 'AM' or 'PM' indicator |
| 934 |
append formatString %s |
| 935 |
append preFormatCode \ |
| 936 |
[list set am [mc AM]] \n \ |
| 937 |
[list set pm [mc PM]] \n |
| 938 |
append substituents \ |
| 939 |
{ [expr {(([dict get $date localSeconds] |
| 940 |
% 86400) < 43200) ? |
| 941 |
$am : $pm}]} |
| 942 |
|
| 943 |
} |
| 944 |
Q { # Hi, Jeff! |
| 945 |
append formatString %s |
| 946 |
append substituents { [FormatStarDate $date]} |
| 947 |
} |
| 948 |
s { # Seconds from the Posix Epoch |
| 949 |
append formatString %s |
| 950 |
append substituents { [dict get $date seconds]} |
| 951 |
} |
| 952 |
S { # Second of the minute, with |
| 953 |
# leading zero |
| 954 |
append formatString %02d |
| 955 |
append substituents \ |
| 956 |
{ [expr { [dict get $date localSeconds] |
| 957 |
% 60 }]} |
| 958 |
} |
| 959 |
t { # A literal tab character |
| 960 |
append formatString \t |
| 961 |
} |
| 962 |
u { # Day of the week (1-Monday, 7-Sunday) |
| 963 |
append formatString %1d |
| 964 |
append substituents { [dict get $date dayOfWeek]} |
| 965 |
} |
| 966 |
U { # Week of the year (00-53). The |
| 967 |
# first Sunday of the year is the |
| 968 |
# first day of week 01 |
| 969 |
append formatString %02d |
| 970 |
append preFormatCode { |
| 971 |
set dow [dict get $date dayOfWeek] |
| 972 |
if { $dow == 7 } { |
| 973 |
set dow 0 |
| 974 |
} |
| 975 |
incr dow |
| 976 |
set UweekNumber \ |
| 977 |
[expr { ( [dict get $date dayOfYear] |
| 978 |
- $dow + 7 ) |
| 979 |
/ 7 }] |
| 980 |
} |
| 981 |
append substituents { $UweekNumber} |
| 982 |
} |
| 983 |
V { # The ISO8601 week number |
| 984 |
append formatString %02d |
| 985 |
append substituents { [dict get $date iso8601Week]} |
| 986 |
} |
| 987 |
w { # Day of the week (0-Sunday, |
| 988 |
# 6-Saturday) |
| 989 |
append formatString %1d |
| 990 |
append substituents \ |
| 991 |
{ [expr { [dict get $date dayOfWeek] % 7 }]} |
| 992 |
} |
| 993 |
W { # Week of the year (00-53). The first |
| 994 |
# Monday of the year is the first day |
| 995 |
# of week 01. |
| 996 |
append preFormatCode { |
| 997 |
set WweekNumber \ |
| 998 |
[expr { ( [dict get $date dayOfYear] |
| 999 |
- [dict get $date dayOfWeek] |
| 1000 |
+ 7 ) |
| 1001 |
/ 7 }] |
| 1002 |
} |
| 1003 |
append formatString %02d |
| 1004 |
append substituents { $WweekNumber} |
| 1005 |
} |
| 1006 |
y { # The two-digit year of the century |
| 1007 |
append formatString %02d |
| 1008 |
append substituents \ |
| 1009 |
{ [expr { [dict get $date year] % 100 }]} |
| 1010 |
} |
| 1011 |
Y { # The four-digit year |
| 1012 |
append formatString %04d |
| 1013 |
append substituents { [dict get $date year]} |
| 1014 |
} |
| 1015 |
z { # The time zone as hours and minutes |
| 1016 |
# east (+) or west (-) of Greenwich |
| 1017 |
append formatString %s |
| 1018 |
append substituents { [FormatNumericTimeZone \ |
| 1019 |
[dict get $date tzOffset]]} |
| 1020 |
} |
| 1021 |
Z { # The name of the time zone |
| 1022 |
append formatString %s |
| 1023 |
append substituents { [dict get $date tzName]} |
| 1024 |
} |
| 1025 |
% { # A literal percent character |
| 1026 |
append formatString %% |
| 1027 |
} |
| 1028 |
default { # An unknown escape sequence |
| 1029 |
append formatString %% $char |
| 1030 |
} |
| 1031 |
} |
| 1032 |
} |
| 1033 |
percentE { # Character following %E |
| 1034 |
set state {} |
| 1035 |
switch -exact -- $char { |
| 1036 |
E { |
| 1037 |
append formatString %s |
| 1038 |
append substituents { } \ |
| 1039 |
[string map \ |
| 1040 |
[list @BCE@ [list [mc BCE]] \ |
| 1041 |
@CE@ [list [mc CE]]] \ |
| 1042 |
{[dict get {BCE @BCE@ CE @CE@} \ |
| 1043 |
[dict get $date era]]}] |
| 1044 |
} |
| 1045 |
C { # Locale-dependent era |
| 1046 |
append formatString %s |
| 1047 |
append substituents { [dict get $date localeEra]} |
| 1048 |
} |
| 1049 |
y { # Locale-dependent year of the era |
| 1050 |
append preFormatCode { |
| 1051 |
set y [dict get $date localeYear] |
| 1052 |
if { $y >= 0 && $y < 100 } { |
| 1053 |
set Eyear [lindex $localeNumerals $y] |
| 1054 |
} else { |
| 1055 |
set Eyear $y |
| 1056 |
} |
| 1057 |
} |
| 1058 |
append formatString %s |
| 1059 |
append substituents { $Eyear} |
| 1060 |
} |
| 1061 |
default { # Unknown %E format group |
| 1062 |
append formatString %%E $char |
| 1063 |
} |
| 1064 |
} |
| 1065 |
} |
| 1066 |
percentO { # Character following %O |
| 1067 |
set state {} |
| 1068 |
switch -exact -- $char { |
| 1069 |
d - e { # Day of the month in alternative |
| 1070 |
# numerals |
| 1071 |
append formatString %s |
| 1072 |
append substituents \ |
| 1073 |
{ [lindex $localeNumerals \ |
| 1074 |
[dict get $date dayOfMonth]]} |
| 1075 |
} |
| 1076 |
H - k { # Hour of the day in alternative |
| 1077 |
# numerals |
| 1078 |
append formatString %s |
| 1079 |
append substituents \ |
| 1080 |
{ [lindex $localeNumerals \ |
| 1081 |
[expr { [dict get $date localSeconds] |
| 1082 |
/ 3600 |
| 1083 |
% 24 }]]} |
| 1084 |
} |
| 1085 |
I - l { # Hour (12-11) AM/PM in alternative |
| 1086 |
# numerals |
| 1087 |
append formatString %s |
| 1088 |
append substituents \ |
| 1089 |
{ [lindex $localeNumerals \ |
| 1090 |
[expr { ( ( ( [dict get $date localSeconds] |
| 1091 |
% 86400 ) |
| 1092 |
+ 86400 |
| 1093 |
- 3600 ) |
| 1094 |
/ 3600 ) |
| 1095 |
% 12 + 1 }]]} |
| 1096 |
} |
| 1097 |
m { # Month number in alternative numerals |
| 1098 |
append formatString %s |
| 1099 |
append substituents \ |
| 1100 |
{ [lindex $localeNumerals [dict get $date month]]} |
| 1101 |
} |
| 1102 |
M { # Minute of the hour in alternative |
| 1103 |
# numerals |
| 1104 |
append formatString %s |
| 1105 |
append substituents \ |
| 1106 |
{ [lindex $localeNumerals \ |
| 1107 |
[expr { [dict get $date localSeconds] |
| 1108 |
/ 60 |
| 1109 |
% 60 }]]} |
| 1110 |
} |
| 1111 |
S { # Second of the minute in alternative |
| 1112 |
# numerals |
| 1113 |
append formatString %s |
| 1114 |
append substituents \ |
| 1115 |
{ [lindex $localeNumerals \ |
| 1116 |
[expr { [dict get $date localSeconds] |
| 1117 |
% 60 }]]} |
| 1118 |
} |
| 1119 |
u { # Day of the week (Monday=1,Sunday=7) |
| 1120 |
# in alternative numerals |
| 1121 |
append formatString %s |
| 1122 |
append substituents \ |
| 1123 |
{ [lindex $localeNumerals \ |
| 1124 |
[dict get $date dayOfWeek]]} |
| 1125 |
} |
| 1126 |
w { # Day of the week (Sunday=0,Saturday=6) |
| 1127 |
# in alternative numerals |
| 1128 |
append formatString %s |
| 1129 |
append substituents \ |
| 1130 |
{ [lindex $localeNumerals \ |
| 1131 |
[expr { [dict get $date dayOfWeek] % 7 }]]} |
| 1132 |
} |
| 1133 |
y { # Year of the century in alternative |
| 1134 |
# numerals |
| 1135 |
append formatString %s |
| 1136 |
append substituents \ |
| 1137 |
{ [lindex $localeNumerals \ |
| 1138 |
[expr { [dict get $date year] % 100 }]]} |
| 1139 |
} |
| 1140 |
default { # Unknown format group |
| 1141 |
append formatString %%O $char |
| 1142 |
} |
| 1143 |
} |
| 1144 |
} |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
# Clean up any improperly terminated groups |
| 1149 |
|
| 1150 |
switch -exact -- $state { |
| 1151 |
percent { |
| 1152 |
append formatString %% |
| 1153 |
} |
| 1154 |
percentE { |
| 1155 |
append retval %%E |
| 1156 |
} |
| 1157 |
percentO { |
| 1158 |
append retval %%O |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
proc $procName {clockval timezone} " |
| 1163 |
$preFormatCode |
| 1164 |
return \[::format [list $formatString] $substituents\] |
| 1165 |
" |
| 1166 |
|
| 1167 |
# puts [list $procName [info args $procName] [info body $procName]] |
| 1168 |
|
| 1169 |
return $procName |
| 1170 |
} |
| 1171 |
|
| 1172 |
#---------------------------------------------------------------------- |
| 1173 |
# |
| 1174 |
# clock scan -- |
| 1175 |
# |
| 1176 |
# Inputs a count of seconds since the Posix Epoch as a time of day. |
| 1177 |
# |
| 1178 |
# The 'clock scan' command scans times of day on input. Refer to the user |
| 1179 |
# documentation to see what it does. |
| 1180 |
# |
| 1181 |
#---------------------------------------------------------------------- |
| 1182 |
|
| 1183 |
proc ::tcl::clock::scan { args } { |
| 1184 |
|
| 1185 |
set format {} |
| 1186 |
|
| 1187 |
# Check the count of args |
| 1188 |
|
| 1189 |
if { [llength $args] < 1 || [llength $args] % 2 != 1 } { |
| 1190 |
set cmdName "clock scan" |
| 1191 |
return -code error \ |
| 1192 |
-errorcode [list CLOCK wrongNumArgs] \ |
| 1193 |
"wrong \# args: should be\ |
| 1194 |
\"$cmdName string\ |
| 1195 |
?-base seconds?\ |
| 1196 |
?-format string? ?-gmt boolean?\ |
| 1197 |
?-locale LOCALE? ?-timezone ZONE?\"" |
| 1198 |
} |
| 1199 |
|
| 1200 |
# Set defaults |
| 1201 |
|
| 1202 |
set base [clock seconds] |
| 1203 |
set string [lindex $args 0] |
| 1204 |
set format {} |
| 1205 |
set gmt 0 |
| 1206 |
set locale c |
| 1207 |
set timezone [GetSystemTimeZone] |
| 1208 |
|
| 1209 |
# Pick up command line options. |
| 1210 |
|
| 1211 |
foreach { flag value } [lreplace $args 0 0] { |
| 1212 |
switch -exact -- $flag { |
| 1213 |
-b - -ba - -bas - -base { |
| 1214 |
set base $value |
| 1215 |
} |
| 1216 |
-f - -fo - -for - -form - -forma - -format { |
| 1217 |
set saw(-format) {} |
| 1218 |
set format $value |
| 1219 |
} |
| 1220 |
-g - -gm - -gmt { |
| 1221 |
set saw(-gmt) {} |
| 1222 |
set gmt $value |
| 1223 |
} |
| 1224 |
-l - -lo - -loc - -loca - -local - -locale { |
| 1225 |
set saw(-locale) {} |
| 1226 |
set locale [string tolower $value] |
| 1227 |
} |
| 1228 |
-t - -ti - -tim - -time - -timez - -timezo - -timezon - -timezone { |
| 1229 |
set saw(-timezone) {} |
| 1230 |
set timezone $value |
| 1231 |
} |
| 1232 |
default { |
| 1233 |
return -code error \ |
| 1234 |
-errorcode [list CLOCK badOption $flag] \ |
| 1235 |
"bad option \"$flag\",\ |
| 1236 |
must be -base, -format, -gmt, -locale or -timezone" |
| 1237 |
} |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
# Check options for validity |
| 1242 |
|
| 1243 |
if { [info exists saw(-gmt)] && [info exists saw(-timezone)] } { |
| 1244 |
return -code error \ |
| 1245 |
-errorcode [list CLOCK gmtWithTimezone] \ |
| 1246 |
"cannot use -gmt and -timezone in same call" |
| 1247 |
} |
| 1248 |
if { [catch { expr { wide($base) } } result] } { |
| 1249 |
return -code error "expected integer but got \"$base\"" |
| 1250 |
} |
| 1251 |
if { ![string is boolean -strict $gmt] } { |
| 1252 |
return -code error "expected boolean value but got \"$gmt\"" |
| 1253 |
} elseif { $gmt } { |
| 1254 |
set timezone :GMT |
| 1255 |
} |
| 1256 |
|
| 1257 |
if { ![info exists saw(-format)] } { |
| 1258 |
# Perhaps someday we'll localize the legacy code. Right now, it's not |
| 1259 |
# localized. |
| 1260 |
if { [info exists saw(-locale)] } { |
| 1261 |
return -code error \ |
| 1262 |
-errorcode [list CLOCK flagWithLegacyFormat] \ |
| 1263 |
"legacy \[clock scan\] does not support -locale" |
| 1264 |
|
| 1265 |
} |
| 1266 |
return [FreeScan $string $base $timezone $locale] |
| 1267 |
} |
| 1268 |
|
| 1269 |
# Change locale if a fresh locale has been given on the command line. |
| 1270 |
|
| 1271 |
EnterLocale $locale |
| 1272 |
|
| 1273 |
try { |
| 1274 |
# Map away the locale-dependent composite format groups |
| 1275 |
|
| 1276 |
set scanner [ParseClockScanFormat $format $locale] |
| 1277 |
return [$scanner $string $base $timezone] |
| 1278 |
} trap CLOCK {result opts} { |
| 1279 |
# Conceal location of generation of expected errors |
| 1280 |
dict unset opts -errorinfo |
| 1281 |
return -options $opts $result |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
#---------------------------------------------------------------------- |
| 1286 |
# |
| 1287 |
# FreeScan -- |
| 1288 |
# |
| 1289 |
# Scans a time in free format |
| 1290 |
# |
| 1291 |
# Parameters: |
| 1292 |
# string - String containing the time to scan |
| 1293 |
# base - Base time, expressed in seconds from the Epoch |
| 1294 |
# timezone - Default time zone in which the time will be expressed |
| 1295 |
# locale - (Unused) Name of the locale where the time will be scanned. |
| 1296 |
# |
| 1297 |
# Results: |
| 1298 |
# Returns the date and time extracted from the string in seconds from |
| 1299 |
# the epoch |
| 1300 |
# |
| 1301 |
#---------------------------------------------------------------------- |
| 1302 |
|
| 1303 |
proc ::tcl::clock::FreeScan { string base timezone locale } { |
| 1304 |
|
| 1305 |
variable TZData |
| 1306 |
|
| 1307 |
# Get the data for time changes in the given zone |
| 1308 |
|
| 1309 |
try { |
| 1310 |
SetupTimeZone $timezone |
| 1311 |
} on error {retval opts} { |
| 1312 |
dict unset opts -errorinfo |
| 1313 |
return -options $opts $retval |
| 1314 |
} |
| 1315 |
|
| 1316 |
# Extract year, month and day from the base time for the parser to use as |
| 1317 |
# defaults |
| 1318 |
|
| 1319 |
set date [GetDateFields $base $TZData($timezone) 2361222] |
| 1320 |
dict set date secondOfDay [expr { |
| 1321 |
[dict get $date localSeconds] % 86400 |
| 1322 |
}] |
| 1323 |
|
| 1324 |
# Parse the date. The parser will return a list comprising date, time, |
| 1325 |
# time zone, relative month/day/seconds, relative weekday, ordinal month. |
| 1326 |
|
| 1327 |
try { |
| 1328 |
set scanned [Oldscan $string \ |
| 1329 |
[dict get $date year] \ |
| 1330 |
[dict get $date month] \ |
| 1331 |
[dict get $date dayOfMonth]] |
| 1332 |
lassign $scanned \ |
| 1333 |
parseDate parseTime parseZone parseRel \ |
| 1334 |
parseWeekday parseOrdinalMonth |
| 1335 |
} on error message { |
| 1336 |
return -code error \ |
| 1337 |
"unable to convert date-time string \"$string\": $message" |
| 1338 |
} |
| 1339 |
|
| 1340 |
# If the caller supplied a date in the string, update the 'date' dict with |
| 1341 |
# the value. If the caller didn't specify a time with the date, default to |
| 1342 |
# midnight. |
| 1343 |
|
| 1344 |
if { [llength $parseDate] > 0 } { |
| 1345 |
lassign $parseDate y m d |
| 1346 |
if { $y < 100 } { |
| 1347 |
if { $y >= 39 } { |
| 1348 |
incr y 1900 |
| 1349 |
} else { |
| 1350 |
incr y 2000 |
| 1351 |
} |
| 1352 |
} |
| 1353 |
dict set date era CE |
| 1354 |
dict set date year $y |
| 1355 |
dict set date month $m |
| 1356 |
dict set date dayOfMonth $d |
| 1357 |
if { $parseTime eq {} } { |
| 1358 |
set parseTime 0 |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
# If the caller supplied a time zone in the string, it comes back as a |
| 1363 |
# two-element list; the first element is the number of minutes east of |
| 1364 |
# Greenwich, and the second is a Daylight Saving Time indicator (1 == yes, |
| 1365 |
# 0 == no, -1 == unknown). We make it into a time zone indicator of |
| 1366 |
# +-hhmm. |
| 1367 |
|
| 1368 |
if { [llength $parseZone] > 0 } { |
| 1369 |
lassign $parseZone minEast dstFlag |
| 1370 |
set timezone [FormatNumericTimeZone \ |
| 1371 |
[expr { 60 * $minEast + 3600 * $dstFlag }]] |
| 1372 |
SetupTimeZone $timezone |
| 1373 |
} |
| 1374 |
dict set date tzName $timezone |
| 1375 |
|
| 1376 |
# Assemble date, time, zone into seconds-from-epoch |
| 1377 |
|
| 1378 |
set date [GetJulianDayFromEraYearMonthDay $date[set date {}] 2361222] |
| 1379 |
if { $parseTime ne {} } { |
| 1380 |
dict set date secondOfDay $parseTime |
| 1381 |
} elseif { [llength $parseWeekday] != 0 |
| 1382 |
|| [llength $parseOrdinalMonth] != 0 |
| 1383 |
|| ( [llength $parseRel] != 0 |
| 1384 |
&& ( [lindex $parseRel 0] != 0 |
| 1385 |
|| [lindex $parseRel 1] != 0 ) ) } { |
| 1386 |
dict set date secondOfDay 0 |
| 1387 |
} |
| 1388 |
|
| 1389 |
dict set date localSeconds [expr { |
| 1390 |
-210866803200 |
| 1391 |
+ ( 86400 * wide([dict get $date julianDay]) ) |
| 1392 |
+ [dict get $date secondOfDay] |
| 1393 |
}] |
| 1394 |
dict set date tzName $timezone |
| 1395 |
set date [ConvertLocalToUTC $date[set date {}] $TZData($timezone) 2361222] |
| 1396 |
set seconds [dict get $date seconds] |
| 1397 |
|
| 1398 |
# Do relative times |
| 1399 |
|
| 1400 |
if { [llength $parseRel] > 0 } { |
| 1401 |
lassign $parseRel relMonth relDay relSecond |
| 1402 |
set seconds [add $seconds \ |
| 1403 |
$relMonth months $relDay days $relSecond seconds \ |
| 1404 |
-timezone $timezone -locale $locale] |
| 1405 |
} |
| 1406 |
|
| 1407 |
# Do relative weekday |
| 1408 |
|
| 1409 |
if { [llength $parseWeekday] > 0 } { |
| 1410 |
lassign $parseWeekday dayOrdinal dayOfWeek |
| 1411 |
set date2 [GetDateFields $seconds $TZData($timezone) 2361222] |
| 1412 |
dict set date2 era CE |
| 1413 |
set jdwkday [WeekdayOnOrBefore $dayOfWeek [expr { |
| 1414 |
[dict get $date2 julianDay] + 6 |
| 1415 |
}]] |
| 1416 |
incr jdwkday [expr { 7 * $dayOrdinal }] |
| 1417 |
if { $dayOrdinal > 0 } { |
| 1418 |
incr jdwkday -7 |
| 1419 |
} |
| 1420 |
dict set date2 secondOfDay \ |
| 1421 |
[expr { [dict get $date2 localSeconds] % 86400 }] |
| 1422 |
dict set date2 julianDay $jdwkday |
| 1423 |
dict set date2 localSeconds [expr { |
| 1424 |
-210866803200 |
| 1425 |
+ ( 86400 * wide([dict get $date2 julianDay]) ) |
| 1426 |
+ [dict get $date secondOfDay] |
| 1427 |
}] |
| 1428 |
dict set date2 tzName $timezone |
| 1429 |
set date2 [ConvertLocalToUTC $date2[set date2 {}] $TZData($timezone) \ |
| 1430 |
2361222] |
| 1431 |
set seconds [dict get $date2 seconds] |
| 1432 |
|
| 1433 |
} |
| 1434 |
|
| 1435 |
# Do relative month |
| 1436 |
|
| 1437 |
if { [llength $parseOrdinalMonth] > 0 } { |
| 1438 |
lassign $parseOrdinalMonth monthOrdinal monthNumber |
| 1439 |
if { $monthOrdinal > 0 } { |
| 1440 |
set monthDiff [expr { $monthNumber - [dict get $date month] }] |
| 1441 |
if { $monthDiff <= 0 } { |
| 1442 |
incr monthDiff 12 |
| 1443 |
} |
| 1444 |
incr monthOrdinal -1 |
| 1445 |
} else { |
| 1446 |
set monthDiff [expr { [dict get $date month] - $monthNumber }] |
| 1447 |
if { $monthDiff >= 0 } { |
| 1448 |
incr monthDiff -12 |
| 1449 |
} |
| 1450 |
incr monthOrdinal |
| 1451 |
} |
| 1452 |
set seconds [add $seconds $monthOrdinal years $monthDiff months \ |
| 1453 |
-timezone $timezone -locale $locale] |
| 1454 |
} |
| 1455 |
|
| 1456 |
return $seconds |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
#---------------------------------------------------------------------- |
| 1461 |
# |
| 1462 |
# ParseClockScanFormat -- |
| 1463 |
# |
| 1464 |
# Parses a format string given to [clock scan -format] |
| 1465 |
# |
| 1466 |
# Parameters: |
| 1467 |
# formatString - The format being parsed |
| 1468 |
# locale - The current locale |
| 1469 |
# |
| 1470 |
# Results: |
| 1471 |
# Constructs and returns a procedure that accepts the string being |
| 1472 |
# scanned, the base time, and the time zone. The procedure will either |
| 1473 |
# return the scanned time or else throw an error that should be rethrown |
| 1474 |
# to the caller of [clock scan] |
| 1475 |
# |
| 1476 |
# Side effects: |
| 1477 |
# The given procedure is defined in the ::tcl::clock namespace. Scan |
| 1478 |
# procedures are not deleted once installed. |
| 1479 |
# |
| 1480 |
# Why do we parse dates by defining a procedure to parse them? The reason is |
| 1481 |
# that by doing so, we have one convenient place to cache all the information: |
| 1482 |
# the regular expressions that match the patterns (which will be compiled), |
| 1483 |
# the code that assembles the date information, everything lands in one place. |
| 1484 |
# In this way, when a given format is reused at run time, all the information |
| 1485 |
# of how to apply it is available in a single place. |
| 1486 |
# |
| 1487 |
#---------------------------------------------------------------------- |
| 1488 |
|
| 1489 |
proc ::tcl::clock::ParseClockScanFormat {formatString locale} { |
| 1490 |
# Check whether the format has been parsed previously, and return the |
| 1491 |
# existing recognizer if it has. |
| 1492 |
|
| 1493 |
set procName scanproc'$formatString'$locale |
| 1494 |
set procName [namespace current]::[string map {: {\:} \\ {\\}} $procName] |
| 1495 |
if { [namespace which $procName] != {} } { |
| 1496 |
return $procName |
| 1497 |
} |
| 1498 |
|
| 1499 |
variable DateParseActions |
| 1500 |
variable TimeParseActions |
| 1501 |
|
| 1502 |
# Localize the %x, %X, etc. groups |
| 1503 |
|
| 1504 |
set formatString [LocalizeFormat $locale $formatString] |
| 1505 |
|
| 1506 |
# Condense whitespace |
| 1507 |
|
| 1508 |
regsub -all {[[:space:]]+} $formatString { } formatString |
| 1509 |
|
| 1510 |
# Walk through the groups of the format string. In this loop, we |
| 1511 |
# accumulate: |
| 1512 |
# - a regular expression that matches the string, |
| 1513 |
# - the count of capturing brackets in the regexp |
| 1514 |
# - a set of code that post-processes the fields captured by the regexp, |
| 1515 |
# - a dictionary whose keys are the names of fields that are present |
| 1516 |
# in the format string. |
| 1517 |
|
| 1518 |
set re {^[[:space:]]*} |
| 1519 |
set captureCount 0 |
| 1520 |
set postcode {} |
| 1521 |
set fieldSet [dict create] |
| 1522 |
set fieldCount 0 |
| 1523 |
set postSep {} |
| 1524 |
set state {} |
| 1525 |
|
| 1526 |
foreach c [split $formatString {}] { |
| 1527 |
switch -exact -- $state { |
| 1528 |
{} { |
| 1529 |
if { $c eq "%" } { |
| 1530 |
set state % |
| 1531 |
} elseif { $c eq " " } { |
| 1532 |
append re {[[:space:]]+} |
| 1533 |
} else { |
| 1534 |
if { ! [string is alnum $c] } { |
| 1535 |
append re "\\" |
| 1536 |
} |
| 1537 |
append re $c |
| 1538 |
} |
| 1539 |
} |
| 1540 |
% { |
| 1541 |
set state {} |
| 1542 |
switch -exact -- $c { |
| 1543 |
% { |
| 1544 |
append re % |
| 1545 |
} |
| 1546 |
{ } { |
| 1547 |
append re "\[\[:space:\]\]*" |
| 1548 |
} |
| 1549 |
a - A { # Day of week, in words |
| 1550 |
set l {} |
| 1551 |
foreach \ |
| 1552 |
i {7 1 2 3 4 5 6} \ |
| 1553 |
abr [mc DAYS_OF_WEEK_ABBREV] \ |
| 1554 |
full [mc DAYS_OF_WEEK_FULL] { |
| 1555 |
dict set l [string tolower $abr] $i |
| 1556 |
dict set l [string tolower $full] $i |
| 1557 |
incr i |
| 1558 |
} |
| 1559 |
lassign [UniquePrefixRegexp $l] regex lookup |
| 1560 |
append re ( $regex ) |
| 1561 |
dict set fieldSet dayOfWeek [incr fieldCount] |
| 1562 |
append postcode "dict set date dayOfWeek \[" \ |
| 1563 |
"dict get " [list $lookup] " " \ |
| 1564 |
\[ {string tolower $field} [incr captureCount] \] \ |
| 1565 |
"\]\n" |
| 1566 |
} |
| 1567 |
b - B - h { # Name of month |
| 1568 |
set i 0 |
| 1569 |
set l {} |
| 1570 |
foreach \ |
| 1571 |
abr [mc MONTHS_ABBREV] \ |
| 1572 |
full [mc MONTHS_FULL] { |
| 1573 |
incr i |
| 1574 |
dict set l [string tolower $abr] $i |
| 1575 |
dict set l [string tolower $full] $i |
| 1576 |
} |
| 1577 |
lassign [UniquePrefixRegexp $l] regex lookup |
| 1578 |
append re ( $regex ) |
| 1579 |
dict set fieldSet month [incr fieldCount] |
| 1580 |
append postcode "dict set date month \[" \ |
| 1581 |
"dict get " [list $lookup] \ |
| 1582 |
" " \[ {string tolower $field} \ |
| 1583 |
[incr captureCount] \] \ |
| 1584 |
"\]\n" |
| 1585 |
} |
| 1586 |
C { # Gregorian century |
| 1587 |
append re \\s*(\\d\\d?) |
| 1588 |
dict set fieldSet century [incr fieldCount] |
| 1589 |
append postcode "dict set date century \[" \ |
| 1590 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1591 |
"\]\n" |
| 1592 |
} |
| 1593 |
d - e { # Day of month |
| 1594 |
append re \\s*(\\d\\d?) |
| 1595 |
dict set fieldSet dayOfMonth [incr fieldCount] |
| 1596 |
append postcode "dict set date dayOfMonth \[" \ |
| 1597 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1598 |
"\]\n" |
| 1599 |
} |
| 1600 |
E { # Prefix for locale-specific codes |
| 1601 |
set state %E |
| 1602 |
} |
| 1603 |
g { # ISO8601 2-digit year |
| 1604 |
append re \\s*(\\d\\d) |
| 1605 |
dict set fieldSet iso8601YearOfCentury \ |
| 1606 |
[incr fieldCount] |
| 1607 |
append postcode \ |
| 1608 |
"dict set date iso8601YearOfCentury \[" \ |
| 1609 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1610 |
"\]\n" |
| 1611 |
} |
| 1612 |
G { # ISO8601 4-digit year |
| 1613 |
append re \\s*(\\d\\d)(\\d\\d) |
| 1614 |
dict set fieldSet iso8601Century [incr fieldCount] |
| 1615 |
dict set fieldSet iso8601YearOfCentury \ |
| 1616 |
[incr fieldCount] |
| 1617 |
append postcode \ |
| 1618 |
"dict set date iso8601Century \[" \ |
| 1619 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1620 |
"\]\n" \ |
| 1621 |
"dict set date iso8601YearOfCentury \[" \ |
| 1622 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1623 |
"\]\n" |
| 1624 |
} |
| 1625 |
H - k { # Hour of day |
| 1626 |
append re \\s*(\\d\\d?) |
| 1627 |
dict set fieldSet hour [incr fieldCount] |
| 1628 |
append postcode "dict set date hour \[" \ |
| 1629 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1630 |
"\]\n" |
| 1631 |
} |
| 1632 |
I - l { # Hour, AM/PM |
| 1633 |
append re \\s*(\\d\\d?) |
| 1634 |
dict set fieldSet hourAMPM [incr fieldCount] |
| 1635 |
append postcode "dict set date hourAMPM \[" \ |
| 1636 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1637 |
"\]\n" |
| 1638 |
} |
| 1639 |
j { # Day of year |
| 1640 |
append re \\s*(\\d\\d?\\d?) |
| 1641 |
dict set fieldSet dayOfYear [incr fieldCount] |
| 1642 |
append postcode "dict set date dayOfYear \[" \ |
| 1643 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1644 |
"\]\n" |
| 1645 |
} |
| 1646 |
J { # Julian Day Number |
| 1647 |
append re \\s*(\\d+) |
| 1648 |
dict set fieldSet julianDay [incr fieldCount] |
| 1649 |
append postcode "dict set date julianDay \[" \ |
| 1650 |
"::scan \$field" [incr captureCount] " %ld" \ |
| 1651 |
"\]\n" |
| 1652 |
} |
| 1653 |
m - N { # Month number |
| 1654 |
append re \\s*(\\d\\d?) |
| 1655 |
dict set fieldSet month [incr fieldCount] |
| 1656 |
append postcode "dict set date month \[" \ |
| 1657 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1658 |
"\]\n" |
| 1659 |
} |
| 1660 |
M { # Minute |
| 1661 |
append re \\s*(\\d\\d?) |
| 1662 |
dict set fieldSet minute [incr fieldCount] |
| 1663 |
append postcode "dict set date minute \[" \ |
| 1664 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1665 |
"\]\n" |
| 1666 |
} |
| 1667 |
n { # Literal newline |
| 1668 |
append re \\n |
| 1669 |
} |
| 1670 |
O { # Prefix for locale numerics |
| 1671 |
set state %O |
| 1672 |
} |
| 1673 |
p - P { # AM/PM indicator |
| 1674 |
set l [list [string tolower [mc AM]] 0 \ |
| 1675 |
[string tolower [mc PM]] 1] |
| 1676 |
lassign [UniquePrefixRegexp $l] regex lookup |
| 1677 |
append re ( $regex ) |
| 1678 |
dict set fieldSet amPmIndicator [incr fieldCount] |
| 1679 |
append postcode "dict set date amPmIndicator \[" \ |
| 1680 |
"dict get " [list $lookup] " \[string tolower " \ |
| 1681 |
"\$field" \ |
| 1682 |
[incr captureCount] \ |
| 1683 |
"\]\]\n" |
| 1684 |
} |
| 1685 |
Q { # Hi, Jeff! |
| 1686 |
append re {Stardate\s+([-+]?\d+)(\d\d\d)[.](\d)} |
| 1687 |
incr captureCount |
| 1688 |
dict set fieldSet seconds [incr fieldCount] |
| 1689 |
append postcode {dict set date seconds } \[ \ |
| 1690 |
{ParseStarDate $field} [incr captureCount] \ |
| 1691 |
{ $field} [incr captureCount] \ |
| 1692 |
{ $field} [incr captureCount] \ |
| 1693 |
\] \n |
| 1694 |
} |
| 1695 |
s { # Seconds from Posix Epoch |
| 1696 |
# This next case is insanely difficult, because it's |
| 1697 |
# problematic to determine whether the field is |
| 1698 |
# actually within the range of a wide integer. |
| 1699 |
append re {\s*([-+]?\d+)} |
| 1700 |
dict set fieldSet seconds [incr fieldCount] |
| 1701 |
append postcode {dict set date seconds } \[ \ |
| 1702 |
{ScanWide $field} [incr captureCount] \] \n |
| 1703 |
} |
| 1704 |
S { # Second |
| 1705 |
append re \\s*(\\d\\d?) |
| 1706 |
dict set fieldSet second [incr fieldCount] |
| 1707 |
append postcode "dict set date second \[" \ |
| 1708 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1709 |
"\]\n" |
| 1710 |
} |
| 1711 |
t { # Literal tab character |
| 1712 |
append re \\t |
| 1713 |
} |
| 1714 |
u - w { # Day number within week, 0 or 7 == Sun |
| 1715 |
# 1=Mon, 6=Sat |
| 1716 |
append re \\s*(\\d) |
| 1717 |
dict set fieldSet dayOfWeek [incr fieldCount] |
| 1718 |
append postcode {::scan $field} [incr captureCount] \ |
| 1719 |
{ %d dow} \n \ |
| 1720 |
{ |
| 1721 |
if { $dow == 0 } { |
| 1722 |
set dow 7 |
| 1723 |
} elseif { $dow > 7 } { |
| 1724 |
return -code error \ |
| 1725 |
-errorcode [list CLOCK badDayOfWeek] \ |
| 1726 |
"day of week is greater than 7" |
| 1727 |
} |
| 1728 |
dict set date dayOfWeek $dow |
| 1729 |
} |
| 1730 |
} |
| 1731 |
U { # Week of year. The first Sunday of |
| 1732 |
# the year is the first day of week |
| 1733 |
# 01. No scan rule uses this group. |
| 1734 |
append re \\s*\\d\\d? |
| 1735 |
} |
| 1736 |
V { # Week of ISO8601 year |
| 1737 |
|
| 1738 |
append re \\s*(\\d\\d?) |
| 1739 |
dict set fieldSet iso8601Week [incr fieldCount] |
| 1740 |
append postcode "dict set date iso8601Week \[" \ |
| 1741 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1742 |
"\]\n" |
| 1743 |
} |
| 1744 |
W { # Week of the year (00-53). The first |
| 1745 |
# Monday of the year is the first day |
| 1746 |
# of week 01. No scan rule uses this |
| 1747 |
# group. |
| 1748 |
append re \\s*\\d\\d? |
| 1749 |
} |
| 1750 |
y { # Two-digit Gregorian year |
| 1751 |
append re \\s*(\\d\\d?) |
| 1752 |
dict set fieldSet yearOfCentury [incr fieldCount] |
| 1753 |
append postcode "dict set date yearOfCentury \[" \ |
| 1754 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1755 |
"\]\n" |
| 1756 |
} |
| 1757 |
Y { # 4-digit Gregorian year |
| 1758 |
append re \\s*(\\d\\d)(\\d\\d) |
| 1759 |
dict set fieldSet century [incr fieldCount] |
| 1760 |
dict set fieldSet yearOfCentury [incr fieldCount] |
| 1761 |
append postcode \ |
| 1762 |
"dict set date century \[" \ |
| 1763 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1764 |
"\]\n" \ |
| 1765 |
"dict set date yearOfCentury \[" \ |
| 1766 |
"::scan \$field" [incr captureCount] " %d" \ |
| 1767 |
"\]\n" |
| 1768 |
} |
| 1769 |
z - Z { # Time zone name |
| 1770 |
append re {(?:([-+]\d\d(?::?\d\d(?::?\d\d)?)?)|([[:alnum:]]{1,4}))} |
| 1771 |
dict set fieldSet tzName [incr fieldCount] |
| 1772 |
append postcode \ |
| 1773 |
{if } \{ { $field} [incr captureCount] \ |
| 1774 |
{ ne "" } \} { } \{ \n \ |
| 1775 |
{dict set date tzName $field} \ |
| 1776 |
$captureCount \n \ |
| 1777 |
\} { else } \{ \n \ |
| 1778 |
{dict set date tzName } \[ \ |
| 1779 |
{ConvertLegacyTimeZone $field} \ |
| 1780 |
[incr captureCount] \] \n \ |
| 1781 |
\} \n \ |
| 1782 |
} |
| 1783 |
% { # Literal percent character |
| 1784 |
append re % |
| 1785 |
} |
| 1786 |
default { |
| 1787 |
append re % |
| 1788 |
if { ! [string is alnum $c] } { |
| 1789 |
append re \\ |
| 1790 |
} |
| 1791 |
append re $c |
| 1792 |
} |
| 1793 |
} |
| 1794 |
} |
| 1795 |
%E { |
| 1796 |
switch -exact -- $c { |
| 1797 |
C { # Locale-dependent era |
| 1798 |
set d {} |
| 1799 |
foreach triple [mc LOCALE_ERAS] { |
| 1800 |
lassign $triple t symbol year |
| 1801 |
dict set d [string tolower $symbol] $year |
| 1802 |
} |
| 1803 |
lassign [UniquePrefixRegexp $d] regex lookup |
| 1804 |
append re (?: $regex ) |
| 1805 |
} |
| 1806 |
E { |
| 1807 |
set l {} |
| 1808 |
dict set l [string tolower [mc BCE]] BCE |
| 1809 |
dict set l [string tolower [mc CE]] CE |
| 1810 |
dict set l b.c.e. BCE |
| 1811 |
dict set l c.e. CE |
| 1812 |
dict set l b.c. BCE |
| 1813 |
dict set l a.d. CE |
| 1814 |
lassign [UniquePrefixRegexp $l] regex lookup |
| 1815 |
append re ( $regex ) |
| 1816 |
dict set fieldSet era [incr fieldCount] |
| 1817 |
append postcode "dict set date era \["\ |
| 1818 |
"dict get " [list $lookup] \ |
| 1819 |
{ } \[ {string tolower $field} \ |
| 1820 |
[incr captureCount] \] \ |
| 1821 |
"\]\n" |
| 1822 |
} |
| 1823 |
y { # Locale-dependent year of the era |
| 1824 |
lassign [LocaleNumeralMatcher $locale] regex lookup |
| 1825 |
append re $regex |
| 1826 |
incr captureCount |
| 1827 |
} |
| 1828 |
default { |
| 1829 |
append re %E |
| 1830 |
if { ! [string is alnum $c] } { |
| 1831 |
append re \\ |
| 1832 |
} |
| 1833 |
append re $c |
| 1834 |
} |
| 1835 |
} |
| 1836 |
set state {} |
| 1837 |
} |
| 1838 |
%O { |
| 1839 |
switch -exact -- $c { |
| 1840 |
d - e { |
| 1841 |
lassign [LocaleNumeralMatcher $locale] regex lookup |
| 1842 |
append re $regex |
| 1843 |
dict set fieldSet dayOfMonth [incr fieldCount] |
| 1844 |
append postcode "dict set date dayOfMonth \[" \ |
| 1845 |
"dict get " [list $lookup] " \$field" \ |
| 1846 |
[incr captureCount] \ |
| 1847 |
"\]\n" |
| 1848 |
} |
| 1849 |
H - k { |
| 1850 |
lassign [LocaleNumeralMatcher $locale] regex lookup |
| 1851 |
append re $regex |
| 1852 |
dict set fieldSet hour [incr fieldCount] |
| 1853 |
append postcode "dict set date hour \[" \ |
| 1854 |
"dict get " [list $lookup] " \$field" \ |
| 1855 |
[incr captureCount] \ |
| 1856 |
"\]\n" |
| 1857 |
} |
| 1858 |
I - l { |
| 1859 |
lassign [LocaleNumeralMatcher $locale] regex lookup |
| 1860 |
append re $regex |
| 1861 |
dict set fieldSet hourAMPM [incr fieldCount] |
| 1862 |
append postcode "dict set date hourAMPM \[" \ |
| 1863 |
"dict get " [list $lookup] " \$field" \ |
| 1864 |
[incr captureCount] \ |
| 1865 |
"\]\n" |
| 1866 |
} |
| 1867 |
m { |
| 1868 |
lassign [LocaleNumeralMatcher $locale] regex lookup |
| 1869 |
append re $regex |
| 1870 |
dict set fieldSet month [incr fieldCount] |
| 1871 |
append postcode "dict set date month \[" \ |
| 1872 |
"dict get " [list $lookup] " \$field" \ |
| 1873 |
[incr captureCount] \ |
| 1874 |
"\]\n" |
| 1875 |
} |
| 1876 |
M { |
| 1877 |
lassign [LocaleNumeralMatcher $locale] regex lookup |
| 1878 |
append re $regex |
| 1879 |
dict set fieldSet minute [incr fieldCount] |
| 1880 |
append postcode "dict set date minute \[" \ |
| 1881 |
"dict get " [list $lookup] " \$field" \ |
| 1882 |
[incr captureCount] \ |
| 1883 |
"\]\n" |
| 1884 |
} |
| 1885 |
S { |
| 1886 |
lassign [LocaleNumeralMatcher $locale] regex lookup |
| 1887 |
append re $regex |
| 1888 |
dict set fieldSet second [incr fieldCount] |
| 1889 |
append postcode "dict set date second \[" \ |
| 1890 |
"dict get " [list $lookup] " \$field" \ |
| 1891 |
[incr captureCount] \ |
| 1892 |
"\]\n" |
| 1893 |
} |
| 1894 |
u - w { |
| 1895 |
lassign [LocaleNumeralMatcher $locale] regex lookup |
| 1896 |
append re $regex |
| 1897 |
dict set fieldSet dayOfWeek [incr fieldCount] |
| 1898 |
append postcode "set dow \[dict get " [list $lookup] \ |
| 1899 |
{ $field} [incr captureCount] \] \n \ |
| 1900 |
{ |
| 1901 |
if { $dow == 0 } { |
| 1902 |
set dow 7 |
| 1903 |
} elseif { $dow > 7 } { |
| 1904 |
return -code error \ |
| 1905 |
-errorcode [list CLOCK badDayOfWeek] \ |
| 1906 |
"day of week is greater than 7" |
| 1907 |
} |
| 1908 |
dict set date dayOfWeek $dow |
| 1909 |
} |
| 1910 |
} |
| 1911 |
y { |
| 1912 |
lassign [LocaleNumeralMatcher $locale] regex lookup |
| 1913 |
append re $regex |
| 1914 |
dict set fieldSet yearOfCentury [incr fieldCount] |
| 1915 |
append postcode {dict set date yearOfCentury } \[ \ |
| 1916 |
{dict get } [list $lookup] { $field} \ |
| 1917 |
[incr captureCount] \] \n |
| 1918 |
} |
| 1919 |
default { |
| 1920 |
append re %O |
| 1921 |
if { ! [string is alnum $c] } { |
| 1922 |
append re \\ |
| 1923 |
} |
| 1924 |
append re $c |
| 1925 |
} |
| 1926 |
} |
| 1927 |
set state {} |
| 1928 |
} |
| 1929 |
} |
| 1930 |
} |
| 1931 |
|
| 1932 |
# Clean up any unfinished format groups |
| 1933 |
|
| 1934 |
append re $state \\s*\$ |
| 1935 |
|
| 1936 |
# Build the procedure |
| 1937 |
|
| 1938 |
set procBody {} |
| 1939 |
append procBody "variable ::tcl::clock::TZData" \n |
| 1940 |
append procBody "if \{ !\[ regexp -nocase [list $re] \$string ->" |
| 1941 |
for { set i 1 } { $i <= $captureCount } { incr i } { |
| 1942 |
append procBody " " field $i |
| 1943 |
} |
| 1944 |
append procBody "\] \} \{" \n |
| 1945 |
append procBody { |
| 1946 |
return -code error -errorcode [list CLOCK badInputString] \ |
| 1947 |
{input string does not match supplied format} |
| 1948 |
} |
| 1949 |
append procBody \}\n |
| 1950 |
append procBody "set date \[dict create\]" \n |
| 1951 |
append procBody {dict set date tzName $timeZone} \n |
| 1952 |
append procBody $postcode |
| 1953 |
append procBody [list set changeover [mc GREGORIAN_CHANGE_DATE]] \n |
| 1954 |
|
| 1955 |
# Set up the time zone before doing anything with a default base date |
| 1956 |
# that might need a timezone to interpret it. |
| 1957 |
|
| 1958 |
if { ![dict exists $fieldSet seconds] |
| 1959 |
&& ![dict exists $fieldSet starDate] } { |
| 1960 |
if { [dict exists $fieldSet tzName] } { |
| 1961 |
append procBody { |
| 1962 |
set timeZone [dict get $date tzName] |
| 1963 |
} |
| 1964 |
} |
| 1965 |
append procBody { |
| 1966 |
::tcl::clock::SetupTimeZone $timeZone |
| 1967 |
} |
| 1968 |
} |
| 1969 |
|
| 1970 |
# Add code that gets Julian Day Number from the fields. |
| 1971 |
|
| 1972 |
append procBody [MakeParseCodeFromFields $fieldSet $DateParseActions] |
| 1973 |
|
| 1974 |
# Get time of day |
| 1975 |
|
| 1976 |
append procBody [MakeParseCodeFromFields $fieldSet $TimeParseActions] |
| 1977 |
|
| 1978 |
# Assemble seconds from the Julian day and second of the day. |
| 1979 |
# Convert to local time unless epoch seconds or stardate are |
| 1980 |
# being processed - they're always absolute |
| 1981 |
|
| 1982 |
if { ![dict exists $fieldSet seconds] |
| 1983 |
&& ![dict exists $fieldSet starDate] } { |
| 1984 |
append procBody { |
| 1985 |
if { [dict get $date julianDay] > 5373484 } { |
| 1986 |
return -code error -errorcode [list CLOCK dateTooLarge] \ |
| 1987 |
"requested date too large to represent" |
| 1988 |
} |
| 1989 |
dict set date localSeconds [expr { |
| 1990 |
-210866803200 |
| 1991 |
+ ( 86400 * wide([dict get $date julianDay]) ) |
| 1992 |
+ [dict get $date secondOfDay] |
| 1993 |
}] |
| 1994 |
} |
| 1995 |
|
| 1996 |
# Finally, convert the date to local time |
| 1997 |
|
| 1998 |
append procBody { |
| 1999 |
set date [::tcl::clock::ConvertLocalToUTC $date[set date {}] \ |
| 2000 |
$TZData($timeZone) $changeover] |
| 2001 |
} |
| 2002 |
} |
| 2003 |
|
| 2004 |
# Return result |
| 2005 |
|
| 2006 |
append procBody {return [dict get $date seconds]} \n |
| 2007 |
|
| 2008 |
proc $procName { string baseTime timeZone } $procBody |
| 2009 |
|
| 2010 |
# puts [list proc $procName [list string baseTime timeZone] $procBody] |
| 2011 |
|
| 2012 |
return $procName |
| 2013 |
} |
| 2014 |
|
| 2015 |
#---------------------------------------------------------------------- |
| 2016 |
# |
| 2017 |
# LocaleNumeralMatcher -- |
| 2018 |
# |
| 2019 |
# Composes a regexp that captures the numerals in the given locale, and |
| 2020 |
# a dictionary to map them to conventional numerals. |
| 2021 |
# |
| 2022 |
# Parameters: |
| 2023 |
# locale - Name of the current locale |
| 2024 |
# |
| 2025 |
# Results: |
| 2026 |
# Returns a two-element list comprising the regexp and the dictionary. |
| 2027 |
# |
| 2028 |
# Side effects: |
| 2029 |
# Caches the result. |
| 2030 |
# |
| 2031 |
#---------------------------------------------------------------------- |
| 2032 |
|
| 2033 |
proc ::tcl::clock::LocaleNumeralMatcher {l} { |
| 2034 |
variable LocaleNumeralCache |
| 2035 |
|
| 2036 |
if { ![dict exists $LocaleNumeralCache $l] } { |
| 2037 |
set d {} |
| 2038 |
set i 0 |
| 2039 |
set sep \( |
| 2040 |
foreach n [mc LOCALE_NUMERALS] { |
| 2041 |
dict set d $n $i |
| 2042 |
regsub -all {[^[:alnum:]]} $n \\\\& subex |
| 2043 |
append re $sep $subex |
| 2044 |
set sep | |
| 2045 |
incr i |
| 2046 |
} |
| 2047 |
append re \) |
| 2048 |
dict set LocaleNumeralCache $l [list $re $d] |
| 2049 |
} |
| 2050 |
return [dict get $LocaleNumeralCache $l] |
| 2051 |
} |
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
#---------------------------------------------------------------------- |
| 2056 |
# |
| 2057 |
# UniquePrefixRegexp -- |
| 2058 |
# |
| 2059 |
# Composes a regexp that performs unique-prefix matching. The RE |
| 2060 |
# matches one of a supplied set of strings, or any unique prefix |
| 2061 |
# thereof. |
| 2062 |
# |
| 2063 |
# Parameters: |
| 2064 |
# data - List of alternating match-strings and values. |
| 2065 |
# Match-strings with distinct values are considered |
| 2066 |
# distinct. |
| 2067 |
# |
| 2068 |
# Results: |
| 2069 |
# Returns a two-element list. The first is a regexp that matches any |
| 2070 |
# unique prefix of any of the strings. The second is a dictionary whose |
| 2071 |
# keys are match values from the regexp and whose values are the |
| 2072 |
# corresponding values from 'data'. |
| 2073 |
# |
| 2074 |
# Side effects: |
| 2075 |
# None. |
| 2076 |
# |
| 2077 |
#---------------------------------------------------------------------- |
| 2078 |
|
| 2079 |
proc ::tcl::clock::UniquePrefixRegexp { data } { |
| 2080 |
# The 'successors' dictionary will contain, for each string that is a |
| 2081 |
# prefix of any key, all characters that may follow that prefix. The |
| 2082 |
# 'prefixMapping' dictionary will have keys that are prefixes of keys and |
| 2083 |
# values that correspond to the keys. |
| 2084 |
|
| 2085 |
set prefixMapping [dict create] |
| 2086 |
set successors [dict create {} {}] |
| 2087 |
|
| 2088 |
# Walk the key-value pairs |
| 2089 |
|
| 2090 |
foreach { key value } $data { |
| 2091 |
# Construct all prefixes of the key; |
| 2092 |
|
| 2093 |
set prefix {} |
| 2094 |
foreach char [split $key {}] { |
| 2095 |
set oldPrefix $prefix |
| 2096 |
dict set successors $oldPrefix $char {} |
| 2097 |
append prefix $char |
| 2098 |
|
| 2099 |
# Put the prefixes in the 'prefixMapping' and 'successors' |
| 2100 |
# dictionaries |
| 2101 |
|
| 2102 |
dict lappend prefixMapping $prefix $value |
| 2103 |
if { ![dict exists $successors $prefix] } { |
| 2104 |
dict set successors $prefix {} |
| 2105 |
} |
| 2106 |
} |
| 2107 |
} |
| 2108 |
|
| 2109 |
# Identify those prefixes that designate unique values, and those that are |
| 2110 |
# the full keys |
| 2111 |
|
| 2112 |
set uniquePrefixMapping {} |
| 2113 |
dict for { key valueList } $prefixMapping { |
| 2114 |
if { [llength $valueList] == 1 } { |
| 2115 |
dict set uniquePrefixMapping $key [lindex $valueList 0] |
| 2116 |
} |
| 2117 |
} |
| 2118 |
foreach { key value } $data { |
| 2119 |
dict set uniquePrefixMapping $key $value |
| 2120 |
} |
| 2121 |
|
| 2122 |
# Construct the re. |
| 2123 |
|
| 2124 |
return [list \ |
| 2125 |
[MakeUniquePrefixRegexp $successors $uniquePrefixMapping {}] \ |
| 2126 |
$uniquePrefixMapping] |
| 2127 |
} |
| 2128 |
|
| 2129 |
#---------------------------------------------------------------------- |
| 2130 |
# |
| 2131 |
# MakeUniquePrefixRegexp -- |
| 2132 |
# |
| 2133 |
# Service procedure for 'UniquePrefixRegexp' that constructs a regular |
| 2134 |
# expresison that matches the unique prefixes. |
| 2135 |
# |
| 2136 |
# Parameters: |
| 2137 |
# successors - Dictionary whose keys are all prefixes |
| 2138 |
# of keys passed to 'UniquePrefixRegexp' and whose |
| 2139 |
# values are dictionaries whose keys are the characters |
| 2140 |
# that may follow those prefixes. |
| 2141 |
# uniquePrefixMapping - Dictionary whose keys are the unique |
| 2142 |
# prefixes and whose values are not examined. |
| 2143 |
# prefixString - Current prefix being processed. |
| 2144 |
# |
| 2145 |
# Results: |
| 2146 |
# Returns a constructed regular expression that matches the set of |
| 2147 |
# unique prefixes beginning with the 'prefixString'. |
| 2148 |
# |
| 2149 |
# Side effects: |
| 2150 |
# None. |
| 2151 |
# |
| 2152 |
#---------------------------------------------------------------------- |
| 2153 |
|
| 2154 |
proc ::tcl::clock::MakeUniquePrefixRegexp { successors |
| 2155 |
uniquePrefixMapping |
| 2156 |
prefixString } { |
| 2157 |
|
| 2158 |
# Get the characters that may follow the current prefix string |
| 2159 |
|
| 2160 |
set schars [lsort -ascii [dict keys [dict get $successors $prefixString]]] |
| 2161 |
if { [llength $schars] == 0 } { |
| 2162 |
return {} |
| 2163 |
} |
| 2164 |
|
| 2165 |
# If there is more than one successor character, or if the current prefix |
| 2166 |
# is a unique prefix, surround the generated re with non-capturing |
| 2167 |
# parentheses. |
| 2168 |
|
| 2169 |
set re {} |
| 2170 |
if { |
| 2171 |
[dict exists $uniquePrefixMapping $prefixString] |
| 2172 |
|| [llength $schars] > 1 |
| 2173 |
} then { |
| 2174 |
append re "(?:" |
| 2175 |
} |
| 2176 |
|
| 2177 |
# Generate a regexp that matches the successors. |
| 2178 |
|
| 2179 |
set sep "" |
| 2180 |
foreach { c } $schars { |
| 2181 |
set nextPrefix $prefixString$c |
| 2182 |
regsub -all {[^[:alnum:]]} $c \\\\& rechar |
| 2183 |
append re $sep $rechar \ |
| 2184 |
[MakeUniquePrefixRegexp \ |
| 2185 |
$successors $uniquePrefixMapping $nextPrefix] |
| 2186 |
set sep | |
| 2187 |
} |
| 2188 |
|
| 2189 |
# If the current prefix is a unique prefix, make all following text |
| 2190 |
# optional. Otherwise, if there is more than one successor character, |
| 2191 |
# close the non-capturing parentheses. |
| 2192 |
|
| 2193 |
if { [dict exists $uniquePrefixMapping $prefixString] } { |
| 2194 |
append re ")?" |
| 2195 |
} elseif { [llength $schars] > 1 } { |
| 2196 |
append re ")" |
| 2197 |
} |
| 2198 |
|
| 2199 |
return $re |
| 2200 |
} |
| 2201 |
|
| 2202 |
#---------------------------------------------------------------------- |
| 2203 |
# |
| 2204 |
# MakeParseCodeFromFields -- |
| 2205 |
# |
| 2206 |
# Composes Tcl code to extract the Julian Day Number from a dictionary |
| 2207 |
# containing date fields. |
| 2208 |
# |
| 2209 |
# Parameters: |
| 2210 |
# dateFields -- Dictionary whose keys are fields of the date, |
| 2211 |
# and whose values are the rightmost positions |
| 2212 |
# at which those fields appear. |
| 2213 |
# parseActions -- List of triples: field set, priority, and |
| 2214 |
# code to emit. Smaller priorities are better, and |
| 2215 |
# the list must be in ascending order by priority |
| 2216 |
# |
| 2217 |
# Results: |
| 2218 |
# Returns a burst of code that extracts the day number from the given |
| 2219 |
# date. |
| 2220 |
# |
| 2221 |
# Side effects: |
| 2222 |
# None. |
| 2223 |
# |
| 2224 |
#---------------------------------------------------------------------- |
| 2225 |
|
| 2226 |
proc ::tcl::clock::MakeParseCodeFromFields { dateFields parseActions } { |
| 2227 |
|
| 2228 |
set currPrio 999 |
| 2229 |
set currFieldPos [list] |
| 2230 |
set currCodeBurst { |
| 2231 |
error "in ::tcl::clock::MakeParseCodeFromFields: can't happen" |
| 2232 |
} |
| 2233 |
|
| 2234 |
foreach { fieldSet prio parseAction } $parseActions { |
| 2235 |
# If we've found an answer that's better than any that follow, quit |
| 2236 |
# now. |
| 2237 |
|
| 2238 |
if { $prio > $currPrio } { |
| 2239 |
break |
| 2240 |
} |
| 2241 |
|
| 2242 |
# Accumulate the field positions that are used in the current field |
| 2243 |
# grouping. |
| 2244 |
|
| 2245 |
set fieldPos [list] |
| 2246 |
set ok true |
| 2247 |
foreach field $fieldSet { |
| 2248 |
if { ! [dict exists $dateFields $field] } { |
| 2249 |
set ok 0 |
| 2250 |
break |
| 2251 |
} |
| 2252 |
lappend fieldPos [dict get $dateFields $field] |
| 2253 |
} |
| 2254 |
|
| 2255 |
# Quit if we don't have a complete set of fields |
| 2256 |
if { !$ok } { |
| 2257 |
continue |
| 2258 |
} |
| 2259 |
|
| 2260 |
# Determine whether the current answer is better than the last. |
| 2261 |
|
| 2262 |
set fPos [lsort -integer -decreasing $fieldPos] |
| 2263 |
|
| 2264 |
if { $prio == $currPrio } { |
| 2265 |
foreach currPos $currFieldPos newPos $fPos { |
| 2266 |
if { |
| 2267 |
![string is integer $newPos] |
| 2268 |
|| ![string is integer $currPos] |
| 2269 |
|| $newPos > $currPos |
| 2270 |
} then { |
| 2271 |
break |
| 2272 |
} |
| 2273 |
if { $newPos < $currPos } { |
| 2274 |
set ok 0 |
| 2275 |
break |
| 2276 |
} |
| 2277 |
} |
| 2278 |
} |
| 2279 |
if { !$ok } { |
| 2280 |
continue |
| 2281 |
} |
| 2282 |
|
| 2283 |
# Remember the best possibility for extracting date information |
| 2284 |
|
| 2285 |
set currPrio $prio |
| 2286 |
set currFieldPos $fPos |
| 2287 |
set currCodeBurst $parseAction |
| 2288 |
} |
| 2289 |
|
| 2290 |
return $currCodeBurst |
| 2291 |
} |
| 2292 |
|
| 2293 |
#---------------------------------------------------------------------- |
| 2294 |
# |
| 2295 |
# EnterLocale -- |
| 2296 |
# |
| 2297 |
# Switch [mclocale] to a given locale if necessary |
| 2298 |
# |
| 2299 |
# Parameters: |
| 2300 |
# locale -- Desired locale |
| 2301 |
# |
| 2302 |
# Results: |
| 2303 |
# Returns the locale that was previously current. |
| 2304 |
# |
| 2305 |
# Side effects: |
| 2306 |
# Does [mclocale]. If necessary, loads the designated locale's files. |
| 2307 |
# |
| 2308 |
#---------------------------------------------------------------------- |
| 2309 |
|
| 2310 |
proc ::tcl::clock::EnterLocale { locale } { |
| 2311 |
if { $locale eq {system} } { |
| 2312 |
if { $::tcl_platform(platform) ne {windows} } { |
| 2313 |
# On a non-windows platform, the 'system' locale is the same as |
| 2314 |
# the 'current' locale |
| 2315 |
|
| 2316 |
set locale current |
| 2317 |
} else { |
| 2318 |
# On a windows platform, the 'system' locale is adapted from the |
| 2319 |
# 'current' locale by applying the date and time formats from the |
| 2320 |
# Control Panel. First, load the 'current' locale if it's not yet |
| 2321 |
# loaded |
| 2322 |
|
| 2323 |
mcpackagelocale set [mclocale] |
| 2324 |
|
| 2325 |
# Make a new locale string for the system locale, and get the |
| 2326 |
# Control Panel information |
| 2327 |
|
| 2328 |
set locale [mclocale]_windows |
| 2329 |
if { ! [mcpackagelocale present $locale] } { |
| 2330 |
LoadWindowsDateTimeFormats $locale |
| 2331 |
} |
| 2332 |
} |
| 2333 |
} |
| 2334 |
if { $locale eq {current}} { |
| 2335 |
set locale [mclocale] |
| 2336 |
} |
| 2337 |
# Eventually load the locale |
| 2338 |
mcpackagelocale set $locale |
| 2339 |
} |
| 2340 |
|
| 2341 |
#---------------------------------------------------------------------- |
| 2342 |
# |
| 2343 |
# LoadWindowsDateTimeFormats -- |
| 2344 |
# |
| 2345 |
# Load the date/time formats from the Control Panel in Windows and |
| 2346 |
# convert them so that they're usable by Tcl. |
| 2347 |
# |
| 2348 |
# Parameters: |
| 2349 |
# locale - Name of the locale in whose message catalog |
| 2350 |
# the converted formats are to be stored. |
| 2351 |
# |
| 2352 |
# Results: |
| 2353 |
# None. |
| 2354 |
# |
| 2355 |
# Side effects: |
| 2356 |
# Updates the given message catalog with the locale strings. |
| 2357 |
# |
| 2358 |
# Presumes that on entry, [mclocale] is set to the current locale, so that |
| 2359 |
# default strings can be obtained if the Registry query fails. |
| 2360 |
# |
| 2361 |
#---------------------------------------------------------------------- |
| 2362 |
|
| 2363 |
proc ::tcl::clock::LoadWindowsDateTimeFormats { locale } { |
| 2364 |
# Bail out if we can't find the Registry |
| 2365 |
|
| 2366 |
variable NoRegistry |
| 2367 |
if { [info exists NoRegistry] } return |
| 2368 |
|
| 2369 |
if { ![catch { |
| 2370 |
registry get "HKEY_CURRENT_USER\\Control Panel\\International" \ |
| 2371 |
sShortDate |
| 2372 |
} string] } { |
| 2373 |
set quote {} |
| 2374 |
set datefmt {} |
| 2375 |
foreach { unquoted quoted } [split $string '] { |
| 2376 |
append datefmt $quote [string map { |
| 2377 |
dddd %A |
| 2378 |
ddd %a |
| 2379 |
dd %d |
| 2380 |
d %e |
| 2381 |
MMMM %B |
| 2382 |
MMM %b |
| 2383 |
MM %m |
| 2384 |
M %N |
| 2385 |
yyyy %Y |
| 2386 |
yy %y |
| 2387 |
y %y |
| 2388 |
gg {} |
| 2389 |
} $unquoted] |
| 2390 |
if { $quoted eq {} } { |
| 2391 |
set quote ' |
| 2392 |
} else { |
| 2393 |
set quote $quoted |
| 2394 |
} |
| 2395 |
} |
| 2396 |
::msgcat::mcset $locale DATE_FORMAT $datefmt |
| 2397 |
} |
| 2398 |
|
| 2399 |
if { ![catch { |
| 2400 |
registry get "HKEY_CURRENT_USER\\Control Panel\\International" \ |
| 2401 |
sLongDate |
| 2402 |
} string] } { |
| 2403 |
set quote {} |
| 2404 |
set ldatefmt {} |
| 2405 |
foreach { unquoted quoted } [split $string '] { |
| 2406 |
append ldatefmt $quote [string map { |
| 2407 |
dddd %A |
| 2408 |
ddd %a |
| 2409 |
dd %d |
| 2410 |
d %e |
| 2411 |
MMMM %B |
| 2412 |
MMM %b |
| 2413 |
MM %m |
| 2414 |
M %N |
| 2415 |
yyyy %Y |
| 2416 |
yy %y |
| 2417 |
y %y |
| 2418 |
gg {} |
| 2419 |
} $unquoted] |
| 2420 |
if { $quoted eq {} } { |
| 2421 |
set quote ' |
| 2422 |
} else { |
| 2423 |
set quote $quoted |
| 2424 |
} |
| 2425 |
} |
| 2426 |
::msgcat::mcset $locale LOCALE_DATE_FORMAT $ldatefmt |
| 2427 |
} |
| 2428 |
|
| 2429 |
if { ![catch { |
| 2430 |
registry get "HKEY_CURRENT_USER\\Control Panel\\International" \ |
| 2431 |
sTimeFormat |
| 2432 |
} string] } { |
| 2433 |
set quote {} |
| 2434 |
set timefmt {} |
| 2435 |
foreach { unquoted quoted } [split $string '] { |
| 2436 |
append timefmt $quote [string map { |
| 2437 |
HH %H |
| 2438 |
H %k |
| 2439 |
hh %I |
| 2440 |
h %l |
| 2441 |
mm %M |
| 2442 |
m %M |
| 2443 |
ss %S |
| 2444 |
s %S |
| 2445 |
tt %p |
| 2446 |
t %p |
| 2447 |
} $unquoted] |
| 2448 |
if { $quoted eq {} } { |
| 2449 |
set quote ' |
| 2450 |
} else { |
| 2451 |
set quote $quoted |
| 2452 |
} |
| 2453 |
} |
| 2454 |
::msgcat::mcset $locale TIME_FORMAT $timefmt |
| 2455 |
} |
| 2456 |
|
| 2457 |
catch { |
| 2458 |
::msgcat::mcset $locale DATE_TIME_FORMAT "$datefmt $timefmt" |
| 2459 |
} |
| 2460 |
catch { |
| 2461 |
::msgcat::mcset $locale LOCALE_DATE_TIME_FORMAT "$ldatefmt $timefmt" |
| 2462 |
} |
| 2463 |
|
| 2464 |
return |
| 2465 |
|
| 2466 |
} |
| 2467 |
|
| 2468 |
#---------------------------------------------------------------------- |
| 2469 |
# |
| 2470 |
# LocalizeFormat -- |
| 2471 |
# |
| 2472 |
# Map away locale-dependent format groups in a clock format. |
| 2473 |
# |
| 2474 |
# Parameters: |
| 2475 |
# locale -- Current [mclocale] locale, supplied to avoid |
| 2476 |
# an extra call |
| 2477 |
# format -- Format supplied to [clock scan] or [clock format] |
| 2478 |
# |
| 2479 |
# Results: |
| 2480 |
# Returns the string with locale-dependent composite format groups |
| 2481 |
# substituted out. |
| 2482 |
# |
| 2483 |
# Side effects: |
| 2484 |
# None. |
| 2485 |
# |
| 2486 |
#---------------------------------------------------------------------- |
| 2487 |
|
| 2488 |
proc ::tcl::clock::LocalizeFormat { locale format } { |
| 2489 |
|
| 2490 |
# message catalog key to cache this format |
| 2491 |
set key FORMAT_$format |
| 2492 |
|
| 2493 |
if { [::msgcat::mcexists -exactlocale -exactnamespace $key] } { |
| 2494 |
return [mc $key] |
| 2495 |
} |
| 2496 |
# Handle locale-dependent format groups by mapping them out of the format |
| 2497 |
# string. Note that the order of the [string map] operations is |
| 2498 |
# significant because later formats can refer to later ones; for example |
| 2499 |
# %c can refer to %X, which in turn can refer to %T. |
| 2500 |
|
| 2501 |
set list { |
| 2502 |
%% %% |
| 2503 |
%D %m/%d/%Y |
| 2504 |
%+ {%a %b %e %H:%M:%S %Z %Y} |
| 2505 |
} |
| 2506 |
lappend list %EY [string map $list [mc LOCALE_YEAR_FORMAT]] |
| 2507 |
lappend list %T [string map $list [mc TIME_FORMAT_24_SECS]] |
| 2508 |
lappend list %R [string map $list [mc TIME_FORMAT_24]] |
| 2509 |
lappend list %r [string map $list [mc TIME_FORMAT_12]] |
| 2510 |
lappend list %X [string map $list [mc TIME_FORMAT]] |
| 2511 |
lappend list %EX [string map $list [mc LOCALE_TIME_FORMAT]] |
| 2512 |
lappend list %x [string map $list [mc DATE_FORMAT]] |
| 2513 |
lappend list %Ex [string map $list [mc LOCALE_DATE_FORMAT]] |
| 2514 |
lappend list %c [string map $list [mc DATE_TIME_FORMAT]] |
| 2515 |
lappend list %Ec [string map $list [mc LOCALE_DATE_TIME_FORMAT]] |
| 2516 |
set format [string map $list $format] |
| 2517 |
|
| 2518 |
::msgcat::mcset $locale $key $format |
| 2519 |
return $format |
| 2520 |
} |
| 2521 |
|
| 2522 |
#---------------------------------------------------------------------- |
| 2523 |
# |
| 2524 |
# FormatNumericTimeZone -- |
| 2525 |
# |
| 2526 |
# Formats a time zone as +hhmmss |
| 2527 |
# |
| 2528 |
# Parameters: |
| 2529 |
# z - Time zone in seconds east of Greenwich |
| 2530 |
# |
| 2531 |
# Results: |
| 2532 |
# Returns the time zone formatted in a numeric form |
| 2533 |
# |
| 2534 |
# Side effects: |
| 2535 |
# None. |
| 2536 |
# |
| 2537 |
#---------------------------------------------------------------------- |
| 2538 |
|
| 2539 |
proc ::tcl::clock::FormatNumericTimeZone { z } { |
| 2540 |
if { $z < 0 } { |
| 2541 |
set z [expr { - $z }] |
| 2542 |
set retval - |
| 2543 |
} else { |
| 2544 |
set retval + |
| 2545 |
} |
| 2546 |
append retval [::format %02d [expr { $z / 3600 }]] |
| 2547 |
set z [expr { $z % 3600 }] |
| 2548 |
append retval [::format %02d [expr { $z / 60 }]] |
| 2549 |
set z [expr { $z % 60 }] |
| 2550 |
if { $z != 0 } { |
| 2551 |
append retval [::format %02d $z] |
| 2552 |
} |
| 2553 |
return $retval |
| 2554 |
} |
| 2555 |
|
| 2556 |
#---------------------------------------------------------------------- |
| 2557 |
# |
| 2558 |
# FormatStarDate -- |
| 2559 |
# |
| 2560 |
# Formats a date as a StarDate. |
| 2561 |
# |
| 2562 |
# Parameters: |
| 2563 |
# date - Dictionary containing 'year', 'dayOfYear', and |
| 2564 |
# 'localSeconds' fields. |
| 2565 |
# |
| 2566 |
# Results: |
| 2567 |
# Returns the given date formatted as a StarDate. |
| 2568 |
# |
| 2569 |
# Side effects: |
| 2570 |
# None. |
| 2571 |
# |
| 2572 |
# Jeff Hobbs put this in to support an atrocious pun about Tcl being |
| 2573 |
# "Enterprise ready." Now we're stuck with it. |
| 2574 |
# |
| 2575 |
#---------------------------------------------------------------------- |
| 2576 |
|
| 2577 |
proc ::tcl::clock::FormatStarDate { date } { |
| 2578 |
variable Roddenberry |
| 2579 |
|
| 2580 |
# Get day of year, zero based |
| 2581 |
|
| 2582 |
set doy [expr { [dict get $date dayOfYear] - 1 }] |
| 2583 |
|
| 2584 |
# Determine whether the year is a leap year |
| 2585 |
|
| 2586 |
set lp [IsGregorianLeapYear $date] |
| 2587 |
|
| 2588 |
# Convert day of year to a fractional year |
| 2589 |
|
| 2590 |
if { $lp } { |
| 2591 |
set fractYear [expr { 1000 * $doy / 366 }] |
| 2592 |
} else { |
| 2593 |
set fractYear [expr { 1000 * $doy / 365 }] |
| 2594 |
} |
| 2595 |
|
| 2596 |
# Put together the StarDate |
| 2597 |
|
| 2598 |
return [::format "Stardate %02d%03d.%1d" \ |
| 2599 |
[expr { [dict get $date year] - $Roddenberry }] \ |
| 2600 |
$fractYear \ |
| 2601 |
[expr { [dict get $date localSeconds] % 86400 |
| 2602 |
/ ( 86400 / 10 ) }]] |
| 2603 |
} |
| 2604 |
|
| 2605 |
#---------------------------------------------------------------------- |
| 2606 |
# |
| 2607 |
# ParseStarDate -- |
| 2608 |
# |
| 2609 |
# Parses a StarDate |
| 2610 |
# |
| 2611 |
# Parameters: |
| 2612 |
# year - Year from the Roddenberry epoch |
| 2613 |
# fractYear - Fraction of a year specifying the day of year. |
| 2614 |
# fractDay - Fraction of a day |
| 2615 |
# |
| 2616 |
# Results: |
| 2617 |
# Returns a count of seconds from the Posix epoch. |
| 2618 |
# |
| 2619 |
# Side effects: |
| 2620 |
# None. |
| 2621 |
# |
| 2622 |
# Jeff Hobbs put this in to support an atrocious pun about Tcl being |
| 2623 |
# "Enterprise ready." Now we're stuck with it. |
| 2624 |
# |
| 2625 |
#---------------------------------------------------------------------- |
| 2626 |
|
| 2627 |
proc ::tcl::clock::ParseStarDate { year fractYear fractDay } { |
| 2628 |
variable Roddenberry |
| 2629 |
|
| 2630 |
# Build a tentative date from year and fraction. |
| 2631 |
|
| 2632 |
set date [dict create \ |
| 2633 |
gregorian 1 \ |
| 2634 |
era CE \ |
| 2635 |
year [expr { $year + $Roddenberry }] \ |
| 2636 |
dayOfYear [expr { $fractYear * 365 / 1000 + 1 }]] |
| 2637 |
set date [GetJulianDayFromGregorianEraYearDay $date[set date {}]] |
| 2638 |
|
| 2639 |
# Determine whether the given year is a leap year |
| 2640 |
|
| 2641 |
set lp [IsGregorianLeapYear $date] |
| 2642 |
|
| 2643 |
# Reconvert the fractional year according to whether the given year is a |
| 2644 |
# leap year |
| 2645 |
|
| 2646 |
if { $lp } { |
| 2647 |
dict set date dayOfYear \ |
| 2648 |
[expr { $fractYear * 366 / 1000 + 1 }] |
| 2649 |
} else { |
| 2650 |
dict set date dayOfYear \ |
| 2651 |
[expr { $fractYear * 365 / 1000 + 1 }] |
| 2652 |
} |
| 2653 |
dict unset date julianDay |
| 2654 |
dict unset date gregorian |
| 2655 |
set date [GetJulianDayFromGregorianEraYearDay $date[set date {}]] |
| 2656 |
|
| 2657 |
return [expr { |
| 2658 |
86400 * [dict get $date julianDay] |
| 2659 |
- 210866803200 |
| 2660 |
+ ( 86400 / 10 ) * $fractDay |
| 2661 |
}] |
| 2662 |
} |
| 2663 |
|
| 2664 |
#---------------------------------------------------------------------- |
| 2665 |
# |
| 2666 |
# ScanWide -- |
| 2667 |
# |
| 2668 |
# Scans a wide integer from an input |
| 2669 |
# |
| 2670 |
# Parameters: |
| 2671 |
# str - String containing a decimal wide integer |
| 2672 |
# |
| 2673 |
# Results: |
| 2674 |
# Returns the string as a pure wide integer. Throws an error if the |
| 2675 |
# string is misformatted or out of range. |
| 2676 |
# |
| 2677 |
#---------------------------------------------------------------------- |
| 2678 |
|
| 2679 |
proc ::tcl::clock::ScanWide { str } { |
| 2680 |
set count [::scan $str {%ld %c} result junk] |
| 2681 |
if { $count != 1 } { |
| 2682 |
return -code error -errorcode [list CLOCK notAnInteger $str] \ |
| 2683 |
"\"$str\" is not an integer" |
| 2684 |
} |
| 2685 |
if { [incr result 0] != $str } { |
| 2686 |
return -code error -errorcode [list CLOCK integervalueTooLarge] \ |
| 2687 |
"integer value too large to represent" |
| 2688 |
} |
| 2689 |
return $result |
| 2690 |
} |
| 2691 |
|
| 2692 |
#---------------------------------------------------------------------- |
| 2693 |
# |
| 2694 |
# InterpretTwoDigitYear -- |
| 2695 |
# |
| 2696 |
# Given a date that contains only the year of the century, determines |
| 2697 |
# the target value of a two-digit year. |
| 2698 |
# |
| 2699 |
# Parameters: |
| 2700 |
# date - Dictionary containing fields of the date. |
| 2701 |
# baseTime - Base time relative to which the date is expressed. |
| 2702 |
# twoDigitField - Name of the field that stores the two-digit year. |
| 2703 |
# Default is 'yearOfCentury' |
| 2704 |
# fourDigitField - Name of the field that will receive the four-digit |
| 2705 |
# year. Default is 'year' |
| 2706 |
# |
| 2707 |
# Results: |
| 2708 |
# Returns the dictionary augmented with the four-digit year, stored in |
| 2709 |
# the given key. |
| 2710 |
# |
| 2711 |
# Side effects: |
| 2712 |
# None. |
| 2713 |
# |
| 2714 |
# The current rule for interpreting a two-digit year is that the year shall be |
| 2715 |
# between 1937 and 2037, thus staying within the range of a 32-bit signed |
| 2716 |
# value for time. This rule may change to a sliding window in future |
| 2717 |
# versions, so the 'baseTime' parameter (which is currently ignored) is |
| 2718 |
# provided in the procedure signature. |
| 2719 |
# |
| 2720 |
#---------------------------------------------------------------------- |
| 2721 |
|
| 2722 |
proc ::tcl::clock::InterpretTwoDigitYear { date baseTime |
| 2723 |
{ twoDigitField yearOfCentury } |
| 2724 |
{ fourDigitField year } } { |
| 2725 |
set yr [dict get $date $twoDigitField] |
| 2726 |
if { $yr <= 37 } { |
| 2727 |
dict set date $fourDigitField [expr { $yr + 2000 }] |
| 2728 |
} else { |
| 2729 |
dict set date $fourDigitField [expr { $yr + 1900 }] |
| 2730 |
} |
| 2731 |
return $date |
| 2732 |
} |
| 2733 |
|
| 2734 |
#---------------------------------------------------------------------- |
| 2735 |
# |
| 2736 |
# AssignBaseYear -- |
| 2737 |
# |
| 2738 |
# Places the number of the current year into a dictionary. |
| 2739 |
# |
| 2740 |
# Parameters: |
| 2741 |
# date - Dictionary value to update |
| 2742 |
# baseTime - Base time from which to extract the year, expressed |
| 2743 |
# in seconds from the Posix epoch |
| 2744 |
# timezone - the time zone in which the date is being scanned |
| 2745 |
# changeover - the Julian Day on which the Gregorian calendar |
| 2746 |
# was adopted in the target locale. |
| 2747 |
# |
| 2748 |
# Results: |
| 2749 |
# Returns the dictionary with the current year assigned. |
| 2750 |
# |
| 2751 |
# Side effects: |
| 2752 |
# None. |
| 2753 |
# |
| 2754 |
#---------------------------------------------------------------------- |
| 2755 |
|
| 2756 |
proc ::tcl::clock::AssignBaseYear { date baseTime timezone changeover } { |
| 2757 |
variable TZData |
| 2758 |
|
| 2759 |
# Find the Julian Day Number corresponding to the base time, and |
| 2760 |
# find the Gregorian year corresponding to that Julian Day. |
| 2761 |
|
| 2762 |
set date2 [GetDateFields $baseTime $TZData($timezone) $changeover] |
| 2763 |
|
| 2764 |
# Store the converted year |
| 2765 |
|
| 2766 |
dict set date era [dict get $date2 era] |
| 2767 |
dict set date year [dict get $date2 year] |
| 2768 |
|
| 2769 |
return $date |
| 2770 |
} |
| 2771 |
|
| 2772 |
#---------------------------------------------------------------------- |
| 2773 |
# |
| 2774 |
# AssignBaseIso8601Year -- |
| 2775 |
# |
| 2776 |
# Determines the base year in the ISO8601 fiscal calendar. |
| 2777 |
# |
| 2778 |
# Parameters: |
| 2779 |
# date - Dictionary containing the fields of the date that |
| 2780 |
# is to be augmented with the base year. |
| 2781 |
# baseTime - Base time expressed in seconds from the Posix epoch. |
| 2782 |
# timeZone - Target time zone |
| 2783 |
# changeover - Julian Day of adoption of the Gregorian calendar in |
| 2784 |
# the target locale. |
| 2785 |
# |
| 2786 |
# Results: |
| 2787 |
# Returns the given date with "iso8601Year" set to the |
| 2788 |
# base year. |
| 2789 |
# |
| 2790 |
# Side effects: |
| 2791 |
# None. |
| 2792 |
# |
| 2793 |
#---------------------------------------------------------------------- |
| 2794 |
|
| 2795 |
proc ::tcl::clock::AssignBaseIso8601Year {date baseTime timeZone changeover} { |
| 2796 |
variable TZData |
| 2797 |
|
| 2798 |
# Find the Julian Day Number corresponding to the base time |
| 2799 |
|
| 2800 |
set date2 [GetDateFields $baseTime $TZData($timeZone) $changeover] |
| 2801 |
|
| 2802 |
# Calculate the ISO8601 date and transfer the year |
| 2803 |
|
| 2804 |
dict set date era CE |
| 2805 |
dict set date iso8601Year [dict get $date2 iso8601Year] |
| 2806 |
return $date |
| 2807 |
} |
| 2808 |
|
| 2809 |
#---------------------------------------------------------------------- |
| 2810 |
# |
| 2811 |
# AssignBaseMonth -- |
| 2812 |
# |
| 2813 |
# Places the number of the current year and month into a |
| 2814 |
# dictionary. |
| 2815 |
# |
| 2816 |
# Parameters: |
| 2817 |
# date - Dictionary value to update |
| 2818 |
# baseTime - Time from which the year and month are to be |
| 2819 |
# obtained, expressed in seconds from the Posix epoch. |
| 2820 |
# timezone - Name of the desired time zone |
| 2821 |
# changeover - Julian Day on which the Gregorian calendar was adopted. |
| 2822 |
# |
| 2823 |
# Results: |
| 2824 |
# Returns the dictionary with the base year and month assigned. |
| 2825 |
# |
| 2826 |
# Side effects: |
| 2827 |
# None. |
| 2828 |
# |
| 2829 |
#---------------------------------------------------------------------- |
| 2830 |
|
| 2831 |
proc ::tcl::clock::AssignBaseMonth {date baseTime timezone changeover} { |
| 2832 |
variable TZData |
| 2833 |
|
| 2834 |
# Find the year and month corresponding to the base time |
| 2835 |
|
| 2836 |
set date2 [GetDateFields $baseTime $TZData($timezone) $changeover] |
| 2837 |
dict set date era [dict get $date2 era] |
| 2838 |
dict set date year [dict get $date2 year] |
| 2839 |
dict set date month [dict get $date2 month] |
| 2840 |
return $date |
| 2841 |
} |
| 2842 |
|
| 2843 |
#---------------------------------------------------------------------- |
| 2844 |
# |
| 2845 |
# AssignBaseWeek -- |
| 2846 |
# |
| 2847 |
# Determines the base year and week in the ISO8601 fiscal calendar. |
| 2848 |
# |
| 2849 |
# Parameters: |
| 2850 |
# date - Dictionary containing the fields of the date that |
| 2851 |
# is to be augmented with the base year and week. |
| 2852 |
# baseTime - Base time expressed in seconds from the Posix epoch. |
| 2853 |
# changeover - Julian Day on which the Gregorian calendar was adopted |
| 2854 |
# in the target locale. |
| 2855 |
# |
| 2856 |
# Results: |
| 2857 |
# Returns the given date with "iso8601Year" set to the |
| 2858 |
# base year and "iso8601Week" to the week number. |
| 2859 |
# |
| 2860 |
# Side effects: |
| 2861 |
# None. |
| 2862 |
# |
| 2863 |
#---------------------------------------------------------------------- |
| 2864 |
|
| 2865 |
proc ::tcl::clock::AssignBaseWeek {date baseTime timeZone changeover} { |
| 2866 |
variable TZData |
| 2867 |
|
| 2868 |
# Find the Julian Day Number corresponding to the base time |
| 2869 |
|
| 2870 |
set date2 [GetDateFields $baseTime $TZData($timeZone) $changeover] |
| 2871 |
|
| 2872 |
# Calculate the ISO8601 date and transfer the year |
| 2873 |
|
| 2874 |
dict set date era CE |
| 2875 |
dict set date iso8601Year [dict get $date2 iso8601Year] |
| 2876 |
dict set date iso8601Week [dict get $date2 iso8601Week] |
| 2877 |
return $date |
| 2878 |
} |
| 2879 |
|
| 2880 |
#---------------------------------------------------------------------- |
| 2881 |
# |
| 2882 |
# AssignBaseJulianDay -- |
| 2883 |
# |
| 2884 |
# Determines the base day for a time-of-day conversion. |
| 2885 |
# |
| 2886 |
# Parameters: |
| 2887 |
# date - Dictionary that is to get the base day |
| 2888 |
# baseTime - Base time expressed in seconds from the Posix epoch |
| 2889 |
# changeover - Julian day on which the Gregorian calendar was |
| 2890 |
# adpoted in the target locale. |
| 2891 |
# |
| 2892 |
# Results: |
| 2893 |
# Returns the given dictionary augmented with a 'julianDay' field |
| 2894 |
# that contains the base day. |
| 2895 |
# |
| 2896 |
# Side effects: |
| 2897 |
# None. |
| 2898 |
# |
| 2899 |
#---------------------------------------------------------------------- |
| 2900 |
|
| 2901 |
proc ::tcl::clock::AssignBaseJulianDay { date baseTime timeZone changeover } { |
| 2902 |
variable TZData |
| 2903 |
|
| 2904 |
# Find the Julian Day Number corresponding to the base time |
| 2905 |
|
| 2906 |
set date2 [GetDateFields $baseTime $TZData($timeZone) $changeover] |
| 2907 |
dict set date julianDay [dict get $date2 julianDay] |
| 2908 |
|
| 2909 |
return $date |
| 2910 |
} |
| 2911 |
|
| 2912 |
#---------------------------------------------------------------------- |
| 2913 |
# |
| 2914 |
# InterpretHMSP -- |
| 2915 |
# |
| 2916 |
# Interprets a time in the form "hh:mm:ss am". |
| 2917 |
# |
| 2918 |
# Parameters: |
| 2919 |
# date -- Dictionary containing "hourAMPM", "minute", "second" |
| 2920 |
# and "amPmIndicator" fields. |
| 2921 |
# |
| 2922 |
# Results: |
| 2923 |
# Returns the number of seconds from local midnight. |
| 2924 |
# |
| 2925 |
# Side effects: |
| 2926 |
# None. |
| 2927 |
# |
| 2928 |
#---------------------------------------------------------------------- |
| 2929 |
|
| 2930 |
proc ::tcl::clock::InterpretHMSP { date } { |
| 2931 |
set hr [dict get $date hourAMPM] |
| 2932 |
if { $hr == 12 } { |
| 2933 |
set hr 0 |
| 2934 |
} |
| 2935 |
if { [dict get $date amPmIndicator] } { |
| 2936 |
incr hr 12 |
| 2937 |
} |
| 2938 |
dict set date hour $hr |
| 2939 |
return [InterpretHMS $date[set date {}]] |
| 2940 |
} |
| 2941 |
|
| 2942 |
#---------------------------------------------------------------------- |
| 2943 |
# |
| 2944 |
# InterpretHMS -- |
| 2945 |
# |
| 2946 |
# Interprets a 24-hour time "hh:mm:ss" |
| 2947 |
# |
| 2948 |
# Parameters: |
| 2949 |
# date -- Dictionary containing the "hour", "minute" and "second" |
| 2950 |
# fields. |
| 2951 |
# |
| 2952 |
# Results: |
| 2953 |
# Returns the given dictionary augmented with a "secondOfDay" |
| 2954 |
# field containing the number of seconds from local midnight. |
| 2955 |
# |
| 2956 |
# Side effects: |
| 2957 |
# None. |
| 2958 |
# |
| 2959 |
#---------------------------------------------------------------------- |
| 2960 |
|
| 2961 |
proc ::tcl::clock::InterpretHMS { date } { |
| 2962 |
return [expr { |
| 2963 |
( [dict get $date hour] * 60 |
| 2964 |
+ [dict get $date minute] ) * 60 |
| 2965 |
+ [dict get $date second] |
| 2966 |
}] |
| 2967 |
} |
| 2968 |
|
| 2969 |
#---------------------------------------------------------------------- |
| 2970 |
# |
| 2971 |
# GetSystemTimeZone -- |
| 2972 |
# |
| 2973 |
# Determines the system time zone, which is the default for the |
| 2974 |
# 'clock' command if no other zone is supplied. |
| 2975 |
# |
| 2976 |
# Parameters: |
| 2977 |
# None. |
| 2978 |
# |
| 2979 |
# Results: |
| 2980 |
# Returns the system time zone. |
| 2981 |
# |
| 2982 |
# Side effects: |
| 2983 |
# Stores the system time zone in the 'CachedSystemTimeZone' |
| 2984 |
# variable, since determining it may be an expensive process. |
| 2985 |
# |
| 2986 |
#---------------------------------------------------------------------- |
| 2987 |
|
| 2988 |
proc ::tcl::clock::GetSystemTimeZone {} { |
| 2989 |
variable CachedSystemTimeZone |
| 2990 |
variable TimeZoneBad |
| 2991 |
|
| 2992 |
if {[set result [getenv TCL_TZ]] ne {}} { |
| 2993 |
set timezone $result |
| 2994 |
} elseif {[set result [getenv TZ]] ne {}} { |
| 2995 |
set timezone $result |
| 2996 |
} else { |
| 2997 |
# Cache the time zone only if it was detected by one of the |
| 2998 |
# expensive methods. |
| 2999 |
if { [info exists CachedSystemTimeZone] } { |
| 3000 |
set timezone $CachedSystemTimeZone |
| 3001 |
} elseif { $::tcl_platform(platform) eq {windows} } { |
| 3002 |
set timezone [GuessWindowsTimeZone] |
| 3003 |
} elseif { [file exists /etc/localtime] |
| 3004 |
&& ![catch {ReadZoneinfoFile \ |
| 3005 |
Tcl/Localtime /etc/localtime}] } { |
| 3006 |
set timezone :Tcl/Localtime |
| 3007 |
} else { |
| 3008 |
set timezone :localtime |
| 3009 |
} |
| 3010 |
set CachedSystemTimeZone $timezone |
| 3011 |
} |
| 3012 |
if { ![dict exists $TimeZoneBad $timezone] } { |
| 3013 |
dict set TimeZoneBad $timezone [catch {SetupTimeZone $timezone}] |
| 3014 |
} |
| 3015 |
if { [dict get $TimeZoneBad $timezone] } { |
| 3016 |
return :localtime |
| 3017 |
} else { |
| 3018 |
return $timezone |
| 3019 |
} |
| 3020 |
} |
| 3021 |
|
| 3022 |
#---------------------------------------------------------------------- |
| 3023 |
# |
| 3024 |
# ConvertLegacyTimeZone -- |
| 3025 |
# |
| 3026 |
# Given an alphanumeric time zone identifier and the system time zone, |
| 3027 |
# convert the alphanumeric identifier to an unambiguous time zone. |
| 3028 |
# |
| 3029 |
# Parameters: |
| 3030 |
# tzname - Name of the time zone to convert |
| 3031 |
# |
| 3032 |
# Results: |
| 3033 |
# Returns a time zone name corresponding to tzname, but in an |
| 3034 |
# unambiguous form, generally +hhmm. |
| 3035 |
# |
| 3036 |
# This procedure is implemented primarily to allow the parsing of RFC822 |
| 3037 |
# date/time strings. Processing a time zone name on input is not recommended |
| 3038 |
# practice, because there is considerable room for ambiguity; for instance, is |
| 3039 |
# BST Brazilian Standard Time, or British Summer Time? |
| 3040 |
# |
| 3041 |
#---------------------------------------------------------------------- |
| 3042 |
|
| 3043 |
proc ::tcl::clock::ConvertLegacyTimeZone { tzname } { |
| 3044 |
variable LegacyTimeZone |
| 3045 |
|
| 3046 |
set tzname [string tolower $tzname] |
| 3047 |
if { ![dict exists $LegacyTimeZone $tzname] } { |
| 3048 |
return -code error -errorcode [list CLOCK badTZName $tzname] \ |
| 3049 |
"time zone \"$tzname\" not found" |
| 3050 |
} |
| 3051 |
return [dict get $LegacyTimeZone $tzname] |
| 3052 |
} |
| 3053 |
|
| 3054 |
#---------------------------------------------------------------------- |
| 3055 |
# |
| 3056 |
# SetupTimeZone -- |
| 3057 |
# |
| 3058 |
# Given the name or specification of a time zone, sets up its in-memory |
| 3059 |
# data. |
| 3060 |
# |
| 3061 |
# Parameters: |
| 3062 |
# tzname - Name of a time zone |
| 3063 |
# |
| 3064 |
# Results: |
| 3065 |
# Unless the time zone is ':localtime', sets the TZData array to contain |
| 3066 |
# the lookup table for local<->UTC conversion. Returns an error if the |
| 3067 |
# time zone cannot be parsed. |
| 3068 |
# |
| 3069 |
#---------------------------------------------------------------------- |
| 3070 |
|
| 3071 |
proc ::tcl::clock::SetupTimeZone { timezone } { |
| 3072 |
variable TZData |
| 3073 |
|
| 3074 |
if {! [info exists TZData($timezone)] } { |
| 3075 |
variable MINWIDE |
| 3076 |
if { $timezone eq {:localtime} } { |
| 3077 |
# Nothing to do, we'll convert using the localtime function |
| 3078 |
|
| 3079 |
} elseif { |
| 3080 |
[regexp {^([-+])(\d\d)(?::?(\d\d)(?::?(\d\d))?)?} $timezone \ |
| 3081 |
-> s hh mm ss] |
| 3082 |
} then { |
| 3083 |
# Make a fixed offset |
| 3084 |
|
| 3085 |
::scan $hh %d hh |
| 3086 |
if { $mm eq {} } { |
| 3087 |
set mm 0 |
| 3088 |
} else { |
| 3089 |
::scan $mm %d mm |
| 3090 |
} |
| 3091 |
if { $ss eq {} } { |
| 3092 |
set ss 0 |
| 3093 |
} else { |
| 3094 |
::scan $ss %d ss |
| 3095 |
} |
| 3096 |
set offset [expr { ( $hh * 60 + $mm ) * 60 + $ss }] |
| 3097 |
if { $s eq {-} } { |
| 3098 |
set offset [expr { - $offset }] |
| 3099 |
} |
| 3100 |
set TZData($timezone) [list [list $MINWIDE $offset -1 $timezone]] |
| 3101 |
|
| 3102 |
} elseif { [string index $timezone 0] eq {:} } { |
| 3103 |
# Convert using a time zone file |
| 3104 |
|
| 3105 |
if { |
| 3106 |
[catch { |
| 3107 |
LoadTimeZoneFile [string range $timezone 1 end] |
| 3108 |
}] && [catch { |
| 3109 |
LoadZoneinfoFile [string range $timezone 1 end] |
| 3110 |
} ret opts] |
| 3111 |
} then { |
| 3112 |
dict unset opts -errorinfo |
| 3113 |
if {[lindex [dict get $opts -errorcode] 0] ne "CLOCK"} { |
| 3114 |
dict set opts -errorcode [list CLOCK badTimeZone $timezone] |
| 3115 |
set ret "time zone \"$timezone\" not found: $ret" |
| 3116 |
} |
| 3117 |
return -options $opts $ret |
| 3118 |
} |
| 3119 |
} elseif { ![catch {ParsePosixTimeZone $timezone} tzfields] } { |
| 3120 |
# This looks like a POSIX time zone - try to process it |
| 3121 |
|
| 3122 |
if { [catch {ProcessPosixTimeZone $tzfields} ret opts] } { |
| 3123 |
dict unset opts -errorinfo |
| 3124 |
if {[lindex [dict get $opts -errorcode] 0] ne "CLOCK"} { |
| 3125 |
dict set opts -errorcode [list CLOCK badTimeZone $timezone] |
| 3126 |
set ret "time zone \"$timezone\" not found: $ret" |
| 3127 |
} |
| 3128 |
return -options $opts $ret |
| 3129 |
} else { |
| 3130 |
set TZData($timezone) $ret |
| 3131 |
} |
| 3132 |
|
| 3133 |
} else { |
| 3134 |
# We couldn't parse this as a POSIX time zone. Try again with a |
| 3135 |
# time zone file - this time without a colon |
| 3136 |
|
| 3137 |
if { [catch { LoadTimeZoneFile $timezone }] |
| 3138 |
&& [catch { LoadZoneinfoFile $timezone } ret opts] } { |
| 3139 |
dict unset opts -errorinfo |
| 3140 |
if {[lindex [dict get $opts -errorcode] 0] ne "CLOCK"} { |
| 3141 |
dict set opts -errorcode [list CLOCK badTimeZone $timezone] |
| 3142 |
set ret "time zone \"$timezone\" not found: $ret" |
| 3143 |
} |
| 3144 |
return -options $opts $ret |
| 3145 |
} |
| 3146 |
set TZData($timezone) $TZData(:$timezone) |
| 3147 |
} |
| 3148 |
} |
| 3149 |
|
| 3150 |
return |
| 3151 |
} |
| 3152 |
|
| 3153 |
#---------------------------------------------------------------------- |
| 3154 |
# |
| 3155 |
# GuessWindowsTimeZone -- |
| 3156 |
# |
| 3157 |
# Determines the system time zone on windows. |
| 3158 |
# |
| 3159 |
# Parameters: |
| 3160 |
# None. |
| 3161 |
# |
| 3162 |
# Results: |
| 3163 |
# Returns a time zone specifier that corresponds to the system time zone |
| 3164 |
# information found in the Registry. |
| 3165 |
# |
| 3166 |
# Bugs: |
| 3167 |
# Fixed dates for DST change are unimplemented at present, because no |
| 3168 |
# time zone information supplied with Windows actually uses them! |
| 3169 |
# |
| 3170 |
# On a Windows system where neither $env(TCL_TZ) nor $env(TZ) is specified, |
| 3171 |
# GuessWindowsTimeZone looks in the Registry for the system time zone |
| 3172 |
# information. It then attempts to find an entry in WinZoneInfo for a time |
| 3173 |
# zone that uses the same rules. If it finds one, it returns it; otherwise, |
| 3174 |
# it constructs a Posix-style time zone string and returns that. |
| 3175 |
# |
| 3176 |
#---------------------------------------------------------------------- |
| 3177 |
|
| 3178 |
proc ::tcl::clock::GuessWindowsTimeZone {} { |
| 3179 |
variable WinZoneInfo |
| 3180 |
variable NoRegistry |
| 3181 |
variable TimeZoneBad |
| 3182 |
|
| 3183 |
if { [info exists NoRegistry] } { |
| 3184 |
return :localtime |
| 3185 |
} |
| 3186 |
|
| 3187 |
# Dredge time zone information out of the registry |
| 3188 |
|
| 3189 |
if { [catch { |
| 3190 |
set rpath HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\TimeZoneInformation |
| 3191 |
set data [list \ |
| 3192 |
[expr { -60 |
| 3193 |
* [registry get $rpath Bias] }] \ |
| 3194 |
[expr { -60 |
| 3195 |
* [registry get $rpath StandardBias] }] \ |
| 3196 |
[expr { -60 \ |
| 3197 |
* [registry get $rpath DaylightBias] }]] |
| 3198 |
set stdtzi [registry get $rpath StandardStart] |
| 3199 |
foreach ind {0 2 14 4 6 8 10 12} { |
| 3200 |
binary scan $stdtzi @${ind}s val |
| 3201 |
lappend data $val |
| 3202 |
} |
| 3203 |
set daytzi [registry get $rpath DaylightStart] |
| 3204 |
foreach ind {0 2 14 4 6 8 10 12} { |
| 3205 |
binary scan $daytzi @${ind}s val |
| 3206 |
lappend data $val |
| 3207 |
} |
| 3208 |
}] } { |
| 3209 |
# Missing values in the Registry - bail out |
| 3210 |
|
| 3211 |
return :localtime |
| 3212 |
} |
| 3213 |
|
| 3214 |
# Make up a Posix time zone specifier if we can't find one. Check here |
| 3215 |
# that the tzdata file exists, in case we're running in an environment |
| 3216 |
# (e.g. starpack) where tzdata is incomplete. (Bug 1237907) |
| 3217 |
|
| 3218 |
if { [dict exists $WinZoneInfo $data] } { |
| 3219 |
set tzname [dict get $WinZoneInfo $data] |
| 3220 |
if { ! [dict exists $TimeZoneBad $tzname] } { |
| 3221 |
dict set TimeZoneBad $tzname [catch {SetupTimeZone $tzname}] |
| 3222 |
} |
| 3223 |
} else { |
| 3224 |
set tzname {} |
| 3225 |
} |
| 3226 |
if { $tzname eq {} || [dict get $TimeZoneBad $tzname] } { |
| 3227 |
lassign $data \ |
| 3228 |
bias stdBias dstBias \ |
| 3229 |
stdYear stdMonth stdDayOfWeek stdDayOfMonth \ |
| 3230 |
stdHour stdMinute stdSecond stdMillisec \ |
| 3231 |
dstYear dstMonth dstDayOfWeek dstDayOfMonth \ |
| 3232 |
dstHour dstMinute dstSecond dstMillisec |
| 3233 |
set stdDelta [expr { $bias + $stdBias }] |
| 3234 |
set dstDelta [expr { $bias + $dstBias }] |
| 3235 |
if { $stdDelta <= 0 } { |
| 3236 |
set stdSignum + |
| 3237 |
set stdDelta [expr { - $stdDelta }] |
| 3238 |
set dispStdSignum - |
| 3239 |
} else { |
| 3240 |
set stdSignum - |
| 3241 |
set dispStdSignum + |
| 3242 |
} |
| 3243 |
set hh [::format %02d [expr { $stdDelta / 3600 }]] |
| 3244 |
set mm [::format %02d [expr { ($stdDelta / 60 ) % 60 }]] |
| 3245 |
set ss [::format %02d [expr { $stdDelta % 60 }]] |
| 3246 |
set tzname {} |
| 3247 |
append tzname < $dispStdSignum $hh $mm > $stdSignum $hh : $mm : $ss |
| 3248 |
if { $stdMonth >= 0 } { |
| 3249 |
if { $dstDelta <= 0 } { |
| 3250 |
set dstSignum + |
| 3251 |
set dstDelta [expr { - $dstDelta }] |
| 3252 |
set dispDstSignum - |
| 3253 |
} else { |
| 3254 |
set dstSignum - |
| 3255 |
set dispDstSignum + |
| 3256 |
} |
| 3257 |
set hh [::format %02d [expr { $dstDelta / 3600 }]] |
| 3258 |
set mm [::format %02d [expr { ($dstDelta / 60 ) % 60 }]] |
| 3259 |
set ss [::format %02d [expr { $dstDelta % 60 }]] |
| 3260 |
append tzname < $dispDstSignum $hh $mm > $dstSignum $hh : $mm : $ss |
| 3261 |
if { $dstYear == 0 } { |
| 3262 |
append tzname ,M $dstMonth . $dstDayOfMonth . $dstDayOfWeek |
| 3263 |
} else { |
| 3264 |
# I have not been able to find any locale on which Windows |
| 3265 |
# converts time zone on a fixed day of the year, hence don't |
| 3266 |
# know how to interpret the fields. If someone can inform me, |
| 3267 |
# I'd be glad to code it up. For right now, we bail out in |
| 3268 |
# such a case. |
| 3269 |
return :localtime |
| 3270 |
} |
| 3271 |
append tzname / [::format %02d $dstHour] \ |
| 3272 |
: [::format %02d $dstMinute] \ |
| 3273 |
: [::format %02d $dstSecond] |
| 3274 |
if { $stdYear == 0 } { |
| 3275 |
append tzname ,M $stdMonth . $stdDayOfMonth . $stdDayOfWeek |
| 3276 |
} else { |
| 3277 |
# I have not been able to find any locale on which Windows |
| 3278 |
# converts time zone on a fixed day of the year, hence don't |
| 3279 |
# know how to interpret the fields. If someone can inform me, |
| 3280 |
# I'd be glad to code it up. For right now, we bail out in |
| 3281 |
# such a case. |
| 3282 |
return :localtime |
| 3283 |
} |
| 3284 |
append tzname / [::format %02d $stdHour] \ |
| 3285 |
: [::format %02d $stdMinute] \ |
| 3286 |
: [::format %02d $stdSecond] |
| 3287 |
} |
| 3288 |
dict set WinZoneInfo $data $tzname |
| 3289 |
} |
| 3290 |
|
| 3291 |
return [dict get $WinZoneInfo $data] |
| 3292 |
} |
| 3293 |
|
| 3294 |
#---------------------------------------------------------------------- |
| 3295 |
# |
| 3296 |
# LoadTimeZoneFile -- |
| 3297 |
# |
| 3298 |
# Load the data file that specifies the conversion between a |
| 3299 |
# given time zone and Greenwich. |
| 3300 |
# |
| 3301 |
# Parameters: |
| 3302 |
# fileName -- Name of the file to load |
| 3303 |
# |
| 3304 |
# Results: |
| 3305 |
# None. |
| 3306 |
# |
| 3307 |
# Side effects: |
| 3308 |
# TZData(:fileName) contains the time zone data |
| 3309 |
# |
| 3310 |
#---------------------------------------------------------------------- |
| 3311 |
|
| 3312 |
proc ::tcl::clock::LoadTimeZoneFile { fileName } { |
| 3313 |
variable DataDir |
| 3314 |
variable TZData |
| 3315 |
|
| 3316 |
if { [info exists TZData($fileName)] } { |
| 3317 |
return |
| 3318 |
} |
| 3319 |
|
| 3320 |
# Since an unsafe interp uses the [clock] command in the parent, this code |
| 3321 |
# is security sensitive. Make sure that the path name cannot escape the |
| 3322 |
# given directory. |
| 3323 |
|
| 3324 |
if { [regexp {^[/\\]|^[a-zA-Z]+:|(?:^|[/\\])\.\.} $fileName] } { |
| 3325 |
return -code error \ |
| 3326 |
-errorcode [list CLOCK badTimeZone :$fileName] \ |
| 3327 |
"time zone \":$fileName\" not valid" |
| 3328 |
} |
| 3329 |
try { |
| 3330 |
source -encoding utf-8 [file join $DataDir $fileName] |
| 3331 |
} on error {} { |
| 3332 |
return -code error \ |
| 3333 |
-errorcode [list CLOCK badTimeZone :$fileName] \ |
| 3334 |
"time zone \":$fileName\" not found" |
| 3335 |
} |
| 3336 |
return |
| 3337 |
} |
| 3338 |
|
| 3339 |
#---------------------------------------------------------------------- |
| 3340 |
# |
| 3341 |
# LoadZoneinfoFile -- |
| 3342 |
# |
| 3343 |
# Loads a binary time zone information file in Olson format. |
| 3344 |
# |
| 3345 |
# Parameters: |
| 3346 |
# fileName - Relative path name of the file to load. |
| 3347 |
# |
| 3348 |
# Results: |
| 3349 |
# Returns an empty result normally; returns an error if no Olson file |
| 3350 |
# was found or the file was malformed in some way. |
| 3351 |
# |
| 3352 |
# Side effects: |
| 3353 |
# TZData(:fileName) contains the time zone data |
| 3354 |
# |
| 3355 |
#---------------------------------------------------------------------- |
| 3356 |
|
| 3357 |
proc ::tcl::clock::LoadZoneinfoFile { fileName } { |
| 3358 |
variable ZoneinfoPaths |
| 3359 |
|
| 3360 |
# Since an unsafe interp uses the [clock] command in the parent, this code |
| 3361 |
# is security sensitive. Make sure that the path name cannot escape the |
| 3362 |
# given directory. |
| 3363 |
|
| 3364 |
if { [regexp {^[/\\]|^[a-zA-Z]+:|(?:^|[/\\])\.\.} $fileName] } { |
| 3365 |
return -code error \ |
| 3366 |
-errorcode [list CLOCK badTimeZone :$fileName] \ |
| 3367 |
"time zone \":$fileName\" not valid" |
| 3368 |
} |
| 3369 |
set fname "" |
| 3370 |
foreach d $ZoneinfoPaths { |
| 3371 |
set fname [file join $d $fileName] |
| 3372 |
if { [file readable $fname] && [file isfile $fname] } { |
| 3373 |
break |
| 3374 |
} |
| 3375 |
set fname "" |
| 3376 |
} |
| 3377 |
if {$fname eq ""} { |
| 3378 |
return -code error \ |
| 3379 |
-errorcode [list CLOCK badTimeZone :$fileName] \ |
| 3380 |
"time zone \":$fileName\" not found" |
| 3381 |
} |
| 3382 |
ReadZoneinfoFile $fileName $fname |
| 3383 |
} |
| 3384 |
|
| 3385 |
#---------------------------------------------------------------------- |
| 3386 |
# |
| 3387 |
# ReadZoneinfoFile -- |
| 3388 |
# |
| 3389 |
# Loads a binary time zone information file in Olson format. |
| 3390 |
# |
| 3391 |
# Parameters: |
| 3392 |
# fileName - Name of the time zone (relative path name of the |
| 3393 |
# file). |
| 3394 |
# fname - Absolute path name of the file. |
| 3395 |
# |
| 3396 |
# Results: |
| 3397 |
# Returns an empty result normally; returns an error if no Olson file |
| 3398 |
# was found or the file was malformed in some way. |
| 3399 |
# |
| 3400 |
# Side effects: |
| 3401 |
# TZData(:fileName) contains the time zone data |
| 3402 |
# |
| 3403 |
#---------------------------------------------------------------------- |
| 3404 |
|
| 3405 |
proc ::tcl::clock::ReadZoneinfoFile {fileName fname} { |
| 3406 |
variable MINWIDE |
| 3407 |
variable TZData |
| 3408 |
if { ![file exists $fname] } { |
| 3409 |
return -code error "$fileName not found" |
| 3410 |
} |
| 3411 |
|
| 3412 |
if { [file size $fname] > 262144 } { |
| 3413 |
return -code error "$fileName too big" |
| 3414 |
} |
| 3415 |
|
| 3416 |
# Suck in all the data from the file |
| 3417 |
|
| 3418 |
set f [open $fname r] |
| 3419 |
fconfigure $f -translation binary |
| 3420 |
set d [read $f] |
| 3421 |
close $f |
| 3422 |
|
| 3423 |
# The file begins with a magic number, sixteen reserved bytes, and then |
| 3424 |
# six 4-byte integers giving counts of fields in the file. |
| 3425 |
|
| 3426 |
binary scan $d a4a1x15IIIIII \ |
| 3427 |
magic version nIsGMT nIsStd nLeap nTime nType nChar |
| 3428 |
set seek 44 |
| 3429 |
set ilen 4 |
| 3430 |
set iformat I |
| 3431 |
if { $magic != {TZif} } { |
| 3432 |
return -code error "$fileName not a time zone information file" |
| 3433 |
} |
| 3434 |
if { $nType > 255 } { |
| 3435 |
return -code error "$fileName contains too many time types" |
| 3436 |
} |
| 3437 |
# Accept only Posix-style zoneinfo. Sorry, 'leaps' bigots. |
| 3438 |
if { $nLeap != 0 } { |
| 3439 |
return -code error "$fileName contains leap seconds" |
| 3440 |
} |
| 3441 |
|
| 3442 |
# In a version 2 file, we use the second part of the file, which contains |
| 3443 |
# 64-bit transition times. |
| 3444 |
|
| 3445 |
if {$version eq "2"} { |
| 3446 |
set seek [expr { |
| 3447 |
44 |
| 3448 |
+ 5 * $nTime |
| 3449 |
+ 6 * $nType |
| 3450 |
+ 4 * $nLeap |
| 3451 |
+ $nIsStd |
| 3452 |
+ $nIsGMT |
| 3453 |
+ $nChar |
| 3454 |
}] |
| 3455 |
binary scan $d @${seek}a4a1x15IIIIII \ |
| 3456 |
magic version nIsGMT nIsStd nLeap nTime nType nChar |
| 3457 |
if {$magic ne {TZif}} { |
| 3458 |
return -code error "seek address $seek miscomputed, magic = $magic" |
| 3459 |
} |
| 3460 |
set iformat W |
| 3461 |
set ilen 8 |
| 3462 |
incr seek 44 |
| 3463 |
} |
| 3464 |
|
| 3465 |
# Next come ${nTime} transition times, followed by ${nTime} time type |
| 3466 |
# codes. The type codes are unsigned 1-byte quantities. We insert an |
| 3467 |
# arbitrary start time in front of the transitions. |
| 3468 |
|
| 3469 |
binary scan $d @${seek}${iformat}${nTime}c${nTime} times tempCodes |
| 3470 |
incr seek [expr { ($ilen + 1) * $nTime }] |
| 3471 |
set times [linsert $times 0 $MINWIDE] |
| 3472 |
set codes {} |
| 3473 |
foreach c $tempCodes { |
| 3474 |
lappend codes [expr { $c & 0xFF }] |
| 3475 |
} |
| 3476 |
set codes [linsert $codes 0 0] |
| 3477 |
|
| 3478 |
# Next come ${nType} time type descriptions, each of which has an offset |
| 3479 |
# (seconds east of GMT), a DST indicator, and an index into the |
| 3480 |
# abbreviation text. |
| 3481 |
|
| 3482 |
for { set i 0 } { $i < $nType } { incr i } { |
| 3483 |
binary scan $d @${seek}Icc gmtOff isDst abbrInd |
| 3484 |
lappend types [list $gmtOff $isDst $abbrInd] |
| 3485 |
incr seek 6 |
| 3486 |
} |
| 3487 |
|
| 3488 |
# Next come $nChar characters of time zone name abbreviations, which are |
| 3489 |
# null-terminated. |
| 3490 |
# We build them up into a dictionary indexed by character index, because |
| 3491 |
# that's what's in the indices above. |
| 3492 |
|
| 3493 |
binary scan $d @${seek}a${nChar} abbrs |
| 3494 |
incr seek ${nChar} |
| 3495 |
set abbrList [split $abbrs \0] |
| 3496 |
set i 0 |
| 3497 |
set abbrevs {} |
| 3498 |
foreach a $abbrList { |
| 3499 |
for {set j 0} {$j <= [string length $a]} {incr j} { |
| 3500 |
dict set abbrevs $i [string range $a $j end] |
| 3501 |
incr i |
| 3502 |
} |
| 3503 |
} |
| 3504 |
|
| 3505 |
# Package up a list of tuples, each of which contains transition time, |
| 3506 |
# seconds east of Greenwich, DST flag and time zone abbreviation. |
| 3507 |
|
| 3508 |
set r {} |
| 3509 |
set lastTime $MINWIDE |
| 3510 |
foreach t $times c $codes { |
| 3511 |
if { $t < $lastTime } { |
| 3512 |
return -code error "$fileName has times out of order" |
| 3513 |
} |
| 3514 |
set lastTime $t |
| 3515 |
lassign [lindex $types $c] gmtoff isDst abbrInd |
| 3516 |
set abbrev [dict get $abbrevs $abbrInd] |
| 3517 |
lappend r [list $t $gmtoff $isDst $abbrev] |
| 3518 |
} |
| 3519 |
|
| 3520 |
# In a version 2 file, there is also a POSIX-style time zone description |
| 3521 |
# at the very end of the file. To get to it, skip over nLeap leap second |
| 3522 |
# values (8 bytes each), |
| 3523 |
# nIsStd standard/DST indicators and nIsGMT UTC/local indicators. |
| 3524 |
|
| 3525 |
if {$version eq {2}} { |
| 3526 |
set seek [expr {$seek + 8 * $nLeap + $nIsStd + $nIsGMT + 1}] |
| 3527 |
set last [string first \n $d $seek] |
| 3528 |
set posix [string range $d $seek [expr {$last-1}]] |
| 3529 |
if {[llength $posix] > 0} { |
| 3530 |
set posixFields [ParsePosixTimeZone $posix] |
| 3531 |
foreach tuple [ProcessPosixTimeZone $posixFields] { |
| 3532 |
lassign $tuple t gmtoff isDst abbrev |
| 3533 |
if {$t > $lastTime} { |
| 3534 |
lappend r $tuple |
| 3535 |
} |
| 3536 |
} |
| 3537 |
} |
| 3538 |
} |
| 3539 |
|
| 3540 |
set TZData(:$fileName) $r |
| 3541 |
|
| 3542 |
return |
| 3543 |
} |
| 3544 |
|
| 3545 |
#---------------------------------------------------------------------- |
| 3546 |
# |
| 3547 |
# ParsePosixTimeZone -- |
| 3548 |
# |
| 3549 |
# Parses the TZ environment variable in Posix form |
| 3550 |
# |
| 3551 |
# Parameters: |
| 3552 |
# tz Time zone specifier to be interpreted |
| 3553 |
# |
| 3554 |
# Results: |
| 3555 |
# Returns a dictionary whose values contain the various pieces of the |
| 3556 |
# time zone specification. |
| 3557 |
# |
| 3558 |
# Side effects: |
| 3559 |
# None. |
| 3560 |
# |
| 3561 |
# Errors: |
| 3562 |
# Throws an error if the syntax of the time zone is incorrect. |
| 3563 |
# |
| 3564 |
# The following keys are present in the dictionary: |
| 3565 |
# stdName - Name of the time zone when Daylight Saving Time |
| 3566 |
# is not in effect. |
| 3567 |
# stdSignum - Sign (+, -, or empty) of the offset from Greenwich |
| 3568 |
# to the given (non-DST) time zone. + and the empty |
| 3569 |
# string denote zones west of Greenwich, - denotes east |
| 3570 |
# of Greenwich; this is contrary to the ISO convention |
| 3571 |
# but follows Posix. |
| 3572 |
# stdHours - Hours part of the offset from Greenwich to the given |
| 3573 |
# (non-DST) time zone. |
| 3574 |
# stdMinutes - Minutes part of the offset from Greenwich to the |
| 3575 |
# given (non-DST) time zone. Empty denotes zero. |
| 3576 |
# stdSeconds - Seconds part of the offset from Greenwich to the |
| 3577 |
# given (non-DST) time zone. Empty denotes zero. |
| 3578 |
# dstName - Name of the time zone when DST is in effect, or the |
| 3579 |
# empty string if the time zone does not observe Daylight |
| 3580 |
# Saving Time. |
| 3581 |
# dstSignum, dstHours, dstMinutes, dstSeconds - |
| 3582 |
# Fields corresponding to stdSignum, stdHours, stdMinutes, |
| 3583 |
# stdSeconds for the Daylight Saving Time version of the |
| 3584 |
# time zone. If dstHours is empty, it is presumed to be 1. |
| 3585 |
# startDayOfYear - The ordinal number of the day of the year on which |
| 3586 |
# Daylight Saving Time begins. If this field is |
| 3587 |
# empty, then DST begins on a given month-week-day, |
| 3588 |
# as below. |
| 3589 |
# startJ - The letter J, or an empty string. If a J is present in |
| 3590 |
# this field, then startDayOfYear does not count February 29 |
| 3591 |
# even in leap years. |
| 3592 |
# startMonth - The number of the month in which Daylight Saving Time |
| 3593 |
# begins, supplied if startDayOfYear is empty. If both |
| 3594 |
# startDayOfYear and startMonth are empty, then US rules |
| 3595 |
# are presumed. |
| 3596 |
# startWeekOfMonth - The number of the week in the month in which |
| 3597 |
# Daylight Saving Time begins, in the range 1-5. |
| 3598 |
# 5 denotes the last week of the month even in a |
| 3599 |
# 4-week month. |
| 3600 |
# startDayOfWeek - The number of the day of the week (Sunday=0, |
| 3601 |
# Saturday=6) on which Daylight Saving Time begins. |
| 3602 |
# startHours - The hours part of the time of day at which Daylight |
| 3603 |
# Saving Time begins. An empty string is presumed to be 2. |
| 3604 |
# startMinutes - The minutes part of the time of day at which DST begins. |
| 3605 |
# An empty string is presumed zero. |
| 3606 |
# startSeconds - The seconds part of the time of day at which DST begins. |
| 3607 |
# An empty string is presumed zero. |
| 3608 |
# endDayOfYear, endJ, endMonth, endWeekOfMonth, endDayOfWeek, |
| 3609 |
# endHours, endMinutes, endSeconds - |
| 3610 |
# Specify the end of DST in the same way that the start* fields |
| 3611 |
# specify the beginning of DST. |
| 3612 |
# |
| 3613 |
# This procedure serves only to break the time specifier into fields. No |
| 3614 |
# attempt is made to canonicalize the fields or supply default values. |
| 3615 |
# |
| 3616 |
#---------------------------------------------------------------------- |
| 3617 |
|
| 3618 |
proc ::tcl::clock::ParsePosixTimeZone { tz } { |
| 3619 |
if {[regexp -expanded -nocase -- { |
| 3620 |
^ |
| 3621 |
# 1 - Standard time zone name |
| 3622 |
([[:alpha:]]+ | <[-+[:alnum:]]+>) |
| 3623 |
# 2 - Standard time zone offset, signum |
| 3624 |
([-+]?) |
| 3625 |
# 3 - Standard time zone offset, hours |
| 3626 |
([[:digit:]]{1,2}) |
| 3627 |
(?: |
| 3628 |
# 4 - Standard time zone offset, minutes |
| 3629 |
: ([[:digit:]]{1,2}) |
| 3630 |
(?: |
| 3631 |
# 5 - Standard time zone offset, seconds |
| 3632 |
: ([[:digit:]]{1,2} ) |
| 3633 |
)? |
| 3634 |
)? |
| 3635 |
(?: |
| 3636 |
# 6 - DST time zone name |
| 3637 |
([[:alpha:]]+ | <[-+[:alnum:]]+>) |
| 3638 |
(?: |
| 3639 |
(?: |
| 3640 |
# 7 - DST time zone offset, signum |
| 3641 |
([-+]?) |
| 3642 |
# 8 - DST time zone offset, hours |
| 3643 |
([[:digit:]]{1,2}) |
| 3644 |
(?: |
| 3645 |
# 9 - DST time zone offset, minutes |
| 3646 |
: ([[:digit:]]{1,2}) |
| 3647 |
(?: |
| 3648 |
# 10 - DST time zone offset, seconds |
| 3649 |
: ([[:digit:]]{1,2}) |
| 3650 |
)? |
| 3651 |
)? |
| 3652 |
)? |
| 3653 |
(?: |
| 3654 |
, |
| 3655 |
(?: |
| 3656 |
# 11 - Optional J in n and Jn form 12 - Day of year |
| 3657 |
( J ? ) ( [[:digit:]]+ ) |
| 3658 |
| M |
| 3659 |
# 13 - Month number 14 - Week of month 15 - Day of week |
| 3660 |
( [[:digit:]] + ) |
| 3661 |
[.] ( [[:digit:]] + ) |
| 3662 |
[.] ( [[:digit:]] + ) |
| 3663 |
) |
| 3664 |
(?: |
| 3665 |
# 16 - Start time of DST - hours |
| 3666 |
/ ( [[:digit:]]{1,2} ) |
| 3667 |
(?: |
| 3668 |
# 17 - Start time of DST - minutes |
| 3669 |
: ( [[:digit:]]{1,2} ) |
| 3670 |
(?: |
| 3671 |
# 18 - Start time of DST - seconds |
| 3672 |
: ( [[:digit:]]{1,2} ) |
| 3673 |
)? |
| 3674 |
)? |
| 3675 |
)? |
| 3676 |
, |
| 3677 |
(?: |
| 3678 |
# 19 - Optional J in n and Jn form 20 - Day of year |
| 3679 |
( J ? ) ( [[:digit:]]+ ) |
| 3680 |
| M |
| 3681 |
# 21 - Month number 22 - Week of month 23 - Day of week |
| 3682 |
( [[:digit:]] + ) |
| 3683 |
[.] ( [[:digit:]] + ) |
| 3684 |
[.] ( [[:digit:]] + ) |
| 3685 |
) |
| 3686 |
(?: |
| 3687 |
# 24 - End time of DST - hours |
| 3688 |
/ ( [[:digit:]]{1,2} ) |
| 3689 |
(?: |
| 3690 |
# 25 - End time of DST - minutes |
| 3691 |
: ( [[:digit:]]{1,2} ) |
| 3692 |
(?: |
| 3693 |
# 26 - End time of DST - seconds |
| 3694 |
: ( [[:digit:]]{1,2} ) |
| 3695 |
)? |
| 3696 |
)? |
| 3697 |
)? |
| 3698 |
)? |
| 3699 |
)? |
| 3700 |
)? |
| 3701 |
$ |
| 3702 |
} $tz -> x(stdName) x(stdSignum) x(stdHours) x(stdMinutes) x(stdSeconds) \ |
| 3703 |
x(dstName) x(dstSignum) x(dstHours) x(dstMinutes) x(dstSeconds) \ |
| 3704 |
x(startJ) x(startDayOfYear) \ |
| 3705 |
x(startMonth) x(startWeekOfMonth) x(startDayOfWeek) \ |
| 3706 |
x(startHours) x(startMinutes) x(startSeconds) \ |
| 3707 |
x(endJ) x(endDayOfYear) \ |
| 3708 |
x(endMonth) x(endWeekOfMonth) x(endDayOfWeek) \ |
| 3709 |
x(endHours) x(endMinutes) x(endSeconds)] } { |
| 3710 |
# it's a good timezone |
| 3711 |
|
| 3712 |
return [array get x] |
| 3713 |
} |
| 3714 |
|
| 3715 |
return -code error\ |
| 3716 |
-errorcode [list CLOCK badTimeZone $tz] \ |
| 3717 |
"unable to parse time zone specification \"$tz\"" |
| 3718 |
} |
| 3719 |
|
| 3720 |
#---------------------------------------------------------------------- |
| 3721 |
# |
| 3722 |
# ProcessPosixTimeZone -- |
| 3723 |
# |
| 3724 |
# Handle a Posix time zone after it's been broken out into fields. |
| 3725 |
# |
| 3726 |
# Parameters: |
| 3727 |
# z - Dictionary returned from 'ParsePosixTimeZone' |
| 3728 |
# |
| 3729 |
# Results: |
| 3730 |
# Returns time zone information for the 'TZData' array. |
| 3731 |
# |
| 3732 |
# Side effects: |
| 3733 |
# None. |
| 3734 |
# |
| 3735 |
#---------------------------------------------------------------------- |
| 3736 |
|
| 3737 |
proc ::tcl::clock::ProcessPosixTimeZone { z } { |
| 3738 |
variable MINWIDE |
| 3739 |
variable TZData |
| 3740 |
|
| 3741 |
# Determine the standard time zone name and seconds east of Greenwich |
| 3742 |
|
| 3743 |
set stdName [dict get $z stdName] |
| 3744 |
if { [string index $stdName 0] eq {<} } { |
| 3745 |
set stdName [string range $stdName 1 end-1] |
| 3746 |
} |
| 3747 |
if { [dict get $z stdSignum] eq {-} } { |
| 3748 |
set stdSignum +1 |
| 3749 |
} else { |
| 3750 |
set stdSignum -1 |
| 3751 |
} |
| 3752 |
set stdHours [lindex [::scan [dict get $z stdHours] %d] 0] |
| 3753 |
if { [dict get $z stdMinutes] ne {} } { |
| 3754 |
set stdMinutes [lindex [::scan [dict get $z stdMinutes] %d] 0] |
| 3755 |
} else { |
| 3756 |
set stdMinutes 0 |
| 3757 |
} |
| 3758 |
if { [dict get $z stdSeconds] ne {} } { |
| 3759 |
set stdSeconds [lindex [::scan [dict get $z stdSeconds] %d] 0] |
| 3760 |
} else { |
| 3761 |
set stdSeconds 0 |
| 3762 |
} |
| 3763 |
set stdOffset [expr { |
| 3764 |
(($stdHours * 60 + $stdMinutes) * 60 + $stdSeconds) * $stdSignum |
| 3765 |
}] |
| 3766 |
set data [list [list $MINWIDE $stdOffset 0 $stdName]] |
| 3767 |
|
| 3768 |
# If there's no daylight zone, we're done |
| 3769 |
|
| 3770 |
set dstName [dict get $z dstName] |
| 3771 |
if { $dstName eq {} } { |
| 3772 |
return $data |
| 3773 |
} |
| 3774 |
if { [string index $dstName 0] eq {<} } { |
| 3775 |
set dstName [string range $dstName 1 end-1] |
| 3776 |
} |
| 3777 |
|
| 3778 |
# Determine the daylight name |
| 3779 |
|
| 3780 |
if { [dict get $z dstSignum] eq {-} } { |
| 3781 |
set dstSignum +1 |
| 3782 |
} else { |
| 3783 |
set dstSignum -1 |
| 3784 |
} |
| 3785 |
if { [dict get $z dstHours] eq {} } { |
| 3786 |
set dstOffset [expr { 3600 + $stdOffset }] |
| 3787 |
} else { |
| 3788 |
set dstHours [lindex [::scan [dict get $z dstHours] %d] 0] |
| 3789 |
if { [dict get $z dstMinutes] ne {} } { |
| 3790 |
set dstMinutes [lindex [::scan [dict get $z dstMinutes] %d] 0] |
| 3791 |
} else { |
| 3792 |
set dstMinutes 0 |
| 3793 |
} |
| 3794 |
if { [dict get $z dstSeconds] ne {} } { |
| 3795 |
set dstSeconds [lindex [::scan [dict get $z dstSeconds] %d] 0] |
| 3796 |
} else { |
| 3797 |
set dstSeconds 0 |
| 3798 |
} |
| 3799 |
set dstOffset [expr { |
| 3800 |
(($dstHours*60 + $dstMinutes) * 60 + $dstSeconds) * $dstSignum |
| 3801 |
}] |
| 3802 |
} |
| 3803 |
|
| 3804 |
# Fill in defaults for European or US DST rules |
| 3805 |
# US start time is the second Sunday in March |
| 3806 |
# EU start time is the last Sunday in March |
| 3807 |
# US end time is the first Sunday in November. |
| 3808 |
# EU end time is the last Sunday in October |
| 3809 |
|
| 3810 |
if { |
| 3811 |
[dict get $z startDayOfYear] eq {} |
| 3812 |
&& [dict get $z startMonth] eq {} |
| 3813 |
} then { |
| 3814 |
if {($stdSignum * $stdHours>=0) && ($stdSignum * $stdHours<=12)} { |
| 3815 |
# EU |
| 3816 |
dict set z startWeekOfMonth 5 |
| 3817 |
if {$stdHours>2} { |
| 3818 |
dict set z startHours 2 |
| 3819 |
} else { |
| 3820 |
dict set z startHours [expr {$stdHours+1}] |
| 3821 |
} |
| 3822 |
} else { |
| 3823 |
# US |
| 3824 |
dict set z startWeekOfMonth 2 |
| 3825 |
dict set z startHours 2 |
| 3826 |
} |
| 3827 |
dict set z startMonth 3 |
| 3828 |
dict set z startDayOfWeek 0 |
| 3829 |
dict set z startMinutes 0 |
| 3830 |
dict set z startSeconds 0 |
| 3831 |
} |
| 3832 |
if { |
| 3833 |
[dict get $z endDayOfYear] eq {} |
| 3834 |
&& [dict get $z endMonth] eq {} |
| 3835 |
} then { |
| 3836 |
if {($stdSignum * $stdHours>=0) && ($stdSignum * $stdHours<=12)} { |
| 3837 |
# EU |
| 3838 |
dict set z endMonth 10 |
| 3839 |
dict set z endWeekOfMonth 5 |
| 3840 |
if {$stdHours>2} { |
| 3841 |
dict set z endHours 3 |
| 3842 |
} else { |
| 3843 |
dict set z endHours [expr {$stdHours+2}] |
| 3844 |
} |
| 3845 |
} else { |
| 3846 |
# US |
| 3847 |
dict set z endMonth 11 |
| 3848 |
dict set z endWeekOfMonth 1 |
| 3849 |
dict set z endHours 2 |
| 3850 |
} |
| 3851 |
dict set z endDayOfWeek 0 |
| 3852 |
dict set z endMinutes 0 |
| 3853 |
dict set z endSeconds 0 |
| 3854 |
} |
| 3855 |
|
| 3856 |
# Put DST in effect in all years from 1916 to 2099. |
| 3857 |
|
| 3858 |
for { set y 1916 } { $y < 2100 } { incr y } { |
| 3859 |
set startTime [DeterminePosixDSTTime $z start $y] |
| 3860 |
incr startTime [expr { - wide($stdOffset) }] |
| 3861 |
set endTime [DeterminePosixDSTTime $z end $y] |
| 3862 |
incr endTime [expr { - wide($dstOffset) }] |
| 3863 |
if { $startTime < $endTime } { |
| 3864 |
lappend data \ |
| 3865 |
[list $startTime $dstOffset 1 $dstName] \ |
| 3866 |
[list $endTime $stdOffset 0 $stdName] |
| 3867 |
} else { |
| 3868 |
lappend data \ |
| 3869 |
[list $endTime $stdOffset 0 $stdName] \ |
| 3870 |
[list $startTime $dstOffset 1 $dstName] |
| 3871 |
} |
| 3872 |
} |
| 3873 |
|
| 3874 |
return $data |
| 3875 |
} |
| 3876 |
|
| 3877 |
#---------------------------------------------------------------------- |
| 3878 |
# |
| 3879 |
# DeterminePosixDSTTime -- |
| 3880 |
# |
| 3881 |
# Determines the time that Daylight Saving Time starts or ends from a |
| 3882 |
# Posix time zone specification. |
| 3883 |
# |
| 3884 |
# Parameters: |
| 3885 |
# z - Time zone data returned from ParsePosixTimeZone. |
| 3886 |
# Missing fields are expected to be filled in with |
| 3887 |
# default values. |
| 3888 |
# bound - The word 'start' or 'end' |
| 3889 |
# y - The year for which the transition time is to be determined. |
| 3890 |
# |
| 3891 |
# Results: |
| 3892 |
# Returns the transition time as a count of seconds from the epoch. The |
| 3893 |
# time is relative to the wall clock, not UTC. |
| 3894 |
# |
| 3895 |
#---------------------------------------------------------------------- |
| 3896 |
|
| 3897 |
proc ::tcl::clock::DeterminePosixDSTTime { z bound y } { |
| 3898 |
|
| 3899 |
variable FEB_28 |
| 3900 |
|
| 3901 |
# Determine the start or end day of DST |
| 3902 |
|
| 3903 |
set date [dict create era CE year $y] |
| 3904 |
set doy [dict get $z ${bound}DayOfYear] |
| 3905 |
if { $doy ne {} } { |
| 3906 |
|
| 3907 |
# Time was specified as a day of the year |
| 3908 |
|
| 3909 |
if { [dict get $z ${bound}J] ne {} |
| 3910 |
&& [IsGregorianLeapYear $y] |
| 3911 |
&& ( $doy > $FEB_28 ) } { |
| 3912 |
incr doy |
| 3913 |
} |
| 3914 |
dict set date dayOfYear $doy |
| 3915 |
set date [GetJulianDayFromEraYearDay $date[set date {}] 2361222] |
| 3916 |
} else { |
| 3917 |
# Time was specified as a day of the week within a month |
| 3918 |
|
| 3919 |
dict set date month [dict get $z ${bound}Month] |
| 3920 |
dict set date dayOfWeek [dict get $z ${bound}DayOfWeek] |
| 3921 |
set dowim [dict get $z ${bound}WeekOfMonth] |
| 3922 |
if { $dowim >= 5 } { |
| 3923 |
set dowim -1 |
| 3924 |
} |
| 3925 |
dict set date dayOfWeekInMonth $dowim |
| 3926 |
set date [GetJulianDayFromEraYearMonthWeekDay $date[set date {}] 2361222] |
| 3927 |
|
| 3928 |
} |
| 3929 |
|
| 3930 |
set jd [dict get $date julianDay] |
| 3931 |
set seconds [expr { |
| 3932 |
wide($jd) * wide(86400) - wide(210866803200) |
| 3933 |
}] |
| 3934 |
|
| 3935 |
set h [dict get $z ${bound}Hours] |
| 3936 |
if { $h eq {} } { |
| 3937 |
set h 2 |
| 3938 |
} else { |
| 3939 |
set h [lindex [::scan $h %d] 0] |
| 3940 |
} |
| 3941 |
set m [dict get $z ${bound}Minutes] |
| 3942 |
if { $m eq {} } { |
| 3943 |
set m 0 |
| 3944 |
} else { |
| 3945 |
set m [lindex [::scan $m %d] 0] |
| 3946 |
} |
| 3947 |
set s [dict get $z ${bound}Seconds] |
| 3948 |
if { $s eq {} } { |
| 3949 |
set s 0 |
| 3950 |
} else { |
| 3951 |
set s [lindex [::scan $s %d] 0] |
| 3952 |
} |
| 3953 |
set tod [expr { ( $h * 60 + $m ) * 60 + $s }] |
| 3954 |
return [expr { $seconds + $tod }] |
| 3955 |
} |
| 3956 |
|
| 3957 |
#---------------------------------------------------------------------- |
| 3958 |
# |
| 3959 |
# GetLocaleEra -- |
| 3960 |
# |
| 3961 |
# Given local time expressed in seconds from the Posix epoch, |
| 3962 |
# determine localized era and year within the era. |
| 3963 |
# |
| 3964 |
# Parameters: |
| 3965 |
# date - Dictionary that must contain the keys, 'localSeconds', |
| 3966 |
# whose value is expressed as the appropriate local time; |
| 3967 |
# and 'year', whose value is the Gregorian year. |
| 3968 |
# etable - Value of the LOCALE_ERAS key in the message catalogue |
| 3969 |
# for the target locale. |
| 3970 |
# |
| 3971 |
# Results: |
| 3972 |
# Returns the dictionary, augmented with the keys, 'localeEra' and |
| 3973 |
# 'localeYear'. |
| 3974 |
# |
| 3975 |
#---------------------------------------------------------------------- |
| 3976 |
|
| 3977 |
proc ::tcl::clock::GetLocaleEra { date etable } { |
| 3978 |
set index [BSearch $etable [dict get $date localSeconds]] |
| 3979 |
if { $index < 0} { |
| 3980 |
dict set date localeEra \ |
| 3981 |
[::format %02d [expr { [dict get $date year] / 100 }]] |
| 3982 |
dict set date localeYear [expr { |
| 3983 |
[dict get $date year] % 100 |
| 3984 |
}] |
| 3985 |
} else { |
| 3986 |
dict set date localeEra [lindex $etable $index 1] |
| 3987 |
dict set date localeYear [expr { |
| 3988 |
[dict get $date year] - [lindex $etable $index 2] |
| 3989 |
}] |
| 3990 |
} |
| 3991 |
return $date |
| 3992 |
} |
| 3993 |
|
| 3994 |
#---------------------------------------------------------------------- |
| 3995 |
# |
| 3996 |
# GetJulianDayFromEraYearDay -- |
| 3997 |
# |
| 3998 |
# Given a year, month and day on the Gregorian calendar, determines |
| 3999 |
# the Julian Day Number beginning at noon on that date. |
| 4000 |
# |
| 4001 |
# Parameters: |
| 4002 |
# date -- A dictionary in which the 'era', 'year', and |
| 4003 |
# 'dayOfYear' slots are populated. The calendar in use |
| 4004 |
# is determined by the date itself relative to: |
| 4005 |
# changeover -- Julian day on which the Gregorian calendar was |
| 4006 |
# adopted in the current locale. |
| 4007 |
# |
| 4008 |
# Results: |
| 4009 |
# Returns the given dictionary augmented with a 'julianDay' key whose |
| 4010 |
# value is the desired Julian Day Number, and a 'gregorian' key that |
| 4011 |
# specifies whether the calendar is Gregorian (1) or Julian (0). |
| 4012 |
# |
| 4013 |
# Side effects: |
| 4014 |
# None. |
| 4015 |
# |
| 4016 |
# Bugs: |
| 4017 |
# This code needs to be moved to the C layer. |
| 4018 |
# |
| 4019 |
#---------------------------------------------------------------------- |
| 4020 |
|
| 4021 |
proc ::tcl::clock::GetJulianDayFromEraYearDay {date changeover} { |
| 4022 |
# Get absolute year number from the civil year |
| 4023 |
|
| 4024 |
switch -exact -- [dict get $date era] { |
| 4025 |
BCE { |
| 4026 |
set year [expr { 1 - [dict get $date year] }] |
| 4027 |
} |
| 4028 |
CE { |
| 4029 |
set year [dict get $date year] |
| 4030 |
} |
| 4031 |
} |
| 4032 |
set ym1 [expr { $year - 1 }] |
| 4033 |
|
| 4034 |
# Try the Gregorian calendar first. |
| 4035 |
|
| 4036 |
dict set date gregorian 1 |
| 4037 |
set jd [expr { |
| 4038 |
1721425 |
| 4039 |
+ [dict get $date dayOfYear] |
| 4040 |
+ ( 365 * $ym1 ) |
| 4041 |
+ ( $ym1 / 4 ) |
| 4042 |
- ( $ym1 / 100 ) |
| 4043 |
+ ( $ym1 / 400 ) |
| 4044 |
}] |
| 4045 |
|
| 4046 |
# If the date is before the Gregorian change, use the Julian calendar. |
| 4047 |
|
| 4048 |
if { $jd < $changeover } { |
| 4049 |
dict set date gregorian 0 |
| 4050 |
set jd [expr { |
| 4051 |
1721423 |
| 4052 |
+ [dict get $date dayOfYear] |
| 4053 |
+ ( 365 * $ym1 ) |
| 4054 |
+ ( $ym1 / 4 ) |
| 4055 |
}] |
| 4056 |
} |
| 4057 |
|
| 4058 |
dict set date julianDay $jd |
| 4059 |
return $date |
| 4060 |
} |
| 4061 |
|
| 4062 |
#---------------------------------------------------------------------- |
| 4063 |
# |
| 4064 |
# GetJulianDayFromEraYearMonthWeekDay -- |
| 4065 |
# |
| 4066 |
# Determines the Julian Day number corresponding to the nth given |
| 4067 |
# day-of-the-week in a given month. |
| 4068 |
# |
| 4069 |
# Parameters: |
| 4070 |
# date - Dictionary containing the keys, 'era', 'year', 'month' |
| 4071 |
# 'weekOfMonth', 'dayOfWeek', and 'dayOfWeekInMonth'. |
| 4072 |
# changeover - Julian Day of adoption of the Gregorian calendar |
| 4073 |
# |
| 4074 |
# Results: |
| 4075 |
# Returns the given dictionary, augmented with a 'julianDay' key. |
| 4076 |
# |
| 4077 |
# Side effects: |
| 4078 |
# None. |
| 4079 |
# |
| 4080 |
# Bugs: |
| 4081 |
# This code needs to be moved to the C layer. |
| 4082 |
# |
| 4083 |
#---------------------------------------------------------------------- |
| 4084 |
|
| 4085 |
proc ::tcl::clock::GetJulianDayFromEraYearMonthWeekDay {date changeover} { |
| 4086 |
# Come up with a reference day; either the zeroeth day of the given month |
| 4087 |
# (dayOfWeekInMonth >= 0) or the seventh day of the following month |
| 4088 |
# (dayOfWeekInMonth < 0) |
| 4089 |
|
| 4090 |
set date2 $date |
| 4091 |
set week [dict get $date dayOfWeekInMonth] |
| 4092 |
if { $week >= 0 } { |
| 4093 |
dict set date2 dayOfMonth 0 |
| 4094 |
} else { |
| 4095 |
dict incr date2 month |
| 4096 |
dict set date2 dayOfMonth 7 |
| 4097 |
} |
| 4098 |
set date2 [GetJulianDayFromEraYearMonthDay $date2[set date2 {}] \ |
| 4099 |
$changeover] |
| 4100 |
set wd0 [WeekdayOnOrBefore [dict get $date dayOfWeek] \ |
| 4101 |
[dict get $date2 julianDay]] |
| 4102 |
dict set date julianDay [expr { $wd0 + 7 * $week }] |
| 4103 |
return $date |
| 4104 |
} |
| 4105 |
|
| 4106 |
#---------------------------------------------------------------------- |
| 4107 |
# |
| 4108 |
# IsGregorianLeapYear -- |
| 4109 |
# |
| 4110 |
# Determines whether a given date represents a leap year in the |
| 4111 |
# Gregorian calendar. |
| 4112 |
# |
| 4113 |
# Parameters: |
| 4114 |
# date -- The date to test. The fields, 'era', 'year' and 'gregorian' |
| 4115 |
# must be set. |
| 4116 |
# |
| 4117 |
# Results: |
| 4118 |
# Returns 1 if the year is a leap year, 0 otherwise. |
| 4119 |
# |
| 4120 |
# Side effects: |
| 4121 |
# None. |
| 4122 |
# |
| 4123 |
#---------------------------------------------------------------------- |
| 4124 |
|
| 4125 |
proc ::tcl::clock::IsGregorianLeapYear { date } { |
| 4126 |
switch -exact -- [dict get $date era] { |
| 4127 |
BCE { |
| 4128 |
set year [expr { 1 - [dict get $date year]}] |
| 4129 |
} |
| 4130 |
CE { |
| 4131 |
set year [dict get $date year] |
| 4132 |
} |
| 4133 |
} |
| 4134 |
if { $year % 4 != 0 } { |
| 4135 |
return 0 |
| 4136 |
} elseif { ![dict get $date gregorian] } { |
| 4137 |
return 1 |
| 4138 |
} elseif { $year % 400 == 0 } { |
| 4139 |
return 1 |
| 4140 |
} elseif { $year % 100 == 0 } { |
| 4141 |
return 0 |
| 4142 |
} else { |
| 4143 |
return 1 |
| 4144 |
} |
| 4145 |
} |
| 4146 |
|
| 4147 |
#---------------------------------------------------------------------- |
| 4148 |
# |
| 4149 |
# WeekdayOnOrBefore -- |
| 4150 |
# |
| 4151 |
# Determine the nearest day of week (given by the 'weekday' parameter, |
| 4152 |
# Sunday==0) on or before a given Julian Day. |
| 4153 |
# |
| 4154 |
# Parameters: |
| 4155 |
# weekday -- Day of the week |
| 4156 |
# j -- Julian Day number |
| 4157 |
# |
| 4158 |
# Results: |
| 4159 |
# Returns the Julian Day Number of the desired date. |
| 4160 |
# |
| 4161 |
# Side effects: |
| 4162 |
# None. |
| 4163 |
# |
| 4164 |
#---------------------------------------------------------------------- |
| 4165 |
|
| 4166 |
proc ::tcl::clock::WeekdayOnOrBefore { weekday j } { |
| 4167 |
set k [expr { ( $weekday + 6 ) % 7 }] |
| 4168 |
return [expr { $j - ( $j - $k ) % 7 }] |
| 4169 |
} |
| 4170 |
|
| 4171 |
#---------------------------------------------------------------------- |
| 4172 |
# |
| 4173 |
# BSearch -- |
| 4174 |
# |
| 4175 |
# Service procedure that does binary search in several places inside the |
| 4176 |
# 'clock' command. |
| 4177 |
# |
| 4178 |
# Parameters: |
| 4179 |
# list - List of lists, sorted in ascending order by the |
| 4180 |
# first elements |
| 4181 |
# key - Value to search for |
| 4182 |
# |
| 4183 |
# Results: |
| 4184 |
# Returns the index of the greatest element in $list that is less than |
| 4185 |
# or equal to $key. |
| 4186 |
# |
| 4187 |
# Side effects: |
| 4188 |
# None. |
| 4189 |
# |
| 4190 |
#---------------------------------------------------------------------- |
| 4191 |
|
| 4192 |
proc ::tcl::clock::BSearch { list key } { |
| 4193 |
if {[llength $list] == 0} { |
| 4194 |
return -1 |
| 4195 |
} |
| 4196 |
if { $key < [lindex $list 0 0] } { |
| 4197 |
return -1 |
| 4198 |
} |
| 4199 |
|
| 4200 |
set l 0 |
| 4201 |
set u [expr { [llength $list] - 1 }] |
| 4202 |
|
| 4203 |
while { $l < $u } { |
| 4204 |
# At this point, we know that |
| 4205 |
# $k >= [lindex $list $l 0] |
| 4206 |
# Either $u == [llength $list] or else $k < [lindex $list $u+1 0] |
| 4207 |
# We find the midpoint of the interval {l,u} rounded UP, compare |
| 4208 |
# against it, and set l or u to maintain the invariant. Note that the |
| 4209 |
# interval shrinks at each step, guaranteeing convergence. |
| 4210 |
|
| 4211 |
set m [expr { ( $l + $u + 1 ) / 2 }] |
| 4212 |
if { $key >= [lindex $list $m 0] } { |
| 4213 |
set l $m |
| 4214 |
} else { |
| 4215 |
set u [expr { $m - 1 }] |
| 4216 |
} |
| 4217 |
} |
| 4218 |
|
| 4219 |
return $l |
| 4220 |
} |
| 4221 |
|
| 4222 |
#---------------------------------------------------------------------- |
| 4223 |
# |
| 4224 |
# clock add -- |
| 4225 |
# |
| 4226 |
# Adds an offset to a given time. |
| 4227 |
# |
| 4228 |
# Syntax: |
| 4229 |
# clock add clockval ?count unit?... ?-option value? |
| 4230 |
# |
| 4231 |
# Parameters: |
| 4232 |
# clockval -- Starting time value |
| 4233 |
# count -- Amount of a unit of time to add |
| 4234 |
# unit -- Unit of time to add, must be one of: |
| 4235 |
# years year months month weeks week |
| 4236 |
# days day hours hour minutes minute |
| 4237 |
# seconds second |
| 4238 |
# |
| 4239 |
# Options: |
| 4240 |
# -gmt BOOLEAN |
| 4241 |
# (Deprecated) Flag synonymous with '-timezone :GMT' |
| 4242 |
# -timezone ZONE |
| 4243 |
# Name of the time zone in which calculations are to be done. |
| 4244 |
# -locale NAME |
| 4245 |
# Name of the locale in which calculations are to be done. |
| 4246 |
# Used to determine the Gregorian change date. |
| 4247 |
# |
| 4248 |
# Results: |
| 4249 |
# Returns the given time adjusted by the given offset(s) in |
| 4250 |
# order. |
| 4251 |
# |
| 4252 |
# Notes: |
| 4253 |
# It is possible that adding a number of months or years will adjust the |
| 4254 |
# day of the month as well. For instance, the time at one month after |
| 4255 |
# 31 January is either 28 or 29 February, because February has fewer |
| 4256 |
# than 31 days. |
| 4257 |
# |
| 4258 |
#---------------------------------------------------------------------- |
| 4259 |
|
| 4260 |
proc ::tcl::clock::add { clockval args } { |
| 4261 |
if { [llength $args] % 2 != 0 } { |
| 4262 |
set cmdName "clock add" |
| 4263 |
return -code error \ |
| 4264 |
-errorcode [list CLOCK wrongNumArgs] \ |
| 4265 |
"wrong \# args: should be\ |
| 4266 |
\"$cmdName clockval ?number units?...\ |
| 4267 |
?-gmt boolean? ?-locale LOCALE? ?-timezone ZONE?\"" |
| 4268 |
} |
| 4269 |
if { [catch { expr {wide($clockval)} } result] } { |
| 4270 |
return -code error $result |
| 4271 |
} |
| 4272 |
|
| 4273 |
set offsets {} |
| 4274 |
set gmt 0 |
| 4275 |
set locale c |
| 4276 |
set timezone [GetSystemTimeZone] |
| 4277 |
|
| 4278 |
foreach { a b } $args { |
| 4279 |
if { [string is integer -strict $a] } { |
| 4280 |
lappend offsets $a $b |
| 4281 |
} else { |
| 4282 |
switch -exact -- $a { |
| 4283 |
-g - -gm - -gmt { |
| 4284 |
set saw(-gmt) {} |
| 4285 |
set gmt $b |
| 4286 |
} |
| 4287 |
-l - -lo - -loc - -loca - -local - -locale { |
| 4288 |
set locale [string tolower $b] |
| 4289 |
} |
| 4290 |
-t - -ti - -tim - -time - -timez - -timezo - -timezon - |
| 4291 |
-timezone { |
| 4292 |
set saw(-timezone) {} |
| 4293 |
set timezone $b |
| 4294 |
} |
| 4295 |
default { |
| 4296 |
throw [list CLOCK badOption $a] \ |
| 4297 |
"bad option \"$a\",\ |
| 4298 |
must be -gmt, -locale or -timezone" |
| 4299 |
} |
| 4300 |
} |
| 4301 |
} |
| 4302 |
} |
| 4303 |
|
| 4304 |
# Check options for validity |
| 4305 |
|
| 4306 |
if { [info exists saw(-gmt)] && [info exists saw(-timezone)] } { |
| 4307 |
return -code error \ |
| 4308 |
-errorcode [list CLOCK gmtWithTimezone] \ |
| 4309 |
"cannot use -gmt and -timezone in same call" |
| 4310 |
} |
| 4311 |
if { [catch { expr { wide($clockval) } } result] } { |
| 4312 |
return -code error "expected integer but got \"$clockval\"" |
| 4313 |
} |
| 4314 |
if { ![string is boolean -strict $gmt] } { |
| 4315 |
return -code error "expected boolean value but got \"$gmt\"" |
| 4316 |
} elseif { $gmt } { |
| 4317 |
set timezone :GMT |
| 4318 |
} |
| 4319 |
|
| 4320 |
EnterLocale $locale |
| 4321 |
|
| 4322 |
set changeover [mc GREGORIAN_CHANGE_DATE] |
| 4323 |
|
| 4324 |
if {[catch {SetupTimeZone $timezone} retval opts]} { |
| 4325 |
dict unset opts -errorinfo |
| 4326 |
return -options $opts $retval |
| 4327 |
} |
| 4328 |
|
| 4329 |
try { |
| 4330 |
foreach { quantity unit } $offsets { |
| 4331 |
switch -exact -- $unit { |
| 4332 |
years - year { |
| 4333 |
set clockval [AddMonths [expr { 12 * $quantity }] \ |
| 4334 |
$clockval $timezone $changeover] |
| 4335 |
} |
| 4336 |
months - month { |
| 4337 |
set clockval [AddMonths $quantity $clockval $timezone \ |
| 4338 |
$changeover] |
| 4339 |
} |
| 4340 |
|
| 4341 |
weeks - week { |
| 4342 |
set clockval [AddDays [expr { 7 * $quantity }] \ |
| 4343 |
$clockval $timezone $changeover] |
| 4344 |
} |
| 4345 |
days - day { |
| 4346 |
set clockval [AddDays $quantity $clockval $timezone \ |
| 4347 |
$changeover] |
| 4348 |
} |
| 4349 |
|
| 4350 |
hours - hour { |
| 4351 |
set clockval [expr { 3600 * $quantity + $clockval }] |
| 4352 |
} |
| 4353 |
minutes - minute { |
| 4354 |
set clockval [expr { 60 * $quantity + $clockval }] |
| 4355 |
} |
| 4356 |
seconds - second { |
| 4357 |
set clockval [expr { $quantity + $clockval }] |
| 4358 |
} |
| 4359 |
|
| 4360 |
default { |
| 4361 |
throw [list CLOCK badUnit $unit] \ |
| 4362 |
"unknown unit \"$unit\", must be \ |
| 4363 |
years, months, weeks, days, hours, minutes or seconds" |
| 4364 |
} |
| 4365 |
} |
| 4366 |
} |
| 4367 |
return $clockval |
| 4368 |
} trap CLOCK {result opts} { |
| 4369 |
# Conceal the innards of [clock] when it's an expected error |
| 4370 |
dict unset opts -errorinfo |
| 4371 |
return -options $opts $result |
| 4372 |
} |
| 4373 |
} |
| 4374 |
|
| 4375 |
#---------------------------------------------------------------------- |
| 4376 |
# |
| 4377 |
# AddMonths -- |
| 4378 |
# |
| 4379 |
# Add a given number of months to a given clock value in a given |
| 4380 |
# time zone. |
| 4381 |
# |
| 4382 |
# Parameters: |
| 4383 |
# months - Number of months to add (may be negative) |
| 4384 |
# clockval - Seconds since the epoch before the operation |
| 4385 |
# timezone - Time zone in which the operation is to be performed |
| 4386 |
# |
| 4387 |
# Results: |
| 4388 |
# Returns the new clock value as a number of seconds since |
| 4389 |
# the epoch. |
| 4390 |
# |
| 4391 |
# Side effects: |
| 4392 |
# None. |
| 4393 |
# |
| 4394 |
#---------------------------------------------------------------------- |
| 4395 |
|
| 4396 |
proc ::tcl::clock::AddMonths { months clockval timezone changeover } { |
| 4397 |
variable DaysInRomanMonthInCommonYear |
| 4398 |
variable DaysInRomanMonthInLeapYear |
| 4399 |
variable TZData |
| 4400 |
|
| 4401 |
# Convert the time to year, month, day, and fraction of day. |
| 4402 |
|
| 4403 |
set date [GetDateFields $clockval $TZData($timezone) $changeover] |
| 4404 |
dict set date secondOfDay [expr { |
| 4405 |
[dict get $date localSeconds] % 86400 |
| 4406 |
}] |
| 4407 |
dict set date tzName $timezone |
| 4408 |
|
| 4409 |
# Add the requisite number of months |
| 4410 |
|
| 4411 |
set m [dict get $date month] |
| 4412 |
incr m $months |
| 4413 |
incr m -1 |
| 4414 |
set delta [expr { $m / 12 }] |
| 4415 |
set mm [expr { $m % 12 }] |
| 4416 |
dict set date month [expr { $mm + 1 }] |
| 4417 |
dict incr date year $delta |
| 4418 |
|
| 4419 |
# If the date doesn't exist in the current month, repair it |
| 4420 |
|
| 4421 |
if { [IsGregorianLeapYear $date] } { |
| 4422 |
set hath [lindex $DaysInRomanMonthInLeapYear $mm] |
| 4423 |
} else { |
| 4424 |
set hath [lindex $DaysInRomanMonthInCommonYear $mm] |
| 4425 |
} |
| 4426 |
if { [dict get $date dayOfMonth] > $hath } { |
| 4427 |
dict set date dayOfMonth $hath |
| 4428 |
} |
| 4429 |
|
| 4430 |
# Reconvert to a number of seconds |
| 4431 |
|
| 4432 |
set date [GetJulianDayFromEraYearMonthDay \ |
| 4433 |
$date[set date {}]\ |
| 4434 |
$changeover] |
| 4435 |
dict set date localSeconds [expr { |
| 4436 |
-210866803200 |
| 4437 |
+ ( 86400 * wide([dict get $date julianDay]) ) |
| 4438 |
+ [dict get $date secondOfDay] |
| 4439 |
}] |
| 4440 |
set date [ConvertLocalToUTC $date[set date {}] $TZData($timezone) \ |
| 4441 |
$changeover] |
| 4442 |
|
| 4443 |
return [dict get $date seconds] |
| 4444 |
|
| 4445 |
} |
| 4446 |
|
| 4447 |
#---------------------------------------------------------------------- |
| 4448 |
# |
| 4449 |
# AddDays -- |
| 4450 |
# |
| 4451 |
# Add a given number of days to a given clock value in a given time |
| 4452 |
# zone. |
| 4453 |
# |
| 4454 |
# Parameters: |
| 4455 |
# days - Number of days to add (may be negative) |
| 4456 |
# clockval - Seconds since the epoch before the operation |
| 4457 |
# timezone - Time zone in which the operation is to be performed |
| 4458 |
# changeover - Julian Day on which the Gregorian calendar was adopted |
| 4459 |
# in the target locale. |
| 4460 |
# |
| 4461 |
# Results: |
| 4462 |
# Returns the new clock value as a number of seconds since the epoch. |
| 4463 |
# |
| 4464 |
# Side effects: |
| 4465 |
# None. |
| 4466 |
# |
| 4467 |
#---------------------------------------------------------------------- |
| 4468 |
|
| 4469 |
proc ::tcl::clock::AddDays { days clockval timezone changeover } { |
| 4470 |
variable TZData |
| 4471 |
|
| 4472 |
# Convert the time to Julian Day |
| 4473 |
|
| 4474 |
set date [GetDateFields $clockval $TZData($timezone) $changeover] |
| 4475 |
dict set date secondOfDay [expr { |
| 4476 |
[dict get $date localSeconds] % 86400 |
| 4477 |
}] |
| 4478 |
dict set date tzName $timezone |
| 4479 |
|
| 4480 |
# Add the requisite number of days |
| 4481 |
|
| 4482 |
dict incr date julianDay $days |
| 4483 |
|
| 4484 |
# Reconvert to a number of seconds |
| 4485 |
|
| 4486 |
dict set date localSeconds [expr { |
| 4487 |
-210866803200 |
| 4488 |
+ ( 86400 * wide([dict get $date julianDay]) ) |
| 4489 |
+ [dict get $date secondOfDay] |
| 4490 |
}] |
| 4491 |
set date [ConvertLocalToUTC $date[set date {}] $TZData($timezone) \ |
| 4492 |
$changeover] |
| 4493 |
|
| 4494 |
return [dict get $date seconds] |
| 4495 |
|
| 4496 |
} |
| 4497 |
|
| 4498 |
#---------------------------------------------------------------------- |
| 4499 |
# |
| 4500 |
# ChangeCurrentLocale -- |
| 4501 |
# |
| 4502 |
# The global locale was changed within msgcat. |
| 4503 |
# Clears the buffered parse functions of the current locale. |
| 4504 |
# |
| 4505 |
# Parameters: |
| 4506 |
# loclist (ignored) |
| 4507 |
# |
| 4508 |
# Results: |
| 4509 |
# None. |
| 4510 |
# |
| 4511 |
# Side effects: |
| 4512 |
# Buffered parse functions are cleared. |
| 4513 |
# |
| 4514 |
#---------------------------------------------------------------------- |
| 4515 |
|
| 4516 |
proc ::tcl::clock::ChangeCurrentLocale {args} { |
| 4517 |
variable FormatProc |
| 4518 |
variable LocaleNumeralCache |
| 4519 |
variable CachedSystemTimeZone |
| 4520 |
variable TimeZoneBad |
| 4521 |
|
| 4522 |
foreach p [info procs [namespace current]::scanproc'*'current] { |
| 4523 |
rename $p {} |
| 4524 |
} |
| 4525 |
foreach p [info procs [namespace current]::formatproc'*'current] { |
| 4526 |
rename $p {} |
| 4527 |
} |
| 4528 |
|
| 4529 |
catch {array unset FormatProc *'current} |
| 4530 |
set LocaleNumeralCache {} |
| 4531 |
} |
| 4532 |
|
| 4533 |
#---------------------------------------------------------------------- |
| 4534 |
# |
| 4535 |
# ClearCaches -- |
| 4536 |
# |
| 4537 |
# Clears all caches to reclaim the memory used in [clock] |
| 4538 |
# |
| 4539 |
# Parameters: |
| 4540 |
# None. |
| 4541 |
# |
| 4542 |
# Results: |
| 4543 |
# None. |
| 4544 |
# |
| 4545 |
# Side effects: |
| 4546 |
# Caches are cleared. |
| 4547 |
# |
| 4548 |
#---------------------------------------------------------------------- |
| 4549 |
|
| 4550 |
proc ::tcl::clock::ClearCaches {} { |
| 4551 |
variable FormatProc |
| 4552 |
variable LocaleNumeralCache |
| 4553 |
variable CachedSystemTimeZone |
| 4554 |
variable TimeZoneBad |
| 4555 |
|
| 4556 |
foreach p [info procs [namespace current]::scanproc'*] { |
| 4557 |
rename $p {} |
| 4558 |
} |
| 4559 |
foreach p [info procs [namespace current]::formatproc'*] { |
| 4560 |
rename $p {} |
| 4561 |
} |
| 4562 |
|
| 4563 |
catch {unset FormatProc} |
| 4564 |
set LocaleNumeralCache {} |
| 4565 |
catch {unset CachedSystemTimeZone} |
| 4566 |
set TimeZoneBad {} |
| 4567 |
InitTZData |
| 4568 |
} |