1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

time: add microsecond precision to Time struct

This commit is contained in:
Tomas Hellström
2020-06-07 15:19:09 +02:00
committed by GitHub
parent eec5cf1eb1
commit 9c8769503f
15 changed files with 832 additions and 17 deletions

View File

@ -24,6 +24,29 @@ pub fn unix(abs int) Time {
}
}
// unix2 returns a time struct from Unix time and microsecond value
pub fn unix2(abs int, microsecond int) Time {
// Split into day and time
mut day_offset := abs / seconds_per_day
if abs % seconds_per_day < 0 {
// Compensate for round towards zero on integers as we want floored instead
day_offset--
}
year,month,day := calculate_date_from_offset(day_offset)
hr,min,sec := calculate_time_from_offset(abs % seconds_per_day)
return Time{
year: year
month: month
day: day
hour: hr
minute: min
second: sec
microsecond: microsecond
unix: u64(abs)
}
}
[inline]
fn calculate_date_from_offset(day_offset_ int) (int,int,int) {
mut day_offset := day_offset_