v

Зеркало из https://github.com/vlang/v
Форк
0
/
parse.c.v 
315 строк · 9.9 Кб
1
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2
// Use of this source code is governed by an MIT license
3
// that can be found in the LICENSE file.
4
module time
5

6
import strconv
7

8
// parse_rfc3339 returns the time from a date string in RFC 3339 datetime format.
9
// See also https://ijmacd.github.io/rfc3339-iso8601/ for a visual reference of
10
// the differences between ISO-8601 and RFC 3339.
11
pub fn parse_rfc3339(s string) !Time {
12
	if s == '' {
13
		return error_invalid_time(0, 'datetime string is empty')
14
	}
15
	// Normalize the input before parsing. Good since iso8601 doesn't permit lower case `t` and `z`.
16
	sn := s.replace_each(['t', 'T', 'z', 'Z'])
17
	mut t := parse_iso8601(sn) or { Time{} }
18
	// If parse_iso8601 DID NOT result in default values (i.e. date was parsed correctly)
19
	if t != Time{} {
20
		return t
21
	}
22

23
	t_i := sn.index('T') or { -1 }
24
	parts := if t_i != -1 { [sn[..t_i], sn[t_i + 1..]] } else { sn.split(' ') }
25

26
	// Check if sn is date only
27
	if !parts[0].contains_any(' Z') && parts[0].contains('-') {
28
		year, month, day := parse_iso8601_date(sn)!
29
		t = new(Time{
30
			year:  year
31
			month: month
32
			day:   day
33
		})
34
		return t
35
	}
36
	// Check if sn is time only
37
	if !parts[0].contains('-') && parts[0].contains(':') {
38
		mut hour_, mut minute_, mut second_, mut microsecond_, mut nanosecond_, mut unix_offset, mut is_local_time := 0, 0, 0, 0, 0, i64(0), true
39
		hour_, minute_, second_, microsecond_, nanosecond_, unix_offset, is_local_time = parse_iso8601_time(parts[0])!
40
		t = new(Time{
41
			hour:       hour_
42
			minute:     minute_
43
			second:     second_
44
			nanosecond: nanosecond_
45
		})
46
		if is_local_time {
47
			return t // Time is already local time
48
		}
49
		mut unix_time := t.unix
50
		if unix_offset < 0 {
51
			unix_time -= (-unix_offset)
52
		} else if unix_offset > 0 {
53
			unix_time += unix_offset
54
		}
55
		t = unix_nanosecond(i64(unix_time), t.nanosecond)
56
		return t
57
	}
58

59
	return error_invalid_time(9, 'malformed date')
60
}
61

62
// parse returns the time from a date string in "YYYY-MM-DD HH:mm:ss" format.
63
pub fn parse(s string) !Time {
64
	if s == '' {
65
		return error_invalid_time(0, 'datetime string is empty')
66
	}
67
	pos := s.index(' ') or {
68
		return error_invalid_time(1, 'string has no space between date and time')
69
	}
70
	symd := s[..pos]
71
	ymd := symd.split('-')
72
	if ymd.len != 3 {
73
		return error_invalid_time(2, 'date must be in the form of y-m-d')
74
	}
75
	shms := s[pos..]
76
	hms := shms.split(':')
77
	if hms.len != 3 {
78
		return error_invalid_time(9, 'time must be in the form of H:i:s')
79
	}
80
	hour_ := hms[0][1..]
81
	minute_ := hms[1]
82
	second_ := hms[2]
83

84
	iyear := strconv.atoi(ymd[0]) or {
85
		return error_invalid_time(0, 'invalid year format: ${ymd[0]}')
86
	}
87
	imonth := strconv.atoi(ymd[1]) or {
88
		return error_invalid_time(0, 'invalid month format: ${ymd[1]}')
89
	}
90
	iday := strconv.atoi(ymd[2]) or {
91
		return error_invalid_time(0, 'invalid day format: ${ymd[2]}')
92
	}
93
	ihour := strconv.atoi(hour_) or {
94
		return error_invalid_time(0, 'invalid hour format: ${hour_}')
95
	}
96
	iminute := strconv.atoi(minute_) or {
97
		return error_invalid_time(0, 'invalid minute format: ${minute_}')
98
	}
99
	isecond := strconv.atoi(second_) or {
100
		return error_invalid_time(0, 'invalid second format: ${second_}')
101
	}
102

103
	// eprintln('>> iyear: $iyear | imonth: $imonth | iday: $iday | ihour: $ihour | iminute: $iminute | isecond: $isecond')
104
	if iyear > 9999 || iyear < -9999 {
105
		return error_invalid_time(3, 'year must be between -10000 and 10000')
106
	}
107
	if imonth > 12 || imonth < 1 {
108
		return error_invalid_time(4, 'month must be between 1 and 12')
109
	}
110
	if iday > 31 || iday < 1 {
111
		return error_invalid_time(5, 'day must be between 1 and 31')
112
	}
113
	if ihour > 23 || ihour < 0 {
114
		return error_invalid_time(6, 'hours must be between 0 and 24')
115
	}
116
	if iminute > 59 || iminute < 0 {
117
		return error_invalid_time(7, 'minutes must be between 0 and 60')
118
	}
119
	if isecond > 59 || isecond < 0 {
120
		return error_invalid_time(8, 'seconds must be between 0 and 60')
121
	}
122
	res := new(Time{
123
		year:   iyear
124
		month:  imonth
125
		day:    iday
126
		hour:   ihour
127
		minute: iminute
128
		second: isecond
129
	})
130
	return res
131
}
132

