v2 / vlib / time / parse.js.v
29 lines · 26 sloc · 1.02 KB · a74f3171db77de969608c57d7c7bf51733d79f45
Raw
1module time
2
3// parse returns the time from a date string.
4//
5// TODO(playX): JS Date expects iso8061 format of strings and other formats
6// are implementation dependent, we probably want to implement parsing in JS.
7pub fn parse(s string) Time {
8 mut res := Time{}
9 #let date = new Date(s.str)
10 #res.year.val = date.getFullYear()
11 #res.month.val = date.getMonth()
12 #res.day.val = date.getDay()
13 #res.hour.val = date.getHours()
14 #res.minute.val = date.getMinutes()
15 #res.second.val = date.getSeconds()
16 #res.microsecond.val = date.getMilliseconds() * 1000
17 #res.unix.val = (date.getTime() / 1000).toFixed(0)
18
19 return res
20}
21
22// parse_iso8601 parses the ISO 8601 time format yyyy-MM-ddTHH:mm:ss.dddddd+dd:dd as local time.
23// The fraction part is difference in milli seconds, and the last part is offset from UTC time.
24// Both can be +/- HH:mm .
25// See https://en.wikipedia.org/wiki/ISO_8601 .
26// Remarks: not all of ISO 8601 is supported; checks and support for leapseconds should be added.
27pub fn parse_iso8601(s string) !Time {
28 return parse(s)
29}
30