time: rename to_local_time() to local(); time.offset()

pull/7572/head
Alexander Medvednikov 2020-12-26 02:10:47 +01:00
parent 7507403118
commit 13f16b4a82
5 changed files with 14 additions and 5 deletions

View File

@ -140,5 +140,5 @@ pub fn parse_iso8601(s string) ?Time {
}
t = unix2(int(unix_time), t.microsecond)
// Convert the time to local time
return t.to_local_time()
return t.local()
}

View File

@ -52,9 +52,9 @@ fn test_parse_iso8601() {
// Because the offset between local time and UTC is not constant in regions
// that use daylight saving times we need to calculate separete offsets for
// summer and winter
offset_summer := time.Duration(time.new_time(year: 2020, month: 6, day: 5, hour: 15).to_local_time() -
offset_summer := time.Duration(time.new_time(year: 2020, month: 6, day: 5, hour: 15).local() -
time.new_time(year: 2020, month: 6, day: 5, hour: 15))
offset_winter := time.Duration(time.new_time(year: 2020, month: 11, day: 5, hour: 15).to_local_time() -
offset_winter := time.Duration(time.new_time(year: 2020, month: 11, day: 5, hour: 15).local() -
time.new_time(year: 2020, month: 11, day: 5, hour: 15))
formats := [
'2020-06-05T15:38:06Z',

View File

@ -418,3 +418,10 @@ pub fn (d Duration) hours() f64 {
nsec := d % hour
return f64(hr) + f64(nsec) / (60 * 60 * 1e9)
}
// offset returns time zone UTC offset in seconds
pub fn offset() int {
t := now()
local := t.local()
return int(local.unix - t.unix)
}

View File

@ -18,13 +18,15 @@ struct C.tm {
fn C.timegm(&tm) time_t
// fn C.gmtime_r(&tm, &gbuf)
fn C.localtime_r(t &C.time_t, tm &C.tm)
fn make_unix_time(t C.tm) int {
return int(C.timegm(&t))
}
pub fn (t Time) to_local_time() Time {
// local returns t with the location set to local time.
pub fn (t Time) local() Time {
loc_tm := C.tm{}
C.localtime_r(time_t(&t.unix), &loc_tm)
return convert_ctime(loc_tm, t.microsecond)

View File

@ -93,7 +93,7 @@ fn local_as_unix_time() int {
return make_unix_time(tm)
}
pub fn (t Time) to_local_time() Time {
pub fn (t Time) local() Time {
st_utc := SystemTime{
year: u16(t.year)
month: u16(t.month)