跳转到内容
标准库Zig 0.17.0官方原文
源码std/time/epoch.zig

第 1 行

纪元参考时间,以相对于 UTC 1970-01-01 的秒数差表示。

std.time.epoch.amiga

常量

第 20 行

公元 1978 年 1 月 1 日

std.time.epoch.android

别名

第 29 行

别名目标 std.time.epoch.posix

std.time.epoch.aros

别名

第 36 行

别名目标 std.time.epoch.amiga

std.time.epoch.atsc

别名

第 39 行

别名目标 std.time.epoch.gps

std.time.epoch.bios

别名

第 31 行

别名目标 std.time.epoch.dos

std.time.epoch.brew

别名

第 38 行

别名目标 std.time.epoch.gps

std.time.epoch.clr

常量

第 26 行

公元 0001 年 1 月 1 日

std.time.epoch.DaySeconds

容器

第 151 行

自当天开始经过的秒数

字段

secs: u17

std.time.epoch.DaySeconds.getHoursIntoDay

函数

第 155 行

自当天起始时刻经过的小时数(0 到 23)

pub fn getHoursIntoDay(self: DaySeconds) u5

参数

self: DaySeconds

std.time.epoch.DaySeconds.getMinutesIntoHour

函数

第 159 行

当前小时内的分钟数(0 到 59)

pub fn getMinutesIntoHour(self: DaySeconds) u6

参数

self: DaySeconds

std.time.epoch.DaySeconds.getSecondsIntoMinute

函数

第 163 行

当前分钟内经过的秒数(0 到 59)

pub fn getSecondsIntoMinute(self: DaySeconds) u6

参数

self: DaySeconds

std.time.epoch.dos

常量

第 10 行

公元 1980 年 1 月 1 日

std.time.epoch.epoch_year

常量

第 45 行

std.time.epoch.EpochDay

容器

第 134 行

自纪元 1970 年 1 月 1 日以来的天数

字段

day: u47

std.time.epoch.EpochDay.calculateYearDay

函数

第 136 行

pub fn calculateYearDay(self: EpochDay) YearAndDay

参数

self: EpochDay

std.time.epoch.EpochSeconds

容器

第 169 行

自纪元 1970 年 1 月 1 日午夜起的秒数

字段

secs: u64

std.time.epoch.EpochSeconds.getDaySeconds

函数

第 180 行

以 DaySeconds 返回一天中已过的秒数。 使用 DaySeconds 获取时间信息。

pub fn getDaySeconds(self: EpochSeconds) DaySeconds

参数

self: EpochSeconds

std.time.epoch.EpochSeconds.getEpochDay

函数

第 174 行

以 EpochDay 返回自纪元以来的天数。 使用 EpochDay 获取此时间所在日期的信息。

pub fn getEpochDay(self: EpochSeconds) EpochDay

参数

self: EpochSeconds

std.time.epoch.getDaysInMonth

函数

第 89 行

获取给定年份及月份的天数

pub fn getDaysInMonth(year: Year, month: Month) u5

参数

year: Year
month: Month

std.time.epoch.getDaysInYear

函数

第 63 行

pub fn getDaysInYear(year: Year) u9

参数

year: Year

std.time.epoch.go

别名

第 40 行

别名目标 std.time.epoch.clr

std.time.epoch.gps

常量

第 24 行

公元 1980 年 1 月 6 日

std.time.epoch.ios

常量

第 12 行

公元 2001 年 1 月 1 日

std.time.epoch.isLeapYear

函数

第 48 行

pub fn isLeapYear(year: Year) bool

参数

year: Year

用法示例

test isLeapYear {
    try testing.expectEqual(false, isLeapYear(2095));
    try testing.expectEqual(true, isLeapYear(2096));
    try testing.expectEqual(false, isLeapYear(2100));
    try testing.expectEqual(true, isLeapYear(2400));
}

std.time.epoch.jbase

别名

第 35 行

别名目标 std.time.epoch.pickos

std.time.epoch.math

别名

第 5 行

别名目标 std.math

std.time.epoch.Month

容器

第 67 行

字段

jan = 1
feb
mar
apr
may
jun
jul
aug
sep
oct
nov
dec

std.time.epoch.Month.numeric

函数

第 83 行

返回给定月份的日历数值 即 jan=1、feb=2 等

pub fn numeric(self: Month) u4

参数

self: Month

std.time.epoch.MonthAndDay

容器

第 128 行

字段

month: Month
day_index: u5

std.time.epoch.morphos

别名

第 37 行

别名目标 std.time.epoch.amiga

std.time.epoch.ntfs

别名

第 33 行

别名目标 std.time.epoch.windows

std.time.epoch.ntp

别名

第 34 行

别名目标 std.time.epoch.zos

std.time.epoch.openvms

常量

第 14 行

公元 1858 年 11 月 17 日

std.time.epoch.os2

别名

第 30 行

