-
Notifications
You must be signed in to change notification settings - Fork 26
Support weekdays in parse_datetime #34
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,12 @@ use std::fmt::{self, Display}; | |
// Expose parse_datetime | ||
mod parse_relative_time; | ||
|
||
use chrono::{DateTime, FixedOffset, Local, LocalResult, NaiveDateTime, TimeZone}; | ||
mod parse_weekday; | ||
|
||
use chrono::{ | ||
DateTime, Datelike, Duration, FixedOffset, Local, LocalResult, NaiveDateTime, TimeZone, | ||
Timelike, | ||
}; | ||
|
||
use parse_relative_time::parse_relative_time; | ||
|
||
|
@@ -168,6 +173,27 @@ pub fn parse_datetime_at_date<S: AsRef<str> + Clone>( | |
} | ||
} | ||
|
||
// parse weekday | ||
if let Some(weekday) = parse_weekday::parse_weekday(s.as_ref()) { | ||
let mut beginning_of_day = date | ||
.with_hour(0) | ||
.unwrap() | ||
.with_minute(0) | ||
.unwrap() | ||
.with_second(0) | ||
.unwrap() | ||
.with_nanosecond(0) | ||
.unwrap(); | ||
|
||
while beginning_of_day.weekday() != weekday { | ||
beginning_of_day += Duration::days(1); | ||
} | ||
|
||
let dt = DateTime::<FixedOffset>::from(beginning_of_day); | ||
|
||
return Ok(dt); | ||
} | ||
|
||
// Parse epoch seconds | ||
if s.as_ref().bytes().next() == Some(b'@') { | ||
if let Ok(parsed) = NaiveDateTime::parse_from_str(&s.as_ref()[1..], "%s") { | ||
|
@@ -353,6 +379,56 @@ mod tests { | |
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod weekday { | ||
use chrono::{DateTime, Local, TimeZone}; | ||
|
||
use crate::parse_datetime_at_date; | ||
|
||
fn get_formatted_date(date: DateTime<Local>, weekday: &str) -> String { | ||
let result = parse_datetime_at_date(date, weekday).unwrap(); | ||
|
||
return result.format("%F %T %f").to_string(); | ||
} | ||
#[test] | ||
fn test_weekday() { | ||
// add some constant hours and minutes and seconds to check its reset | ||
let date = Local.with_ymd_and_hms(2023, 02, 28, 10, 12, 3).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oww, I really want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure how to best not use local but agreed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, |
||
|
||
// 2023-2-28 is tuesday | ||
assert_eq!( | ||
get_formatted_date(date, "tuesday"), | ||
"2023-02-28 00:00:00 000000000" | ||
); | ||
|
||
// 2023-3-01 is wednesday | ||
assert_eq!( | ||
get_formatted_date(date, "wed"), | ||
"2023-03-01 00:00:00 000000000" | ||
); | ||
|
||
assert_eq!( | ||
get_formatted_date(date, "thu"), | ||
"2023-03-02 00:00:00 000000000" | ||
); | ||
|
||
assert_eq!( | ||
get_formatted_date(date, "fri"), | ||
"2023-03-03 00:00:00 000000000" | ||
); | ||
|
||
assert_eq!( | ||
get_formatted_date(date, "sat"), | ||
"2023-03-04 00:00:00 000000000" | ||
); | ||
|
||
assert_eq!( | ||
get_formatted_date(date, "sun"), | ||
"2023-03-05 00:00:00 000000000" | ||
); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod timestamp { | ||
use crate::parse_datetime; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
use chrono::Weekday; | ||
sylvestre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use nom::branch::alt; | ||
use nom::bytes::complete::tag; | ||
use nom::combinator::value; | ||
use nom::{self, IResult}; | ||
|
||
// Helper macro to simplify tag matching | ||
macro_rules! tag_match { | ||
($day:expr, $($pattern:expr),+) => { | ||
value($day, alt(($(tag($pattern)),+))) | ||
}; | ||
} | ||
|
||
pub(crate) fn parse_weekday(s: &str) -> Option<Weekday> { | ||
let s = s.trim().to_lowercase(); | ||
let s = s.as_str(); | ||
|
||
let parse_result: IResult<&str, Weekday> = nom::combinator::all_consuming(alt(( | ||
tag_match!(Weekday::Mon, "monday", "mon"), | ||
tag_match!(Weekday::Tue, "tuesday", "tues", "tue"), | ||
tag_match!(Weekday::Wed, "wednesday", "wednes", "wed"), | ||
tag_match!(Weekday::Thu, "thursday", "thurs", "thur", "thu"), | ||
tag_match!(Weekday::Fri, "friday", "fri"), | ||
tag_match!(Weekday::Sat, "saturday", "sat"), | ||
tag_match!(Weekday::Sun, "sunday", "sun"), | ||
)))(s); | ||
|
||
match parse_result { | ||
Ok((_, weekday)) => Some(weekday), | ||
Err(_) => None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why don't you return a Result ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had this discussion with @tertsdiepraam, but I think the reason is this is used in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My reasoning was that we only care about the parsing the whole datetime. If a weekday fails, another type of item might still succeedsl. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sylvestre should i change it to Result? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tertsdiepraam @sylvestre its been a while, i am fine with whatever but it would be good to get this pr also merged, before we forget what it was about :) . |
||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use chrono::Weekday::*; | ||
|
||
use crate::parse_weekday::parse_weekday; | ||
|
||
#[test] | ||
fn test_valid_weekdays() { | ||
let days = [ | ||
("mon", Mon), | ||
("monday", Mon), | ||
("tue", Tue), | ||
("tues", Tue), | ||
("tuesday", Tue), | ||
("wed", Wed), | ||
("wednes", Wed), | ||
("wednesday", Wed), | ||
("thu", Thu), | ||
("thursday", Thu), | ||
("fri", Fri), | ||
("friday", Fri), | ||
("sat", Sat), | ||
("saturday", Sat), | ||
("sun", Sun), | ||
("sunday", Sun), | ||
]; | ||
|
||
for (name, weekday) in days { | ||
assert_eq!(parse_weekday(name), Some(weekday)); | ||
assert_eq!(parse_weekday(&format!(" {}", name)), Some(weekday)); | ||
assert_eq!(parse_weekday(&format!(" {} ", name)), Some(weekday)); | ||
assert_eq!(parse_weekday(&format!("{} ", name)), Some(weekday)); | ||
|
||
let (left, right) = name.split_at(1); | ||
let (test_str1, test_str2) = ( | ||
format!("{}{}", left.to_uppercase(), right.to_lowercase()), | ||
format!("{}{}", left.to_lowercase(), right.to_uppercase()), | ||
); | ||
|
||
assert_eq!(parse_weekday(&test_str1), Some(weekday)); | ||
assert_eq!(parse_weekday(&test_str2), Some(weekday)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_invalid_weekdays() { | ||
let days = [ | ||
"mond", | ||
"tuesda", | ||
"we", | ||
"th", | ||
"fr", | ||
"sa", | ||
"su", | ||
"garbageday", | ||
sylvestre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"tomorrow", | ||
"yesterday", | ||
]; | ||
for day in days { | ||
assert!(parse_weekday(day).is_none()); | ||
} | ||
} | ||
} |
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.
Oh I see what the trouble was now. I forgot we needed
00:00
on the time. I'm not sure what the best way to do that is. This looks ok, but it would be great ifchrono
had some nice method for this, but I can't find any 😄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.
i dit not find anything either :(