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

Suggestions for TzDateTime Enhancements #1473

Closed
hkalexling opened this issue Sep 23, 2024 · 1 comment · Fixed by #1484
Closed

Suggestions for TzDateTime Enhancements #1473

hkalexling opened this issue Sep 23, 2024 · 1 comment · Fixed by #1484
Labels
Accepted Accepted Request Enhancement Request New feature or request Stdlib Standard library issue
Milestone

Comments

@hkalexling
Copy link
Contributor

In my application I need to work with timezones, and I found TzDateTime in the standard library a bit lacking.

1. from_date equivalent with timezone information

It would be great to have a function similar to datetime::from_date but with timezone information. Currently I need to write something like this:

fn TzDateTime from_date_tz(int year, Month month = JANUARY, int day = 1, int hour = 0, int min = 0, int sec = 0, int us = 0, int gmt_offset = 0)
{
    DateTime dt = from_date(year, month, day, hour, min, sec, us);
    dt.time -= (Time)gmt_offset * SEC;
    return { dt, gmt_offset };
}

2. timezone-aware add_* methods

It would be useful to have TzDateTime counterparts for the add_* methods. While we can already call these methods on TzDateTime (since TzDateTime has inlined DateTime), we must manually adjust the returned values to maintain timezone consistency. For example:

	TzDateTime now = datetime::now().to_local();

	io::printn(now); // 2024/SEPTEMBER/23 13:50:51 +7

	// This is enough for add_days etc., where we directly work with `time`
	io::printn(now.add_days(10).to_local()); // 2024/OCTOBER/3 13:50:51 +7

	// If we do this for add_months and add_years, it produces incorrect result
	io::printn(now.add_months(1).to_local()); // 2024/OCTOBER/23 20:50:51 +7

	// Instead we have to do
	TzDateTime dt = { now.add_months(1), now.gmt_offset };
	io::printn(dt); // 2024/OCTOBER/23 13:50:51 +7

3. to_gmt_offset method

A to_gmt_offset method for both DateTime and TzDateTime would be useful. This would function similarly to to_local, but allow specifying a custom GMT offset.

If these ideas align with the direction of c3 I'd be happy to submit a PR.

@lerno
Copy link
Collaborator

lerno commented Sep 23, 2024

Yes, those sounds like good additions.

@lerno lerno added Enhancement Request New feature or request Stdlib Standard library issue Accepted Accepted Request labels Sep 23, 2024
@lerno lerno added this to the 0.6.4 milestone Sep 24, 2024
hkalexling added a commit to hkalexling/c3c that referenced this issue Sep 26, 2024
@lerno lerno modified the milestones: 0.6.4, 0.6.3 Sep 26, 2024
lerno pushed a commit that referenced this issue Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted Accepted Request Enhancement Request New feature or request Stdlib Standard library issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants