Skip to content

Commit

Permalink
std/time: add the Nanosecond method to Time
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Nov 21, 2024
1 parent bca54d1 commit 4dac5b3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions std/time/time.jule
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const unixToAbsolute = 9223372028715321600
// equivalent to Wed Dec 31 1969 23:59:50 UTC+0000.
struct Time {
sec: i64
nsec: i64
nsec: i32 // In the range [0, 999999999].
}

impl Time {
Expand Down Expand Up @@ -72,6 +72,12 @@ impl Time {
ret int(self.abs()%secPerHour) / secPerMinute
}

// Returns the nanosecond offset within the second specified by the time,
// in the range [0, 999999999].
fn Nanosecond(self): int {
ret int(self.nsec)
}

// Returns the year, month, and day of the time.
fn Date(self): (year: int, month: int, day: int) {
year, month, day, _ = absDate(self.abs(), true)
Expand Down Expand Up @@ -175,7 +181,7 @@ fn absDate(abs: u64, full: bool): (year: int, month: int, day: int, yday: int) {
// Returns the current system time with UTC local.
fn Now(): Time {
sec, nsec := runtime::timeNow()
ret Time{sec: sec, nsec: nsec}
ret Time{sec: sec, nsec: i32(nsec)}
}

// Returns new time by Unix time with nanoseconds.
Expand All @@ -192,7 +198,7 @@ fn Unix(mut sec: i64, mut nsec: i64): Time {
sec--
}
}
ret Time{sec: sec, nsec: nsec}
ret Time{sec: sec, nsec: i32(nsec)}
}

// Absolute time.
Expand Down Expand Up @@ -341,6 +347,6 @@ fn absUnix(mut year: int, mut month: int, mut day: int,
fn Date(year: int, month: int, day: int,
hour: int, minute: int, second: int, nsecond: int): (t: Time) {
t.sec = absUnix(year, month, day, hour, minute, second, unsafe { *(&nsecond) })
t.nsec = i64(nsecond)
t.nsec = i32(nsecond)
ret t
}

0 comments on commit 4dac5b3

Please sign in to comment.