133
// parse_format parses the string `s`, as a custom `format`, containing the following specifiers:
134
//
135
// |Category| Format | Description |
136
// |:-----  | :----- | :---------- |
137
// |Year    | YYYY   | 4 digit year, 0000..9999 |
138
// |        | YY     | 2 digit year, 00..99 |
139
// |Month   | M      | month, 1..12 |
140
// |        | MM     | month, 2 digits, 01..12 |
141
// |        | MMM    | month, three letters, Jan..Dec |
142
// |        | MMMM   | name of month |
143
// |Day     | D      | day of the month, 1..31 |
144
// |        | DD     | day of the month, 01..31 |
145
// |        | d      | day of week, 0..6 |
146
// |        | c      | day of week, 1..7 |
147
// |        | dd     | day of week, Su..Sa |
148
// |        | ddd    | day of week, Sun..Sat |
149
// |        | dddd   | day of week, Sunday..Saturday |
150
// |Hour    | H      | hour, 0..23 |
151
// |        | HH     | hour, 00..23 |
152
// |        | h      | hour, 0..23 |
153
// |        | hh     | hour, 0..23 |
154
// |        | k      | hour, 0..23 |
155
// |        | kk     | hour, 0..23 |
156
// |Minute  | m      | minute, 0..59 |
157
// |        | mm     | minute, 0..59 |
158
// |Second  | s      | second, 0..59 |
159
// |        | ss     | second, 0..59 |
160
pub fn parse_format(s string, format string) !Time {
161
	if s == '' {
162
		return error_invalid_time(0, 'datetime string is empty')
163
	}
164
	mut p := new_date_time_parser(s, format)
165
	return p.parse()
166
}
167

168
// parse_iso8601 parses the ISO 8601 time format yyyy-MM-ddTHH:mm:ss.dddddd+dd:dd as local time.
169
// The fraction part is difference in milli seconds, and the last part is offset from UTC time.
170
// Both can be +/- HH:mm .
171
// See https://en.wikipedia.org/wiki/ISO_8601 .
172
// Remarks: not all of ISO 8601 is supported; checks and support for leapseconds should be added.
173
pub fn parse_iso8601(s string) !Time {
174
	if s == '' {
175
		return error_invalid_time(0, 'datetime string is empty')
176
	}
177
	t_i := s.index('T') or { -1 }
178
	parts := if t_i != -1 { [s[..t_i], s[t_i + 1..]] } else { s.split(' ') }
179
	if !(parts.len == 1 || parts.len == 2) {
180
		return error_invalid_time(12, 'malformed date')
181
	}
182
	year, month, day := parse_iso8601_date(parts[0])!
183
	mut hour_, mut minute_, mut second_, mut microsecond_, mut nanosecond_, mut unix_offset, mut is_local_time := 0, 0, 0, 0, 0, i64(0), true
184
	if parts.len == 2 {
185
		hour_, minute_, second_, microsecond_, nanosecond_, unix_offset, is_local_time = parse_iso8601_time(parts[1])!
186
	}
187
	mut t := new(
188
		year:       year
189
		month:      month
190
		day:        day
191
		hour:       hour_
192
		minute:     minute_
193
		second:     second_
194
		nanosecond: nanosecond_
195
	)
196
	if is_local_time {
197
		return t // Time already local time
198
	}
199
	mut unix_time := t.unix
200
	if unix_offset < 0 {
201
		unix_time -= (-unix_offset)
202
	} else if unix_offset > 0 {
203
		unix_time += unix_offset
204
	}
205
	t = unix_nanosecond(i64(unix_time), t.nanosecond)
206
	return t
207
}
208

