-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add functions to decode an epoch timestamp #9040
Conversation
The code added here is alternative to the libc gmtime function. This function takes a unix epoch timestamp and decodes it into things like the year/day/time/etc. I looked at various libc implementations to see how it was implemented and this appears to be correct. I reorganized it so that applications can choose which data they need rather than calcualting it all in a single function. The data structures layout the order of operations required to decode various things like the year/month or time of day.
pub const YearAndDay = struct { | ||
year: Year, | ||
/// The number of days into the year (0 to 365) | ||
day: u9, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ISO 8601 says that day of the year is in range [1,365 (366)]
, maybe this should be too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure. I actually can't think of a use case for wanting the day of the year. For the calculation of the month/month-day, we need it to be 0 based, so if we make it 1 based, we'll just be adding 1 then undoing that operation in calculateMonthDay
. An alternative is also to create a method that will return this day value with 1 added. Not sure what the best route is here.
lib/std/time/epoch.zig
Outdated
pub const YearLeapKind = enum(u1) { not_leap, leap }; | ||
|
||
pub const Month = enum(u4) { | ||
jan, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jan, | |
jan = 1, |
And so forth. You can avoid the +1 dance and the numeric
helper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure that should work, done
This is possibly related to #8396 as it implements random chunks of a |
The code added here is alternative to the libc
gmtime
function. This function takes a unix epoch timestamp and decodes it into things like the year/month/day/time/etc. I looked at various libc implementations to see how it was implemented and this appears to be correct (the tests also match what I get on epochconverter.com).I split the single
gmtime
function into multiple functions based on the order in which the date information is decoded so that applications can limit their calculations to what they need rather than calculating everything in a single function. The data structures are based on the order that pieces of the timestamp are calculated.