Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-std

Source of release 0.7.4

0.7.4 / src / clock / calendar.lucb

#==============================================================================================
#
#   calendar - Days and the local offset from UTC
#
#   DESCRIPTION:
#       The proleptic Gregorian calendar as day counts from 1970-01-01, and the offset of the
#       system's local time zone at an instant.
#
#==============================================================================================

## `struct tm` on macOS and Linux, through tm_gmtoff, the offset east of UTC.
extern struct CalendarTime:
    second: i32
    minute: i32
    hour: i32
    day: i32
    month: i32
    year: i32
    weekday: i32
    yearday: i32
    daylight: i32
    offset: c.long
    zone: void*?

## `struct tm` on Windows, which has no offset field.
extern struct WindowsCalendarTime:
    second: i32
    minute: i32
    hour: i32
    day: i32
    month: i32
    year: i32
    weekday: i32
    yearday: i32
    daylight: i32

extern func localtime_r(seconds: const i64*, result: CalendarTime*) -> CalendarTime*?
extern func windows_localtime as "_localtime64_s"(result: WindowsCalendarTime*, seconds: const i64*) -> i32
extern func windows_utc_seconds as "_mkgmtime64"(calendar: WindowsCalendarTime*) -> i64

## The local time zone's offset east of UTC, in seconds, at `seconds` since 1970 UTC.
func local_offset(seconds: i64) -> i32!:
    if platform.windows:
        var calendar: WindowsCalendarTime
        if windows_localtime(&calendar, &seconds) != 0:
            error(invalid, "the local time zone has no offset for this time")
        # the local fields read as UTC, less the instant itself
        return (i32)(windows_utc_seconds(&calendar) - seconds)
    else:
        var calendar: CalendarTime
        let found = localtime_r(&seconds, &calendar) else error(invalid, "the local time zone has no offset for this time")
        return (i32)found.offset

## Days from 1970-01-01 to the given date (Howard Hinnant's days_from_civil).
func days_from_civil(year: i64, month: i64, day: i64) -> i64:
    let y = year - 1 if month <= 2 else year
    let era = (y if y >= 0 else y - 399) // 400
    let year_of_era = y - era * 400
    let shifted = month + 9 if month <= 2 else month - 3
    let day_of_year = (153 * shifted + 2) // 5 + day - 1
    let day_of_era = year_of_era * 365 + year_of_era // 4 - year_of_era // 100 + day_of_year
    return era * 146097 + day_of_era - 719468

## The date `days` after 1970-01-01, as year, month and day (civil_from_days).
func civil_from_days(days: i64) -> (i64, i64, i64):
    let z = days + 719468
    let era = (z if z >= 0 else z - 146096) // 146097
    let day_of_era = z - era * 146097
    let year_of_era = (day_of_era - day_of_era // 1460 + day_of_era // 36524 - day_of_era // 146096) // 365
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era // 4 - year_of_era // 100)
    let shifted = (5 * day_of_year + 2) // 153
    let day = day_of_year - (153 * shifted + 2) // 5 + 1
    let month = shifted + 3 if shifted < 10 else shifted - 9
    let year = year_of_era + era * 400
    return (year + 1 if month <= 2 else year, month, day)

## The days in `month` of `year`.
func days_in_month(year: i64, month: i64) -> i64:
    if month == 2:
        let leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
        return 29 if leap else 28
    return 30 if month == 4 or month == 6 or month == 9 or month == 11 else 31

## `value // divisor`, rounded down also for a negative value.
func floor_divide(value: i64, divisor: i64) -> i64:
    let quotient = value // divisor
    return quotient - 1 if value % divisor < 0 else quotient

test "day counts and dates agree across eras, leap days and the epoch":
    assert(days_from_civil(1970, 1, 1) == 0)
    assert(days_from_civil(2000, 3, 1) == 11017)
    assert(civil_from_days(-1) == (1969, 12, 31))
    assert(civil_from_days(19723) == (2024, 1, 1))
    assert(civil_from_days(days_from_civil(2024, 2, 29)) == (2024, 2, 29))
    assert(days_from_civil(1, 1, 1) == -719162 and civil_from_days(2932896) == (9999, 12, 31))
    assert(days_in_month(1900, 2) == 28 and days_in_month(2000, 2) == 29 and days_in_month(2026, 9) == 30)
    assert(floor_divide(-1, 86400) == -1 and floor_divide(86400, 86400) == 1)