209
// parse_rfc2822 returns the time from a date string in RFC 2822 datetime format.
210
pub fn parse_rfc2822(s string) !Time {
211
	if s == '' {
212
		return error_invalid_time(0, 'datetime string is empty')
213
	}
214
	fields := s.split(' ')
215
	if fields.len < 5 {
216
		return error_invalid_time(1, 'datetime string must have 5 components, has: ${fields.len}')
217
	}
218
	pos := months_string.index(fields[2]) or {
219
		return error_invalid_time(2, 'invalid month format')
220
	}
221
	mm := pos / 3 + 1
222
	unsafe {
223
		tmstr := malloc_noscan(s.len * 2)
224
		count := C.snprintf(&char(tmstr), (s.len * 2), c'%s-%02d-%s %s', fields[3].str,
225
			mm, fields[1].str, fields[4].str)
226
		return parse(tos(tmstr, count))
227
	}
228
}
229

230
// ----- iso8601 -----
231
fn parse_iso8601_date(s string) !(int, int, int) {
232
	year, month, day, dummy := 0, 0, 0, u8(0)
233
	count := unsafe { C.sscanf(&char(s.str), c'%4d-%2d-%2d%c', &year, &month, &day, &dummy) }
234
	if count != 3 {
235
		return error_invalid_time(10, 'datetime string must have 3 components, but has ${count}')
236
	}
237
	if year > 9999 {
238
		return error_invalid_time(13, 'year must be smaller than 10000')
239
	}
240
	if month > 12 {
241
		return error_invalid_time(14, 'month must be smaller than 12')
242
	}
243
	if day > 31 {
244
		return error_invalid_time(15, 'day must be smaller than 31')
245
	}
246
	return year, month, day
247
}
248

249
fn parse_iso8601_time(s string) !(int, int, int, int, int, i64, bool) {
250
	hour_ := 0
251
	minute_ := 0
252
	second_ := 0
253
	mut microsecond_ := 0
254
	mut nanosecond_ := 0
255
	plus_min_z := `a`
256
	offset_hour := 0
257
	offset_minute := 0
258
	mut count := 0
259
	count = unsafe {
260
		C.sscanf(&char(s.str), c'%2d:%2d:%2d.%9d%c', &hour_, &minute_, &second_, &nanosecond_,
261
			&char(&plus_min_z))
262
	}
263
	if count == 5 && plus_min_z == `Z` {
264
		// normalise the nanoseconds:
265
		mut ndigits := 0
266
		if mut pos := s.index('.') {
267
			pos++
268
			for ; pos < s.len && s[pos].is_digit(); pos++ {
269
				ndigits++
270
			}
271
		}
272
		for ndigits < 9 {
273
			nanosecond_ *= 10
274
			ndigits++
275
		}
276
		microsecond_ = nanosecond_ / 1000
277
	} else {
278
		count = unsafe {
279
			C.sscanf(&char(s.str), c'%2d:%2d:%2d.%9d%c%2d:%2d', &hour_, &minute_, &second_,
280
				&microsecond_, &char(&plus_min_z), &offset_hour, &offset_minute)
281
		}
282
		// Missread microsecond ([Sec Hour Minute].len == 3 < 4)
283
		if count < 4 {
284
			count = unsafe {
285
				C.sscanf(&char(s.str), c'%2d:%2d:%2d%c%2d:%2d', &hour_, &minute_, &second_,
286
					&char(&plus_min_z), &offset_hour, &offset_minute)
287
			}
288
			count++ // Increment count because skipped microsecond
289
		}
290
		if count < 4 {
291
			return error_invalid_time(10, 'malformed date')
292
		}
293
		nanosecond_ = microsecond_ * 1000
294
	}
295
	is_local_time := plus_min_z == `a` && count == 4
296
	is_utc := plus_min_z == `Z` && count == 5
297
	if !(count == 7 || is_local_time || is_utc) {
298
		return error_invalid_time(11, 'malformed date')
299
	}
300
	if plus_min_z != `+` && plus_min_z != `-` && !is_utc && !is_local_time {
301
		return error_invalid_time(12, 'missing timezone')
302
	}
303
	mut unix_offset := 0
304
	if offset_hour > 0 {
305
		unix_offset += 3600 * offset_hour
306
	}
307
	if offset_minute > 0 {
308
		unix_offset += 60 * offset_minute
309
	}
310
	if plus_min_z == `+` {
311
		unix_offset *= -1
312
	}
313
	// eprintln('parse_iso8601_time s: $s | hour_: $hour_ | minute_: $minute_ | second_: $second_ | microsecond_: $microsecond_ | nanosecond_: $nanosecond_ | unix_offset: $unix_offset | is_local_time: $is_local_time')
314
	return hour_, minute_, second_, microsecond_, nanosecond_, unix_offset, is_local_time
315
}
316

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.