3
3
// that can be found in the LICENSE file.
4
4
module time
5
5
6
- pub struct TimeParseError {
7
- msg string
8
- code int
9
- }
10
-
11
- fn error_invalid_time (code int ) IError {
12
- return TimeParseError{
13
- msg: 'Invalid time format code: $code '
14
- code: code
15
- }
16
- }
17
-
18
- // parse returns time from a date string in "YYYY-MM-DD HH:MM:SS" format.
19
- pub fn parse (s string ) ? Time {
20
- if s == '' {
21
- return error_invalid_time (0 )
22
- }
23
- pos := s.index (' ' ) or { return error_invalid_time (1 ) }
24
- symd := s[..pos]
25
- ymd := symd.split ('-' )
26
- if ymd.len != 3 {
27
- return error_invalid_time (2 )
28
- }
29
- shms := s[pos..]
30
- hms := shms.split (':' )
31
- hour_ := hms[0 ][1 ..]
32
- minute_ := hms[1 ]
33
- second_ := hms[2 ]
34
- //
35
- iyear := ymd[0 ].int ()
36
- imonth := ymd[1 ].int ()
37
- iday := ymd[2 ].int ()
38
- ihour := hour_.int ()
39
- iminute := minute_.int ()
40
- isecond := second_.int ()
41
- // eprintln('>> iyear: $iyear | imonth: $imonth | iday: $iday | ihour: $ihour | iminute: $iminute | isecond: $isecond')
42
- if iyear > 9999 || iyear < - 9999 {
43
- return error_invalid_time (3 )
44
- }
45
- if imonth > 12 || imonth < 1 {
46
- return error_invalid_time (4 )
47
- }
48
- if iday > 31 || iday < 1 {
49
- return error_invalid_time (5 )
50
- }
51
- if ihour > 23 || ihour < 0 {
52
- return error_invalid_time (6 )
53
- }
54
- if iminute > 59 || iminute < 0 {
55
- return error_invalid_time (7 )
56
- }
57
- if isecond > 59 || isecond < 0 {
58
- return error_invalid_time (8 )
59
- }
60
- res := new_time (Time{
61
- year: iyear
62
- month: imonth
63
- day: iday
64
- hour: ihour
65
- minute: iminute
66
- second: isecond
67
- })
68
- return res
69
- }
70
-
71
6
// parse_rfc2822 returns time from a date string in RFC 2822 datetime format.
72
7
pub fn parse_rfc2822 (s string ) ? Time {
73
8
if s == '' {
@@ -87,56 +22,6 @@ pub fn parse_rfc2822(s string) ?Time {
87
22
}
88
23
}
89
24
90
- // parse_rfc3339 returns time from a date string in RFC 3339 datetime format.
91
- pub fn parse_rfc3339 (s string ) ? Time {
92
- if s == '' {
93
- return error_invalid_time (0 )
94
- }
95
- mut t := parse_iso8601 (s) or { Time{} }
96
- // If parse_iso8601 DID NOT result in default values (i.e. date was parsed correctly)
97
- if t != Time{} {
98
- return t
99
- }
100
-
101
- t_i := s.index ('T' ) or { - 1 }
102
- parts := if t_i != - 1 { [s[..t_i], s[t_i + 1 ..]] } else { s.split (' ' ) }
103
-
104
- // Check if s is date only
105
- if ! parts[0 ].contains_any (' Z' ) && parts[0 ].contains ('-' ) {
106
- year , month , day := parse_iso8601_date (s) ?
107
- t = new_time (Time{
108
- year: year
109
- month: month
110
- day: day
111
- })
112
- return t
113
- }
114
- // Check if s is time only
115
- if ! parts[0 ].contains ('-' ) && parts[0 ].contains (':' ) {
116
- mut hour_, mut minute_, mut second_, mut microsecond_, mut unix_offset, mut is_local_time := 0 , 0 , 0 , 0 , i64 (0 ), true
117
- hour_ , minute_ , second_ , microsecond_ , unix_offset , is_local_time = parse_iso8601_time (parts[0 ]) ?
118
- t = new_time (Time{
119
- hour: hour_
120
- minute: minute_
121
- second: second_
122
- microsecond: microsecond_
123
- })
124
- if is_local_time {
125
- return t // Time is already local time
126
- }
127
- mut unix_time := t.unix
128
- if unix_offset < 0 {
129
- unix_time - = (- unix_offset)
130
- } else if unix_offset > 0 {
131
- unix_time + = unix_offset
132
- }
133
- t = unix2 (i64 (unix_time), t.microsecond)
134
- return t
135
- }
136
-
137
- return error_invalid_time (9 )
138
- }
139
-
140
25
// ----- iso8601 -----
141
26
fn parse_iso8601_date (s string ) ? (int , int , int ) {
142
27
year , month , day , dummy := 0 , 0 , 0 , byte (0 )
@@ -190,44 +75,3 @@ fn parse_iso8601_time(s string) ?(int, int, int, int, i64, bool) {
190
75
}
191
76
return hour_, minute_, second_, microsecond_, unix_offset, is_local_time
192
77
}
193
-
194
- // parse_iso8601 parses rfc8601 time format yyyy-MM-ddTHH:mm:ss.dddddd+dd:dd as local time
195
- // the fraction part is difference in milli seconds and the last part is offset
196
- // from UTC time and can be both +/- HH:mm
197
- // remarks: not all iso8601 is supported
198
- // also checks and support for leapseconds should be added in future PR
199
- pub fn parse_iso8601 (s string ) ? Time {
200
- if s == '' {
201
- return error_invalid_time (0 )
202
- }
203
- t_i := s.index ('T' ) or { - 1 }
204
- parts := if t_i != - 1 { [s[..t_i], s[t_i + 1 ..]] } else { s.split (' ' ) }
205
- if ! (parts.len == 1 || parts.len == 2 ) {
206
- return error_invalid_time (12 )
207
- }
208
- year , month , day := parse_iso8601_date (parts[0 ]) ?
209
- mut hour_, mut minute_, mut second_, mut microsecond_, mut unix_offset, mut is_local_time := 0 , 0 , 0 , 0 , i64 (0 ), true
210
- if parts.len == 2 {
211
- hour_ , minute_ , second_ , microsecond_ , unix_offset , is_local_time = parse_iso8601_time (parts[1 ]) ?
212
- }
213
- mut t := new_time (Time{
214
- year: year
215
- month: month
216
- day: day
217
- hour: hour_
218
- minute: minute_
219
- second: second_
220
- microsecond: microsecond_
221
- })
222
- if is_local_time {
223
- return t // Time already local time
224
- }
225
- mut unix_time := t.unix
226
- if unix_offset < 0 {
227
- unix_time - = (- unix_offset)
228
- } else if unix_offset > 0 {
229
- unix_time + = unix_offset
230
- }
231
- t = unix2 (i64 (unix_time), t.microsecond)
232
- return t
233
- }
0 commit comments