Skip to content

Commit

Permalink
chore: upgrade chrono and rewrite local usage
Browse files Browse the repository at this point in the history
  • Loading branch information
cavivie committed Sep 26, 2023
1 parent d6000ac commit b71337e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
76 changes: 38 additions & 38 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,19 @@ mod test {
fn frequency_every_day() {
let mut c = build_context(RollingConditionBase::new().daily(), 9);
c.rolling
.write_with_datetime(b"Line 1\n", &Local.ymd(2021, 3, 30).and_hms(1, 2, 3))
.write_with_datetime(b"Line 1\n", &Local.with_ymd_and_hms(2021, 3, 30, 1, 2, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 2\n", &Local.ymd(2021, 3, 30).and_hms(1, 3, 0))
.write_with_datetime(b"Line 2\n", &Local.with_ymd_and_hms(2021, 3, 30, 1, 3, 0).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 3\n", &Local.ymd(2021, 3, 31).and_hms(1, 4, 0))
.write_with_datetime(b"Line 3\n", &Local.with_ymd_and_hms(2021, 3, 31, 1, 4, 0).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 4\n", &Local.ymd(2021, 5, 31).and_hms(1, 4, 0))
.write_with_datetime(b"Line 4\n", &Local.with_ymd_and_hms(2021, 5, 31, 1, 4, 0).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 5\n", &Local.ymd(2022, 5, 31).and_hms(1, 4, 0))
.write_with_datetime(b"Line 5\n", &Local.with_ymd_and_hms(2022, 5, 31, 1, 4, 0).unwrap())
.unwrap();
assert!(!AsRef::<Path>::as_ref(&c.rolling.filename_for(4)).exists());
c.verify_contains("Line 1", 3);
Expand All @@ -149,19 +149,19 @@ mod test {
fn frequency_every_day_limited_files() {
let mut c = build_context(RollingConditionBase::new().daily(), 2);
c.rolling
.write_with_datetime(b"Line 1\n", &Local.ymd(2021, 3, 30).and_hms(1, 2, 3))
.write_with_datetime(b"Line 1\n", &Local.with_ymd_and_hms(2021, 3, 30, 1, 2, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 2\n", &Local.ymd(2021, 3, 30).and_hms(1, 3, 0))
.write_with_datetime(b"Line 2\n", &Local.with_ymd_and_hms(2021, 3, 30, 1, 3, 0).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 3\n", &Local.ymd(2021, 3, 31).and_hms(1, 4, 0))
.write_with_datetime(b"Line 3\n", &Local.with_ymd_and_hms(2021, 3, 31, 1, 4, 0).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 4\n", &Local.ymd(2021, 5, 31).and_hms(1, 4, 0))
.write_with_datetime(b"Line 4\n", &Local.with_ymd_and_hms(2021, 5, 31, 1, 4, 0).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 5\n", &Local.ymd(2022, 5, 31).and_hms(1, 4, 0))
.write_with_datetime(b"Line 5\n", &Local.with_ymd_and_hms(2022, 5, 31, 1, 4, 0).unwrap())
.unwrap();
assert!(!AsRef::<Path>::as_ref(&c.rolling.filename_for(4)).exists());
assert!(!AsRef::<Path>::as_ref(&c.rolling.filename_for(3)).exists());
Expand All @@ -174,16 +174,16 @@ mod test {
fn frequency_every_hour() {
let mut c = build_context(RollingConditionBase::new().hourly(), 9);
c.rolling
.write_with_datetime(b"Line 1\n", &Local.ymd(2021, 3, 30).and_hms(1, 2, 3))
.write_with_datetime(b"Line 1\n", &Local.with_ymd_and_hms(2021, 3, 30, 1, 2, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 2\n", &Local.ymd(2021, 3, 30).and_hms(1, 3, 2))
.write_with_datetime(b"Line 2\n", &Local.with_ymd_and_hms(2021, 3, 30, 1, 3, 2).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 3\n", &Local.ymd(2021, 3, 30).and_hms(2, 1, 0))
.write_with_datetime(b"Line 3\n", &Local.with_ymd_and_hms(2021, 3, 30, 2, 1, 0).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 4\n", &Local.ymd(2021, 3, 31).and_hms(2, 1, 0))
.write_with_datetime(b"Line 4\n", &Local.with_ymd_and_hms(2021, 3, 31, 2, 1, 0).unwrap())
.unwrap();
assert!(!AsRef::<Path>::as_ref(&c.rolling.filename_for(3)).exists());
c.verify_contains("Line 1", 2);
Expand All @@ -196,22 +196,22 @@ mod test {
fn frequency_every_minute() {
let mut c = build_context(RollingConditionBase::new().frequency(RollingFrequency::EveryMinute), 9);
c.rolling
.write_with_datetime(b"Line 1\n", &Local.ymd(2021, 3, 30).and_hms(1, 2, 3))
.write_with_datetime(b"Line 1\n", &Local.with_ymd_and_hms(2021, 3, 30, 1, 2, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 2\n", &Local.ymd(2021, 3, 30).and_hms(1, 2, 3))
.write_with_datetime(b"Line 2\n", &Local.with_ymd_and_hms(2021, 3, 30, 1, 2, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 3\n", &Local.ymd(2021, 3, 30).and_hms(1, 2, 4))
.write_with_datetime(b"Line 3\n", &Local.with_ymd_and_hms(2021, 3, 30, 1, 2, 4).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 4\n", &Local.ymd(2021, 3, 30).and_hms(1, 3, 0))
.write_with_datetime(b"Line 4\n", &Local.with_ymd_and_hms(2021, 3, 30, 1, 3, 0).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 5\n", &Local.ymd(2021, 3, 30).and_hms(2, 3, 0))
.write_with_datetime(b"Line 5\n", &Local.with_ymd_and_hms(2021, 3, 30, 2, 3, 0).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"Line 6\n", &Local.ymd(2022, 3, 30).and_hms(2, 3, 0))
.write_with_datetime(b"Line 6\n", &Local.with_ymd_and_hms(2022, 3, 30, 2, 3, 0).unwrap())
.unwrap();
assert!(!AsRef::<Path>::as_ref(&c.rolling.filename_for(4)).exists());
c.verify_contains("Line 1", 3);
Expand All @@ -226,75 +226,75 @@ mod test {
fn max_size() {
let mut c = build_context(RollingConditionBase::new().max_size(10), 9);
c.rolling
.write_with_datetime(b"12345", &Local.ymd(2021, 3, 30).and_hms(1, 2, 3))
.write_with_datetime(b"12345", &Local.with_ymd_and_hms(2021, 3, 30, 1, 2, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"6789", &Local.ymd(2021, 3, 30).and_hms(1, 3, 3))
.write_with_datetime(b"6789", &Local.with_ymd_and_hms(2021, 3, 30, 1, 3, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"0", &Local.ymd(2021, 3, 30).and_hms(2, 3, 3))
.write_with_datetime(b"0", &Local.with_ymd_and_hms(2021, 3, 30, 2, 3, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"abcdefghijklmn", &Local.ymd(2021, 3, 31).and_hms(2, 3, 3))
.write_with_datetime(b"abcdefghijkl", &Local.with_ymd_and_hms(2021, 3, 31, 2, 3, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"ZZZ", &Local.ymd(2022, 3, 31).and_hms(1, 2, 3))
.write_with_datetime(b"ZZZ", &Local.with_ymd_and_hms(2022, 3, 31, 1, 2, 3).unwrap())
.unwrap();
assert!(!AsRef::<Path>::as_ref(&c.rolling.filename_for(3)).exists());
c.verify_contains("1234567890", 2);
c.verify_contains("abcdefghijklmn", 1);
c.verify_contains("abcdefghijkl", 1);
c.verify_contains("ZZZ", 0);
}

#[test]
fn max_size_existing() {
let mut c = build_context(RollingConditionBase::new().max_size(10), 9);
c.rolling
.write_with_datetime(b"12345", &Local.ymd(2021, 3, 30).and_hms(1, 2, 3))
.write_with_datetime(b"12345", &Local.with_ymd_and_hms(2021, 3, 30, 1, 2, 3).unwrap())
.unwrap();
// close the file and make sure that it can re-open it, and that it
// resets the file size properly.
c.rolling.writer_opt.take();
c.rolling.current_filesize = 0;
c.rolling
.write_with_datetime(b"6789", &Local.ymd(2021, 3, 30).and_hms(1, 3, 3))
.write_with_datetime(b"6789", &Local.with_ymd_and_hms(2021, 3, 30, 1, 3, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"0", &Local.ymd(2021, 3, 30).and_hms(2, 3, 3))
.write_with_datetime(b"0", &Local.with_ymd_and_hms(2021, 3, 30, 2, 3, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"abcdefghijklmn", &Local.ymd(2021, 3, 31).and_hms(2, 3, 3))
.write_with_datetime(b"abcdefghijkl", &Local.with_ymd_and_hms(2021, 3, 31, 2, 3, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"ZZZ", &Local.ymd(2022, 3, 31).and_hms(1, 2, 3))
.write_with_datetime(b"ZZZ", &Local.with_ymd_and_hms(2022, 3, 31, 1, 2, 3).unwrap())
.unwrap();
assert!(!AsRef::<Path>::as_ref(&c.rolling.filename_for(3)).exists());
c.verify_contains("1234567890", 2);
c.verify_contains("abcdefghijklmn", 1);
c.verify_contains("abcdefghijkl", 1);
c.verify_contains("ZZZ", 0);
}

#[test]
fn daily_and_max_size() {
let mut c = build_context(RollingConditionBase::new().daily().max_size(10), 9);
c.rolling
.write_with_datetime(b"12345", &Local.ymd(2021, 3, 30).and_hms(1, 2, 3))
.write_with_datetime(b"12345", &Local.with_ymd_and_hms(2021, 3, 30, 1, 2, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"6789", &Local.ymd(2021, 3, 30).and_hms(2, 3, 3))
.write_with_datetime(b"6789", &Local.with_ymd_and_hms(2021, 3, 30, 2, 3, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"0", &Local.ymd(2021, 3, 31).and_hms(2, 3, 3))
.write_with_datetime(b"0", &Local.with_ymd_and_hms(2021, 3, 31, 2, 3, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"abcdefghijklmn", &Local.ymd(2021, 3, 31).and_hms(3, 3, 3))
.write_with_datetime(b"abcdefghijkl", &Local.with_ymd_and_hms(2021, 3, 31, 3, 3, 3).unwrap())
.unwrap();
c.rolling
.write_with_datetime(b"ZZZ", &Local.ymd(2021, 3, 31).and_hms(4, 4, 4))
.write_with_datetime(b"ZZZ", &Local.with_ymd_and_hms(2021, 3, 31, 4, 4, 4).unwrap())
.unwrap();
assert!(!AsRef::<Path>::as_ref(&c.rolling.filename_for(3)).exists());
c.verify_contains("123456789", 2);
c.verify_contains("0abcdefghijklmn", 1);
c.verify_contains("0abcdefghijkl", 1);
c.verify_contains("ZZZ", 0);
}
}
Expand Down
16 changes: 7 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ impl RollingFrequency {
/// Calculates a datetime that will be different if data should be in
/// different files.
pub fn equivalent_datetime(&self, dt: &DateTime<Local>) -> DateTime<Local> {
match self {
RollingFrequency::EveryDay => Local.ymd(dt.year(), dt.month(), dt.day()).and_hms(0, 0, 0),
RollingFrequency::EveryHour => Local.ymd(dt.year(), dt.month(), dt.day()).and_hms(dt.hour(), 0, 0),
RollingFrequency::EveryMinute => {
Local
.ymd(dt.year(), dt.month(), dt.day())
.and_hms(dt.hour(), dt.minute(), 0)
},
}
let (year, month, day) = (dt.year(), dt.month(), dt.day());
let (hour, min, sec) = match self {
RollingFrequency::EveryDay => (0, 0, 0),
RollingFrequency::EveryHour => (dt.hour(), 0, 0),
RollingFrequency::EveryMinute => (dt.hour(), dt.minute(), 0),
};
Local.with_ymd_and_hms(year, month, day, hour, min, sec).unwrap()
}
}

Expand Down

0 comments on commit b71337e

Please sign in to comment.