summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/clock/iso8601.tcl
blob: c31df61c2a114d457608b87e97aa0b735e7e4270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
## -*- tcl -*-
# # ## ### ##### ######## ############# #####################
## Copyright (c) 2004 Kevin Kenny
## Origin http://wiki.tcl.tk/13094
## Modified for Tcl 8.5 only (eval -> {*}).

# # ## ### ##### ######## ############# #####################
## Requisites

package require Tcl 8.5
package provide clock::iso8601 0.1
namespace eval ::clock::iso8601 {}

# # ## ### ##### ######## ############# #####################
## API

# iso8601::parse_date --
#
#       Parse an ISO8601 date/time string in an unknown variant.
#
# Parameters:
#       string -- String to parse
#       args -- Arguments as for [clock scan]; may include any of
#               the '-base', '-gmt', '-locale' or '-timezone options.
#
# Results:
#       Returns the given date in seconds from the Posix epoch.

proc ::clock::iso8601::parse_date { string args } {
    variable DatePatterns
    variable Repattern
    foreach { regex interpretation } $DatePatterns {
	if { [regexp "^$regex\$" $string] } {
	    #puts A|$string|\t|$regex|\t|$interpretation|

	    # For incomplete dates (month and/or day missing), we have
	    # to set our own default values to overcome clock scan's
	    # settings. We do this by switching to a different pattern
	    # and extending the input properly for that pattern.

	    if {[dict exists $Repattern $interpretation]} {
		lassign [dict get $Repattern $interpretation] interpretation adjust modifier
		{*}$modifier
		# adjust irrelevant here, see parse_time for use.
	    }

	    #puts B|$string|\t|$regex|\t|$interpretation|
	    return [clock scan $string -format $interpretation {*}$args]
	}
    }
    return -code error "not an iso8601 date string"
}

# iso8601::parse_time --
#
#       Parse a point-in-time in ISO8601 format
#
# Parameters:
#       string -- String to parse
#       args -- Arguments as for [clock scan]; may include any of
#               the '-base', '-gmt', '-locale' or '-timezone options.
#
# Results:
#       Returns the given time in seconds from the Posix epoch.

proc ::clock::iso8601::parse_time { string args } {
    variable DatePatterns
    variable Repattern
    if {![MatchTime $string field]} {
	return -code error "not an iso8601 time string"
    }

    #parray field
    #puts A|$string|

    set pattern {}
    foreach {regex interpretation} $DatePatterns {
	if {[Has $interpretation tstart]} {
	    append pattern $interpretation
	}
    }

    if {[dict exists $Repattern $pattern]} {
	lassign [dict get $Repattern $pattern] interpretation adjust modifier
	{*}$modifier
	incr tstart $adjust
    }

    append pattern [Get T len]
    incr tstart $len

    if {[Has %H tstart]} {
	append pattern %H [Get Hcolon len]
	incr tstart $len

	if {[Has %M tstart]} {
	    append pattern %M [Get Mcolon len]
	    incr tstart $len

	    if {[Has %S tstart]} {
		append pattern %S
	    } else {
		# No seconds, default to start of minute.
		append pattern %S
		Insert string $tstart 00
	    }
	} else {
	    # No minutes, nor seconds, default to start of hour.
	    append pattern %M%S
	    Insert string $tstart 0000
	}
    } else {
	# No time information, default to midnight.
	append pattern %H%M%S
	Insert string $tstart 000000
    }
    if {[Has %Z _]} {
	append pattern %Z
    }

    #puts B|$string|\t|$pattern|
    return [clock scan $string -format $pattern {*}$args]
}

# # ## ### ##### ######## ############# #####################

proc ::clock::iso8601::Get {x lv} {
    upvar 1 field field string string $lv len
    lassign $field($x) s e
    if {($s >= 0) && ($e >= 0)} {
	set len [expr {$e - $s + 1}]
	return [string range $string $s $e]
    }
    set len 0
    return ""

}

proc ::clock::iso8601::Has {x nv} {
    upvar 1 field field string string $nv next
    lassign $field($x) s e
    if {($s >= 0) && ($e >= 0)} {
	set  next $e
	incr next
	return 1
    }
    return 0
}

proc ::clock::iso8601::Insert {sv index str} {
    upvar 1 $sv string
    append r [string range $string 0 ${index}-1]
    append r $str
    append r [string range $string $index end]
    set string $r
    return
}

# # ## ### ##### ######## ############# #####################
## State

namespace eval ::clock::iso8601 {

    namespace export parse_date parse_time
    namespace ensemble create

    # Enumerate the patterns that we recognize for an ISO8601 date as both
    # the regexp patterns that match them and the [clock] patterns that scan
    # them.

