mirror of
https://github.com/vlang/v.git
synced 2025-09-09 23:39:39 -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
|
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
|
type C.time_t = i64
|
||||||
|
|
||||||
fn C.time(t &C.time_t) C.time_t
|
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(t &C.time_t) &C.tm
|
||||||
fn C.gmtime_r(t &C.time_t, res &C.tm) &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
|
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):
|
// This expression overflows i64 for big years (and we do not have i128 yet):
|
||||||
// nanos := t.unix * 1_000_000_000 + i64(t.nanosecond) <-
|
// nanos := t.unix * 1_000_000_000 + i64(t.nanosecond) <-
|
||||||
// ... so instead, handle the addition manually in parts ¯\_(ツ)_/¯
|
// ... so instead, handle the addition manually in parts ¯\_(ツ)_/¯
|
||||||
|
|
||||||
mut increased_time_nanosecond := i64(t.nanosecond) + duration_in_nanosecond.nanoseconds()
|
mut increased_time_nanosecond := i64(t.nanosecond) + duration_in_nanosecond.nanoseconds()
|
||||||
|
|
||||||
// increased_time_second
|
// increased_time_second
|
||||||
mut increased_time_second := t.unix + (increased_time_nanosecond / second)
|
mut increased_time_second := t.unix + (increased_time_nanosecond / second)
|
||||||
|
|
||||||
increased_time_nanosecond = increased_time_nanosecond % second
|
increased_time_nanosecond = increased_time_nanosecond % second
|
||||||
if increased_time_nanosecond < 0 {
|
if increased_time_nanosecond < 0 {
|
||||||
increased_time_second--
|
increased_time_second--
|
||||||
increased_time_nanosecond += second
|
increased_time_nanosecond += second
|
||||||
}
|
}
|
||||||
if t.is_local {
|
res := unix_nanosecond(increased_time_second, int(increased_time_nanosecond))
|
||||||
return unix_nanosecond(increased_time_second, int(increased_time_nanosecond)).as_local()
|
return if t.is_local { res.as_local() } else { res }
|
||||||
}
|
|
||||||
return unix_nanosecond(increased_time_second, int(increased_time_nanosecond))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add_seconds returns a new time struct with an added number of seconds.
|
// 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.
|
// that can be found in the LICENSE file.
|
||||||
module time
|
module time
|
||||||
|
|
||||||
// unix returns a time struct from an Unix timestamp (number of seconds since 1970-01-01)
|
// unix returns a Time struct calculated from a Unix timestamp (number of seconds since 1970-01-01)
|
||||||
pub fn unix(abs i64) Time {
|
pub fn unix(epoch i64) Time {
|
||||||
// Split into day and time
|
// Split into day and time
|
||||||
mut day_offset := abs / seconds_per_day
|
mut day_offset := epoch / seconds_per_day
|
||||||
if abs % seconds_per_day < 0 {
|
if epoch % seconds_per_day < 0 {
|
||||||
// Compensate for round towards zero on integers as we want floored instead
|
// Compensate for round towards zero on integers as we want floored instead
|
||||||
day_offset--
|
day_offset--
|
||||||
}
|
}
|
||||||
year, month, day := calculate_date_from_day_offset(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{
|
return Time{
|
||||||
year: year
|
year: year
|
||||||
month: month
|
month: month
|
||||||
@ -20,20 +20,20 @@ pub fn unix(abs i64) Time {
|
|||||||
hour: hr
|
hour: hr
|
||||||
minute: min
|
minute: min
|
||||||
second: sec
|
second: sec
|
||||||
unix: abs
|
unix: epoch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// unix2 returns a Time struct, given an Unix timestamp in seconds, and a microsecond value
|
// unix2 returns a Time struct, given an Unix timestamp in seconds, and a microsecond value
|
||||||
@[deprecated: 'use unix_microsecond(unix_ts, us) instead']
|
@[deprecated: 'use unix_microsecond(unix_ts, us) instead']
|
||||||
@[deprecated_after: '2023-09-05']
|
@[deprecated_after: '2023-09-05']
|
||||||
pub fn unix2(abs i64, microsecond int) Time {
|
pub fn unix2(epoch i64, microsecond int) Time {
|
||||||
return unix_nanosecond(abs, microsecond * 1000)
|
return unix_nanosecond(epoch, microsecond * 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
// unix_microsecond returns a Time struct, given an Unix timestamp in seconds, and a microsecond value
|
// 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 {
|
pub fn unix_microsecond(epoch i64, microsecond int) Time {
|
||||||
return unix_nanosecond(abs, microsecond * 1000)
|
return unix_nanosecond(epoch, microsecond * 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
// unix_nanosecond returns a Time struct given a Unix timestamp in seconds and a nanosecond value
|
// 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