From 95426d53f096258cdc0a971296c113c36b70b2cc Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Tue, 9 Apr 2024 13:05:14 +0200 Subject: [PATCH] time: cleanup module (#21217) --- vlib/time/time.c.v | 8 ++------ vlib/time/time.v | 9 ++------- vlib/time/unix.v | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/vlib/time/time.c.v b/vlib/time/time.c.v index bbec57f9127456..2bdb792bf2a5b3 100644 --- a/vlib/time/time.c.v +++ b/vlib/time/time.c.v @@ -11,15 +11,11 @@ pub struct C.timeval { tv_usec u64 } -fn C.localtime(t &C.time_t) &C.tm -fn C.localtime_r(t &C.time_t, tm &C.tm) - -// struct C.time_t {} - type C.time_t = i64 fn C.time(t &C.time_t) C.time_t - +fn C.localtime(t &C.time_t) &C.tm +fn C.localtime_r(t &C.time_t, tm &C.tm) fn C.gmtime(t &C.time_t) &C.tm fn C.gmtime_r(t &C.time_t, res &C.tm) &C.tm fn C.strftime(buf &char, maxsize usize, const_format &char, const_tm &C.tm) usize diff --git a/vlib/time/time.v b/vlib/time/time.v index bd6cbb962a2e9e..18851e2797f550 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -129,21 +129,16 @@ pub fn (t Time) add(duration_in_nanosecond Duration) Time { // This expression overflows i64 for big years (and we do not have i128 yet): // nanos := t.unix * 1_000_000_000 + i64(t.nanosecond) <- // ... so instead, handle the addition manually in parts ¯\_(ツ)_/¯ - mut increased_time_nanosecond := i64(t.nanosecond) + duration_in_nanosecond.nanoseconds() - // increased_time_second mut increased_time_second := t.unix + (increased_time_nanosecond / second) - increased_time_nanosecond = increased_time_nanosecond % second if increased_time_nanosecond < 0 { increased_time_second-- increased_time_nanosecond += second } - if t.is_local { - return unix_nanosecond(increased_time_second, int(increased_time_nanosecond)).as_local() - } - return unix_nanosecond(increased_time_second, int(increased_time_nanosecond)) + res := unix_nanosecond(increased_time_second, int(increased_time_nanosecond)) + return if t.is_local { res.as_local() } else { res } } // add_seconds returns a new time struct with an added number of seconds. diff --git a/vlib/time/unix.v b/vlib/time/unix.v index 29a1894a5872e1..1b0f9cff6aae44 100644 --- a/vlib/time/unix.v +++ b/vlib/time/unix.v @@ -3,16 +3,16 @@ // that can be found in the LICENSE file. module time -// unix returns a time struct from an Unix timestamp (number of seconds since 1970-01-01) -pub fn unix(abs i64) Time { +// unix returns a Time struct calculated from a Unix timestamp (number of seconds since 1970-01-01) +pub fn unix(epoch i64) Time { // Split into day and time - mut day_offset := abs / seconds_per_day - if abs % seconds_per_day < 0 { + mut day_offset := epoch / seconds_per_day + if epoch % seconds_per_day < 0 { // Compensate for round towards zero on integers as we want floored instead day_offset-- } year, month, day := calculate_date_from_day_offset(day_offset) - hr, min, sec := calculate_time_from_second_offset(abs % seconds_per_day) + hr, min, sec := calculate_time_from_second_offset(epoch % seconds_per_day) return Time{ year: year month: month @@ -20,20 +20,20 @@ pub fn unix(abs i64) Time { hour: hr minute: min second: sec - unix: abs + unix: epoch } } // unix2 returns a Time struct, given an Unix timestamp in seconds, and a microsecond value @[deprecated: 'use unix_microsecond(unix_ts, us) instead'] @[deprecated_after: '2023-09-05'] -pub fn unix2(abs i64, microsecond int) Time { - return unix_nanosecond(abs, microsecond * 1000) +pub fn unix2(epoch i64, microsecond int) Time { + return unix_nanosecond(epoch, microsecond * 1000) } // unix_microsecond returns a Time struct, given an Unix timestamp in seconds, and a microsecond value -pub fn unix_microsecond(abs i64, microsecond int) Time { - return unix_nanosecond(abs, microsecond * 1000) +pub fn unix_microsecond(epoch i64, microsecond int) Time { + return unix_nanosecond(epoch, microsecond * 1000) } // unix_nanosecond returns a Time struct given a Unix timestamp in seconds and a nanosecond value