mirror of
https://github.com/vlang/v.git
synced 2025-09-09 15:27:05 -04:00
time: cleanup module (#21217)
This commit is contained in:
parent
cca426f69e
commit
95426d53f0
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user