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 From<NaiveDate> for NaiveDateTime #1355

Merged
merged 1 commit into from
Nov 20, 2023
Merged

Add From<NaiveDate> for NaiveDateTime #1355

merged 1 commit into from
Nov 20, 2023

Conversation

dcechano
Copy link
Contributor

This PR implements From<NaiveDate> to NaiveDateTime to facilitate more flexible APIs.

Motivation

Some users of the chrono library may want to create functions that can take both NaiveDate and NaiveDateTime. Without this trait that is not necessarily possible. Currently NaiveDate implements From<NaiveDateTime> so they could build an API that takes NaiveDateTimes and converts them NaiveDates but the reverse is not possible.

Currently only this is possible:

fn use_as_naive_date<D>(date: D) -> NaiveDate
where
    D: Into<NaiveDate>,
{
    date.into()
}

The function above can handle NaiveDate or NaiveDateTime input where the body can use the input to get a NaiveDate.
With my changes, this will be possible as well:

fn use_as_naive_datetime<D>(date: D) -> NaiveDateTime
where
    D: Into<NaiveDateTime>,
{
    date.into()
}

Here the use has created a function that can take NaiveDate or NaiveDateTime and get a NaiveDateTime. While ostensibly a small change, it gives chrono users the ability to create more powerful and flexible APIs.

Thank you for considering my pull request.

Copy link

codecov bot commented Nov 12, 2023

Codecov Report

Merging #1355 (cefa644) into 0.4.x (c0f418b) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##            0.4.x    #1355   +/-   ##
=======================================
  Coverage   91.61%   91.62%           
=======================================
  Files          38       38           
  Lines       17489    17497    +8     
=======================================
+ Hits        16023    16031    +8     
  Misses       1466     1466           
Files Coverage Δ
src/naive/datetime/mod.rs 97.75% <100.00%> (+0.01%) ⬆️

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

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

This seems reasonable, thanks!

@djc djc merged commit 6f3ab9d into chronotope:0.4.x Nov 20, 2023
37 checks passed
@pitdicker
Copy link
Collaborator

A late reply, but cool trick to use Into<NaiveDateTime> as a bound!

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

Successfully merging this pull request may close these issues.

3 participants