stevedonovan / chrono-english

Converting informal English dates (like `date` command) to chrono DateTime in Rust
MIT License
57 stars 14 forks source link

panic in chrono when parsing the string "12pm" #24

Open willfindlay opened 1 year ago

willfindlay commented 1 year ago

See issue title.

Relevant Cargo.lock versions:

[[package]]
name = "chrono"
version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
dependencies = [
 "iana-time-zone",
 "js-sys",
 "num-integer",
 "num-traits 0.2.15",
 "serde",
 "time 0.1.45",
 "wasm-bindgen",
 "winapi",
]

[[package]]
name = "chrono-english"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f73d909da7eb4a7d88c679c3f5a1bc09d965754e0adb2e7627426cef96a00d6f"
dependencies = [
 "chrono",
 "scanlex",
]
willfindlay commented 1 year ago

The following changes to am_pm() fix the bug:

    fn am_pm(name: &str, mut hour: u32) -> DateResult<u32> {
        if name == "pm" {
            if hour != 12 {
                hour += 12;
            }
        } else if name == "am" {
            if hour == 12 {
                hour = 0;
            }
        } else if name != "am" {
            return date_result("expected am or pm");
        }
        Ok(hour)
    }
conradludgate commented 1 year ago

I noticed that my fork, interim, also has this problem.

What isn't immediately clear to me is whether "12pm" should be tonight or this morning. e.g. it's currently 2023-10-15T13:33:00+01:00. Should "12pm" be 2023-10-16T00:00:00+01:00 or 2023-10-15T00:00:00+01:00. My gut feeling tells me that it should choose tonight.

Never mind, 12pm should be midday

stevedonovan commented 1 year ago

Sorry guys, this is a nasty one and I will fix as soon as I get back from vacation.