clock.tcl 126 KB

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