    variable DatePatterns {
	{\d\d\d\d-\d\d-\d\d}            {%Y-%m-%d}
	{\d\d\d\d\d\d\d\d}              {%Y%m%d}
	{\d\d\d\d-\d\d\d}               {%Y-%j}
	{\d\d\d\d\d\d\d}                {%Y%j}
	{\d\d-\d\d-\d\d}                {%y-%m-%d}
	{\d\d\d\d-\d\d}                 {%Y-%m}
	{\d\d\d\d\d\d}                  {%y%m%d}
	{\d\d-\d\d\d}                   {%y-%j}
	{\d\d\d\d\d}                    {%y%j}
	{--\d\d-\d\d}                   {--%m-%d}
	{--\d\d\d\d}                    {--%m%d}
	{--\d\d\d}                      {--%j}
	{---\d\d}                       {---%d}
	{\d\d\d\d-W\d\d-\d}             {%G-W%V-%u}
	{\d\d\d\dW\d\d\d}               {%GW%V%u}
	{\d\d-W\d\d-\d}                 {%g-W%V-%u}
	{\d\dW\d\d\d}                   {%gW%V%u}
	{\d\d\d\d-W\d\d}                {%G-W%V}
	{\d\d\d\dW\d\d}                 {%GW%V}
	{-W\d\d-\d}                     {-W%V-%u}
	{-W\d\d\d}                      {-W%V%u}
	{-W-\d}                         {%u}
	{\d\d\d\d}                      {%Y}
    }

    # Dictionary of the patterns requiring modifications to the input
    # for proper month and/or day defaults.
    variable Repattern {
	%Y-%m  {%Y-%m-%d  3 {Insert string 7 -01}}
	%Y     {%Y-%m-%d  5 {Insert string 4 -01-01}}
	%G-W%V {%G-W%V-%u 1 {Insert string 8 -1}}
	%GW%V  {%GW%V%u   1 {Insert string 6 1}}
    }
}

# # ## ### ##### ######## ############# #####################
## Initialization

apply {{} {
    # MatchTime -- (constructed procedure)
    #
    #   Match an ISO8601 date/time string and indicate how it matched.
    #
    # Parameters:
    #   string -- String to match.
    #   fieldArray -- Name of an array in caller's scope that will receive
    #                 parsed fields of the time.
    #
    # Results:
    #   Returns 1 if the time was scanned successfully, 0 otherwise.
    #
    # Side effects:
    #   Initializes the field array.  The keys that are significant:
    #           - Any date pattern in 'DatePatterns' indicates that the
    #             corresponding value, if non-empty, contains a date string
    #             in the given format.
    #           - The patterns T, Hcolon, and Mcolon indicate a literal
    #             T preceding the time, a colon following the hour, or
    #             a colon following the minute.
    #           - %H, %M, %S, and %Z indicate the presence of the
    #             corresponding parts of the time.

    variable DatePatterns

    set cmd {regexp -indices -expanded -nocase -- {PATTERN} $timeString ->}
    set re \(?:\(?:
    set sep {}
    foreach {regex interpretation} $DatePatterns {
	append re $sep \( $regex \)
	append cmd " " [list field($interpretation)]
	set sep |
    }
    append re \) {(T|[[:space:]]+)} \)?
    append cmd { field(T)}
    append re {(\d\d)(?:(:?)(\d\d)(?:(:?)(\d\d)?))?}
    append cmd { field(%H) field(Hcolon) } {field(%M) field(Mcolon) field(%S)}
    append re {[[:space:]]*(Z|[-+]\d\d:?\d\d)?}
    append cmd { field(%Z)}
    set cmd [string map [list {{PATTERN}} [list $re]] \
		 $cmd]

    proc MatchTime { timeString fieldArray } "
             upvar 1 \$fieldArray field
             $cmd
         "

    #puts [info body MatchTime]

} ::clock::iso8601}

# # ## ### ##### ######## ############# #####################

return
# Usage examples, disabled.

if { [info exists ::argv0] && ( $::argv0 eq [info script] ) } {
    puts "::clock::iso8601::parse_date"
    puts [::clock::iso8601::parse_date 1970-01-02 -timezone :UTC]
    puts [::clock::iso8601::parse_date 1970-W01-5 -timezone :UTC]
    puts [time {::clock::iso8601::parse_date 1970-01-02 -timezone :UTC} 1000]
    puts [time {::clock::iso8601::parse_date 1970-W01-5 -timezone :UTC} 1000]
    puts "::clock::iso8601::parse_time"
    puts [clock format [::clock::iso8601::parse_time 2004-W33-2T18:52:24Z] \
	      -format {%X %x %z} -locale system]
    puts [clock format [::clock::iso8601::parse_time 18:52:24Z] \
	      -format {%X %x %z} -locale system]
    puts [time {::clock::iso8601::parse_time 2004-W33-2T18:52:24Z} 1000]
    puts [time {::clock::iso8601::parse_time 18:52:24Z} 1000]
}