别名目标 std.time.epoch.dos

std.time.epoch.pickos

常量

第 22 行

公元 1967 年 12 月 31 日

std.time.epoch.posix

常量

第 8 行

公元 1970 年 1 月 1 日

std.time.epoch.secs_per_day

常量

第 46 行

u17

std.time.epoch.std

别名

第 3 行

别名目标 std

std.time.epoch.testEpoch

函数

第 185 行

fn testEpoch(secs: u64, expected_year_day: YearAndDay, expected_month_day: MonthAndDay, expected_day_seconds: struct { hours_into_day: u5, minutes_into_hour: u6, seconds_into_minute: u6, }) !void

参数

secs: u64
expected_year_day: YearAndDay
expected_month_day: MonthAndDay
expected_day_seconds: struct {
    /// 0 至 23
    hours_into_day: u5,
    /// 0 到 59
    minutes_into_hour: u6,
    /// 0 到 59
    seconds_into_minute: u6,
}

std.time.epoch.testing

别名

第 4 行

别名目标 std.testing

std.time.epoch.unix

别名

第 28 行

别名目标 std.time.epoch.posix

std.time.epoch.vfat

别名

第 32 行

别名目标 std.time.epoch.dos

std.time.epoch.windows

常量

第 18 行

公元 1601 年 1 月 1 日

std.time.epoch.Year

类型

第 43 行

保存当前年份的类型,例如 2016。

std.time.epoch.YearAndDay

容器

第 109 行

字段

year: Year
day: u9

一年中已过的天数(0 至 365)

std.time.epoch.YearAndDay.calculateMonthDay

函数

第 114 行

pub fn calculateMonthDay(self: YearAndDay) MonthAndDay

参数

self: YearAndDay

std.time.epoch.zos

常量

第 16 行

公元 1900 年 1 月 1 日

源码

std/time/epoch.zig

//! 纪元参考时间,以相对于
//! UTC 1970-01-01 的秒数差表示。
const std = @import("../std.zig");
const testing = std.testing;
const math = std.math;

/// 公元 1970 年 1 月 1 日
pub const posix = 0;
/// 公元 1980 年 1 月 1 日
pub const dos = 315532800;
/// 公元 2001 年 1 月 1 日
pub const ios = 978307200;
/// 公元 1858 年 11 月 17 日
pub const openvms = -3506716800;
/// 公元 1900 年 1 月 1 日
pub const zos = -2208988800;
/// 公元 1601 年 1 月 1 日
pub const windows = -11644473600;
/// 公元 1978 年 1 月 1 日
pub const amiga = 252460800;
/// 公元 1967 年 12 月 31 日
pub const pickos = -63244800;
/// 公元 1980 年 1 月 6 日
pub const gps = 315964800;
/// 公元 0001 年 1 月 1 日
pub const clr = -62135769600;

pub const unix = posix;
pub const android = posix;
pub const os2 = dos;
pub const bios = dos;
pub const vfat = dos;
pub const ntfs = windows;
pub const ntp = zos;
pub const jbase = pickos;
pub const aros = amiga;
pub const morphos = amiga;
pub const brew = gps;
pub const atsc = gps;
pub const go = clr;

/// 保存当前年份的类型,例如 2016。
pub const Year = u16;

pub const epoch_year = 1970;
pub const secs_per_day: u17 = 24 * 60 * 60;

pub fn isLeapYear(year: Year) bool {
    if (@mod(year, 4) != 0)
        return false;
    if (@mod(year, 100) != 0)
        return true;
    return (0 == @mod(year, 400));
}

test isLeapYear {
    try testing.expectEqual(false, isLeapYear(2095));
    try testing.expectEqual(true, isLeapYear(2096));
    try testing.expectEqual(false, isLeapYear(2100));
    try testing.expectEqual(true, isLeapYear(2400));
}

pub fn getDaysInYear(year: Year) u9 {
    return if (isLeapYear(year)) 366 else 365;
}

pub const Month = enum(u4) {
    jan = 1,
    feb,
    mar,
    apr,
    may,
    jun,
    jul,
    aug,
    sep,
    oct,
    nov,
    dec,

    /// 返回给定月份的日历数值
    /// 即 jan=1、feb=2 等
    pub fn numeric(self: Month) u4 {
        return @backingInt(self);
    }
};

/// 获取给定年份及月份的天数
pub fn getDaysInMonth(year: Year, month: Month) u5 {
    return switch (month) {
        .jan => 31,
        .feb => switch (isLeapYear(year)) {
            true => 29,
            false => 28,
        },
        .mar => 31,
        .apr => 30,
        .may => 31,
        .jun => 30,
        .jul => 31,
        .aug => 31,
        .sep => 30,
        .oct => 31,
        .nov => 30,
        .dec => 31,
    };
}

