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

Resetting time to 00:00:00.0 #928

Closed
rimutaka opened this issue Jan 1, 2023 · 4 comments
Closed

Resetting time to 00:00:00.0 #928

rimutaka opened this issue Jan 1, 2023 · 4 comments

Comments

@rimutaka
Copy link

rimutaka commented Jan 1, 2023

It takes too many lines to do a simple and commonly used (at least in my case) operation:

let x = dt.with_hour(0).unwrap().with_minute(0).unwrap().with_second(0).unwrap();

Is there a simpler way of achieving the same?

if not, would you consider a PR to do a reset like that in a single step? E.g.

let x = dt.reset_time();
@esheppa
Copy link
Collaborator

esheppa commented Jan 2, 2023

Thanks for the question @rimutaka - one possible option is to do:

let x = NaiveTime::MIN;

This works as of e7d4402 and will be released in 0.4.23 but can currently be accessed on branch 0.4.x.

@rimutaka
Copy link
Author

rimutaka commented Jan 3, 2023

@esheppa , Eric, sorry, I probably miss something important here.

Is there a way to replace the time only portion in DateTime<Tz> with NaiveTime::MIN as you suggested?

I have an immutable var with DateTime<Tz>, say let x = Utc::now();. I need to reset the time portion of that to zeros, e.g. let y = x.reset_time(); or x.with_time(NaiveTime::MIN);.

  • x: 2023-01-03T23:19:08.281426984+00:00
  • y: 2023-01-03T00:00:00.0+00:00

And I need to do it often enough in my code to find the current multi-step approach of resetting hr, min, sec, ns too verbose.

rimutaka added a commit to rimutaka/chrono that referenced this issue Jan 5, 2023
* added the trail itself
* implemented the trait for DateTime, NaiveDateTime and NaiveTime
* implemented tests for all trait implementations
rimutaka added a commit to rimutaka/chrono that referenced this issue Jan 5, 2023
@rimutaka
Copy link
Author

rimutaka commented Jan 5, 2023

@esheppa , Eric, I submitted a draft PR for this feature under #930 . Can you or any other maintainers take a look before I spend any more time polishing it?

Unrelated: the pipeline has a step with cargo clippy --color=always -- -D warnings -A clippy::manual-non-exhaustive, but there are no warnings related to clippy::manual-non-exhaustive. Should that option be removed?

rimutaka added a commit to rimutaka/chrono that referenced this issue Jan 5, 2023
@rimutaka
Copy link
Author

rimutaka commented Jan 7, 2023

Looks like the simplest way of resetting the time for DateTime<Tz> is this:

let x = dt_w_tz.date_naive()
        .and_time(NaiveTime::MIN)
        .and_local_timezone(x.timezone())
        .unwrap();

The maintainers looked into my PR #930 to simplify this, but the gains were marginal and we would still need to use unwrap() to avoid a potential panic.

@rimutaka rimutaka closed this as completed Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants