Skip to content

Commit

Permalink
std/time: add the Weekday type alias and relevant constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Nov 21, 2024
1 parent cd2bad9 commit 1e99c2f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions std/time/time.jule
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ const October = Month(10)
const November = Month(11)
const December = Month(12)

// Specifies a day of the week (Sunday = 0, ...).
type Weekday: int

const Sunday = Weekday(0)
const Monday = Weekday(1)
const Tuesday = Weekday(2)
const Wednesday = Weekday(3)
const Thursday = Weekday(4)
const Friday = Weekday(5)
const Saturday = Weekday(6)

// A Time represents an instant in time with nanosecond precision.
//
// Zero-value indicates the beginning of Unix time, i.e. zero seconds.
Expand Down Expand Up @@ -74,7 +85,7 @@ impl Time {
}

// Returns the day of the week specified by the time.
fn Weekday(self): int {
fn Weekday(self): Weekday {
ret absWeekday(self.abs())
}

Expand Down Expand Up @@ -120,10 +131,10 @@ fn absClock(abs: u64): (hour: int, minute: int, second: int) {
ret
}

fn absWeekday(abs: u64): int {
fn absWeekday(abs: u64): Weekday {
// January 1 of the absolute year, like January 1 of 2001, was a Monday.
sec := (abs + 1*secPerDay) % secPerWeek
ret int(sec) / secPerDay
sec := (abs + u64(Monday)*secPerDay) % secPerWeek
ret Weekday(int(sec) / secPerDay)
}

fn absDate(abs: u64, full: bool): (year: int, month: Month, day: int, yday: int) {
Expand Down Expand Up @@ -225,7 +236,7 @@ fn Unix(mut sec: i64, mut nsec: i64): Time {
// Absolute time.
struct AbsTime {
Day: int
Weekday: int
Weekday: Weekday
YearDay: int
Month: Month
Year: int
Expand Down

0 comments on commit 1e99c2f

Please sign in to comment.