Skip to content

Commit

Permalink
time: cleanup module (#21217)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Apr 9, 2024
1 parent cca426f commit 95426d5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
8 changes: 2 additions & 6 deletions vlib/time/time.c.v
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions vlib/time/time.v
Expand Up @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions vlib/time/unix.v
Expand Up @@ -3,37 +3,37 @@
// 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
day: day
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
Expand Down

0 comments on commit 95426d5

Please sign in to comment.