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
Deprecate TimeZone::datetime_from_str
#1251
Deprecate TimeZone::datetime_from_str
#1251
Conversation
3f24903
to
1bce6f1
Compare
Codecov Report
@@ Coverage Diff @@
## 0.4.x #1251 +/- ##
==========================================
- Coverage 91.41% 91.37% -0.04%
==========================================
Files 37 37
Lines 16453 16448 -5
==========================================
- Hits 15041 15030 -11
- Misses 1412 1418 +6
... and 1 file with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
@@ -338,6 +338,40 @@ fn test_datetime_parse_from_str() { | |||
); | |||
} | |||
|
|||
#[test] |
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.
Please split this into two commits. As a reminder, moving code and changing it in the same code makes it harder to review the changes.
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.
Yes, of course.
The format string doesn't contain any offset info.
1bce6f1
to
bbd7dd3
Compare
This was a transition to appropriate use of NaiveDateTime::parse_from_str(/**/).and_utc() as discussed in chronotope/chrono#1251. Signed-off-by: Jason Heath <jason.heath@progress.com>
Top-level documentation still recommends Also, deprecation warning points to |
I have another use case: I want to parse a string in the local time zone. So I currently use:
But moving away from the deprecated function that becomes: NaiveDateTime::parse_from_str(s, "%F %T").ok().and_then(|d| d.and_local_timezone(Local).single()) or possibly NaiveDateTime::parse_from_str(s, "%F %T").ok()?.and_local_timezone(Local).single() I have a couple complaints about this:
|
@tmccombs sorry you had a bad experience here.
|
I did not consider the use case of parsing a string without offset info to a known timezone, such as |
If doing that, then it may make sense to have it return an error if the format string includes an offset placeholder in the next major release. |
Maybe a |
Here's an example resolving the deprecation for iCal timestamp parsing, going from this to this: fn from_timestamp(s: &str, tzid: Option<&str>) -> Result<DateTime<Utc>> {
use chrono_tz::Tz;
use std::str::FromStr;
if let Ok(time) = NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%S") {
if let Some(tz) = tzid.map(|tz| Tz::from_str(tz).ok()).unwrap_or(Some(Tz::UTC)) {
if let Some(time) = time.and_local_timezone(tz).single() {
return Ok(time.with_timezone(&Utc));
}
}
}
bail!("failed to parse timestamp {}", s)
} The documentation or deprecation warning could definitely be improved, at least. |
@seanlinsley feel free to submit a PR implementing any improvements you have in mind. Unfortunately I won't have time to work on that myself. |
To quote #93 (comment):
There is one case where it is currently used in tests and documentation that makes some sense:
Utc.datetime_from_str
. This allows parsing a string without offset values directly into aDateTime<Utc>
.Users would have now have to write
NaiveDateTime::parse_from_str(/**/).and_utc()
. I don't think this is much worse, it may even show more clearly what is happening.