v / vlib / time / chrono.c.v
17 lines · 16 sloc · 547 bytes · ae0937c83e982224c24e87ef34dcf5796a62b14c
Raw
1module time
2
3// portable_timegm does the same as C._mkgmtime, but unlike it, can work with dates before the Unix epoch of 1970-01-01 .
4pub fn portable_timegm(t &C.tm) i64 {
5 mut year := t.tm_year + 1900
6 mut month := t.tm_mon // 0-11
7 if month > 11 {
8 year += month / 12
9 month %= 12
10 } else if month < 0 {
11 years_diff := (11 - month) / 12
12 year -= years_diff
13 month += 12 * years_diff
14 }
15 days_since_1970 := i64(days_from_unix_epoch(year, month + 1, t.tm_mday))
16 return 60 * (60 * (24 * days_since_1970 + t.tm_hour) + t.tm_min) + t.tm_sec
17}
18