pub const YearAndDay = struct {
    year: Year,
    /// 一年中已过的天数(0 至 365)
    day: u9,

    pub fn calculateMonthDay(self: YearAndDay) MonthAndDay {
        var month: Month = .jan;
        var days_left = self.day;
        while (true) {
            const days_in_month = getDaysInMonth(self.year, month);
            if (days_left < days_in_month)
                break;
            days_left -= days_in_month;
            month = @as(Month, @fromBackingInt(@intCast(@backingInt(month) + 1)));
        }
        return .{ .month = month, .day_index = @as(u5, @intCast(days_left)) };
    }
};

pub const MonthAndDay = struct {
    month: Month,
    day_index: u5, // 月内经过的天数(0 至 30)
};

/// 自纪元 1970 年 1 月 1 日以来的天数
pub const EpochDay = struct {
    day: u47, // u47 = u64 - u17(因为 day = sec(u64) / secs_per_day(u17)
    pub fn calculateYearDay(self: EpochDay) YearAndDay {
        var year_day = self.day;
        var year: Year = epoch_year;
        while (true) {
            const year_size = getDaysInYear(year);
            if (year_day < year_size)
                break;
            year_day -= year_size;
            year += 1;
        }
        return .{ .year = year, .day = @as(u9, @intCast(year_day)) };
    }
};

/// 自当天开始经过的秒数
pub const DaySeconds = struct {
    secs: u17, // 最大值为 24*60*60 = 86400

    /// 自当天起始时刻经过的小时数(0 到 23)
    pub fn getHoursIntoDay(self: DaySeconds) u5 {
        return @as(u5, @intCast(@divTrunc(self.secs, 3600)));
    }
    /// 当前小时内的分钟数(0 到 59)
    pub fn getMinutesIntoHour(self: DaySeconds) u6 {
        return @as(u6, @intCast(@divTrunc(@mod(self.secs, 3600), 60)));
    }
    /// 当前分钟内经过的秒数(0 到 59)
    pub fn getSecondsIntoMinute(self: DaySeconds) u6 {
        return math.comptimeMod(self.secs, 60);
    }
};

/// 自纪元 1970 年 1 月 1 日午夜起的秒数
pub const EpochSeconds = struct {
    secs: u64,

    /// 以 EpochDay 返回自纪元以来的天数。
    /// 使用 EpochDay 获取此时间所在日期的信息。
    pub fn getEpochDay(self: EpochSeconds) EpochDay {
        return EpochDay{ .day = @as(u47, @intCast(@divTrunc(self.secs, secs_per_day))) };
    }

    /// 以 DaySeconds 返回一天中已过的秒数。
    /// 使用 DaySeconds 获取时间信息。
    pub fn getDaySeconds(self: EpochSeconds) DaySeconds {
        return DaySeconds{ .secs = math.comptimeMod(self.secs, secs_per_day) };
    }
};

fn testEpoch(secs: u64, expected_year_day: YearAndDay, expected_month_day: MonthAndDay, expected_day_seconds: struct {
    /// 0 至 23
    hours_into_day: u5,
    /// 0 到 59
    minutes_into_hour: u6,
    /// 0 到 59
    seconds_into_minute: u6,
}) !void {
    const epoch_seconds = EpochSeconds{ .secs = secs };
    const epoch_day = epoch_seconds.getEpochDay();
    const day_seconds = epoch_seconds.getDaySeconds();
    const year_day = epoch_day.calculateYearDay();
    try testing.expectEqual(expected_year_day, year_day);
    try testing.expectEqual(expected_month_day, year_day.calculateMonthDay());
    try testing.expectEqual(expected_day_seconds.hours_into_day, day_seconds.getHoursIntoDay());
    try testing.expectEqual(expected_day_seconds.minutes_into_hour, day_seconds.getMinutesIntoHour());
    try testing.expectEqual(expected_day_seconds.seconds_into_minute, day_seconds.getSecondsIntoMinute());
}

test "epoch decoding" {
    try testEpoch(0, .{ .year = 1970, .day = 0 }, .{
        .month = .jan,
        .day_index = 0,
    }, .{ .hours_into_day = 0, .minutes_into_hour = 0, .seconds_into_minute = 0 });

    try testEpoch(31535999, .{ .year = 1970, .day = 364 }, .{
        .month = .dec,
        .day_index = 30,
    }, .{ .hours_into_day = 23, .minutes_into_hour = 59, .seconds_into_minute = 59 });

    try testEpoch(1622924906, .{ .year = 2021, .day = 31 + 28 + 31 + 30 + 31 + 4 }, .{
        .month = .jun,
        .day_index = 4,
    }, .{ .hours_into_day = 20, .minutes_into_hour = 28, .seconds_into_minute = 26 });

    try testEpoch(1625159473, .{ .year = 2021, .day = 31 + 28 + 31 + 30 + 31 + 30 }, .{
        .month = .jul,
        .day_index = 0,
    }, .{ .hours_into_day = 17, .minutes_into_hour = 11, .seconds_into_minute = 13 });
}