Skip to content
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

Merged
merged 4 commits into from
Sep 14, 2021
Merged

Conversation

marler8997
Copy link
Contributor

@marler8997 marler8997 commented Jun 8, 2021

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.

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,
Copy link
Member

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?

Copy link
Contributor Author

@marler8997 marler8997 Jun 8, 2021

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.

pub const YearLeapKind = enum(u1) { not_leap, leap };

pub const Month = enum(u4) {
jan,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
jan,
jan = 1,

And so forth. You can avoid the +1 dance and the numeric helper.

Copy link
Contributor Author

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

@LemonBoy
Copy link
Contributor

LemonBoy commented Jun 8, 2021

This is possibly related to #8396 as it implements random chunks of a Date structure.

@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label Jun 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants