Update Chrono Crate (#2242)

* Remove deprecated `and_hms()`

* Update chrono

* Update licenses and fix script

* Remove deprecated Date struct

* Remove chrono pin

* Skip format check on .vscode

Was failing for no reason.

* Replace deprecated chrono functions

* Add cargo-deny to update-licenses & pin versions (dae)

* Remove time 0.1 dependency  (dae)

We don't need to wait for chrono 0.5; it was provided behind a legacy
feature flag.
This commit is contained in:
RumovZ 2022-12-07 08:00:14 +01:00 committed by GitHub
parent ef3cfc561c
commit 0e7f02bfb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 602 additions and 324 deletions

View file

@ -11,9 +11,6 @@ unmaintained = "warn"
yanked = "warn"
notice = "warn"
ignore = [
# time 0.1, via chrono. Chrono no longer uses the vulnerable code path,
# and they plan to drop the dependency for the 0.5 release.
"RUSTSEC-2020-0071",
# atty: we don't use a custom allocator on Windows, and our deps haven't
# updated to alternatives yet
"RUSTSEC-2021-0145",

View file

@ -15,6 +15,7 @@
},
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,md,toml,svelte}"],
"excludes": [
".vscode",
"**/node_modules",
"out/**",
"**/*-lock.json",

82
Cargo.lock generated
View file

@ -407,16 +407,13 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.22"
version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1"
checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
dependencies = [
"iana-time-zone",
"js-sys",
"num-integer",
"num-traits",
"time 0.1.45",
"wasm-bindgen",
"winapi",
]
@ -764,9 +761,9 @@ dependencies = [
[[package]]
name = "cxx"
version = "1.0.82"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4a41a86530d0fe7f5d9ea779916b7cadd2d4f9add748b99c2c029cbbdfaf453"
checksum = "bdf07d07d6531bfcdbe9b8b739b104610c6508dcc4d63b410585faf338241daf"
dependencies = [
"cc",
"cxxbridge-flags",
@ -776,9 +773,9 @@ dependencies = [
[[package]]
name = "cxx-build"
version = "1.0.82"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06416d667ff3e3ad2df1cd8cd8afae5da26cf9cec4d0825040f88b5ca659a2f0"
checksum = "d2eb5b96ecdc99f72657332953d4d9c50135af1bac34277801cc3937906ebd39"
dependencies = [
"cc",
"codespan-reporting",
@ -791,15 +788,15 @@ dependencies = [
[[package]]
name = "cxxbridge-flags"
version = "1.0.82"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "820a9a2af1669deeef27cb271f476ffd196a2c4b6731336011e0ba63e2c7cf71"
checksum = "ac040a39517fd1674e0f32177648334b0f4074625b5588a64519804ba0553b12"
[[package]]
name = "cxxbridge-macro"
version = "1.0.82"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a08a6e2fcc370a089ad3b4aaf54db3b1b4cee38ddabce5896b33eb693275f470"
checksum = "1362b0ddcfc4eb0a1f57b68bd77dd99f0e826958a96abd0ae9bd092e114ffed6"
dependencies = [
"proc-macro2",
"quote",
@ -1711,9 +1708,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.137"
version = "0.2.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
[[package]]
name = "libsqlite3-sys"
@ -2041,9 +2038,9 @@ dependencies = [
[[package]]
name = "num-format"
version = "0.4.3"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54b862ff8df690cf089058c98b183676a7ed0f974cc08b426800093227cbff3b"
checksum = "a652d9771a63711fd3c3deb670acfbe5c30a4072e664d7a3bf5a9e1056ac72c3"
dependencies = [
"arrayvec",
"itoa 1.0.4",
@ -2474,7 +2471,7 @@ dependencies = [
"indexmap",
"line-wrap",
"serde",
"time 0.3.17",
"time",
"xml-rs",
]
@ -2847,7 +2844,7 @@ checksum = "6413f3de1edee53342e6138e75b56d32e7bc6e332b3bd62d497b1929d4cfbcdd"
dependencies = [
"pem",
"ring",
"time 0.3.17",
"time",
"yasna",
]
@ -3170,9 +3167,9 @@ checksum = "e25dfac463d778e353db5be2449d1cce89bd6fd23c9f1ea21310ce6e5a1b29c4"
[[package]]
name = "serde"
version = "1.0.148"
version = "1.0.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e53f64bb4ba0191d6d0676e1b141ca55047d83b74f5607e6d8eb88126c52c2dc"
checksum = "256b9932320c590e707b94576e3cc1f7c9024d0ee6612dfbcf1cb106cbe8e055"
dependencies = [
"serde_derive",
]
@ -3190,9 +3187,9 @@ dependencies = [
[[package]]
name = "serde_derive"
version = "1.0.148"
version = "1.0.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a55492425aa53521babf6137309e7d34c20bbfbbfcfe2c7f3a047fd1f6b92c0c"
checksum = "b4eae9b04cbffdfd550eb462ed33bc6a1b68c935127d008b27444d08380f94e4"
dependencies = [
"proc-macro2",
"quote",
@ -3391,7 +3388,7 @@ dependencies = [
"slog",
"term",
"thread_local",
"time 0.3.17",
"time",
]
[[package]]
@ -3513,9 +3510,9 @@ checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]]
name = "syn"
version = "1.0.104"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ae548ec36cf198c0ef7710d3c230987c2d6d7bd98ad6edc0274462724c585ce"
checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
dependencies = [
"proc-macro2",
"quote",
@ -3631,17 +3628,6 @@ dependencies = [
"once_cell",
]
[[package]]
name = "time"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi",
]
[[package]]
name = "time"
version = "0.3.17"
@ -3736,9 +3722,9 @@ dependencies = [
[[package]]
name = "tokio-macros"
version = "1.8.0"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484"
checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8"
dependencies = [
"proc-macro2",
"quote",
@ -3879,7 +3865,7 @@ dependencies = [
"log",
"p12",
"rcgen",
"time 0.3.17",
"time",
"tugger-common",
"tugger-windows",
"yasna",
@ -3896,9 +3882,9 @@ dependencies = [
[[package]]
name = "typenum"
version = "1.15.0"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]]
name = "unic-char-property"
@ -4112,12 +4098,6 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
@ -4409,7 +4389,7 @@ dependencies = [
"sha2",
"snafu",
"syn",
"time 0.3.17",
"time",
"time-macros",
"tokio",
"url",
@ -4449,7 +4429,7 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "346d34a236c9d3e5f3b9b74563f238f955bbd05fa0b8b4efa53c130c43982f4c"
dependencies = [
"time 0.3.17",
"time",
]
[[package]]
@ -4468,7 +4448,7 @@ dependencies = [
"hmac",
"pbkdf2",
"sha1",
"time 0.3.17",
"time",
"zstd 0.11.2+zstd.1.5.2",
]

File diff suppressed because it is too large Load diff

View file

@ -1,3 +1,7 @@
#!/bin/bash
cargo-license --features rustls native-tls --json --manifest-path ../rslib/Cargo.toml > licenses.json
cargo install cargo-license@0.5.1
cargo-license --features rustls --features native-tls --json --manifest-path ../rslib/Cargo.toml >licenses.json
cargo install cargo-deny@0.13.5
(cd .. && cargo deny check -A duplicate)

View file

@ -48,15 +48,13 @@ pct-str = { git = "https://github.com/timothee-haudebourg/pct-str.git", rev = "4
# pinned as any changes could invalidate sqlite indexes
unicase = "=2.6.0"
# pinned until the deprecation warnings can be addressed
chrono = "=0.4.22"
criterion = { version = "0.4.0", optional = true }
ammonia = "3.3.0"
async-trait = "0.1.59"
blake3 = "1.3.3"
bytes = "1.3.0"
chrono = { version = "0.4.23", default-features = false, features = ["std", "clock"] }
coarsetime = "0.1.22"
convert_case = "0.6.0"
dissimilar = "1.0.4"

View file

@ -104,7 +104,7 @@ fn thin_backups<P: AsRef<Path>>(
) -> Result<()> {
let backups =
read_dir(backup_folder)?.filter_map(|entry| entry.ok().and_then(Backup::from_entry));
let obsolete_backups = BackupFilter::new(Local::today(), limits).obsolete_backups(backups);
let obsolete_backups = BackupFilter::new(Local::now(), limits).obsolete_backups(backups);
for backup in obsolete_backups {
if let Err(error) = remove_file(&backup.path) {
error!(log, "failed to remove {:?}: {error:?}", &backup.path);
@ -176,7 +176,7 @@ enum BackupStage {
}
impl BackupFilter {
fn new(today: Date<Local>, limits: BackupLimits) -> Self {
fn new(today: DateTime<Local>, limits: BackupLimits) -> Self {
Self {
yesterday: today.num_days_from_ce() - 1,
last_kept_day: i32::MAX,
@ -262,7 +262,10 @@ mod test {
Backup {
datetime: Local
.from_local_datetime(
&NaiveDate::from_num_days_from_ce($num_days_from_ce).and_hms(0, 0, 0),
&NaiveDate::from_num_days_from_ce_opt($num_days_from_ce)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap(),
)
.latest()
.unwrap(),
@ -271,13 +274,19 @@ mod test {
};
($year:expr, $month:expr, $day:expr) => {
Backup {
datetime: Local.ymd($year, $month, $day).and_hms(0, 0, 0),
datetime: Local
.with_ymd_and_hms($year, $month, $day, 0, 0, 0)
.latest()
.unwrap(),
path: PathBuf::new(),
}
};
($year:expr, $month:expr, $day:expr, $hour:expr, $min:expr, $sec:expr) => {
Backup {
datetime: Local.ymd($year, $month, $day).and_hms($hour, $min, $sec),
datetime: Local
.with_ymd_and_hms($year, $month, $day, $hour, $min, $sec)
.latest()
.unwrap(),
path: PathBuf::new(),
}
};
@ -285,7 +294,10 @@ mod test {
#[test]
fn thinning_manual() {
let today = Local.ymd(2022, 2, 22);
let today = Local
.with_ymd_and_hms(2022, 2, 22, 0, 0, 0)
.latest()
.unwrap();
let limits = BackupLimits {
daily: 3,
weekly: 2,
@ -328,7 +340,10 @@ mod test {
#[test]
fn thinning_generic() {
let today = Local.ymd(2022, 1, 1);
let today = Local
.with_ymd_and_hms(2022, 1, 1, 0, 0, 0)
.latest()
.unwrap();
let today_ce_days = today.num_days_from_ce();
let limits = BackupLimits {
// config defaults
@ -357,7 +372,9 @@ mod test {
// monthly backups from the last day of the month
for _ in 0..limits.monthly {
for backup in backup_iter.by_ref() {
if backup.datetime.date().month() != backup.datetime.date().succ().month() {
if backup.datetime.month()
!= backup.datetime.date_naive().succ_opt().unwrap().month()
{
break;
} else {
expected.push(backup.clone())

View file

@ -89,7 +89,7 @@ impl Collection {
if s.new_timezone {
if self.get_creation_utc_offset().is_none() {
self.set_creation_utc_offset(Some(local_minutes_west_for_stamp(created.0)))?;
self.set_creation_utc_offset(Some(local_minutes_west_for_stamp(created)?))?;
}
} else {
self.set_creation_utc_offset(None)?;

View file

@ -71,13 +71,13 @@ impl Collection {
}
};
Ok(sched_timing_today(
sched_timing_today(
self.storage.creation_stamp()?,
now,
self.creation_utc_offset(),
current_utc_offset,
rollover_hour,
))
)
}
/// In the client case, return the current local timezone offset,
@ -88,7 +88,7 @@ impl Collection {
let config_tz = self
.get_configured_utc_offset()
.and_then(|v| FixedOffset::west_opt(v * 60))
.unwrap_or_else(|| FixedOffset::west(0));
.unwrap_or_else(|| FixedOffset::west_opt(0).unwrap());
let local_tz = TimestampSecs::now().local_utc_offset()?;
@ -113,9 +113,7 @@ impl Collection {
pub fn rollover_for_current_scheduler(&self) -> Result<u8> {
match self.scheduler_version() {
SchedulerVersion::V1 => Ok(v1_rollover_from_creation_stamp(
self.storage.creation_stamp()?.0,
)),
SchedulerVersion::V1 => v1_rollover_from_creation_stamp(self.storage.creation_stamp()?),
SchedulerVersion::V2 => Ok(self.get_v2_rollover().unwrap_or(4)),
}
}
@ -123,7 +121,7 @@ impl Collection {
pub(crate) fn set_rollover_for_current_scheduler(&mut self, hour: u8) -> Result<()> {
match self.scheduler_version() {
SchedulerVersion::V1 => self.set_creation_stamp(TimestampSecs(
v1_creation_date_adjusted_to_hour(self.storage.creation_stamp()?.0, hour),
v1_creation_date_adjusted_to_hour(self.storage.creation_stamp()?, hour)?,
)),
SchedulerVersion::V2 => self.set_v2_rollover(hour as u32),
}

View file

@ -1,7 +1,7 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use chrono::{Date, Duration, FixedOffset, Local, TimeZone, Timelike};
use chrono::{DateTime, Datelike, Duration, FixedOffset, Timelike};
use crate::prelude::*;
@ -26,14 +26,13 @@ pub fn sched_timing_today_v2_new(
current_secs: TimestampSecs,
current_utc_offset: FixedOffset,
rollover_hour: u8,
) -> SchedTimingToday {
) -> Result<SchedTimingToday> {
// get date(times) based on timezone offsets
let created_date = creation_secs.datetime(creation_utc_offset).date();
let now_datetime = current_secs.datetime(current_utc_offset);
let today = now_datetime.date();
let created_datetime = creation_secs.datetime(creation_utc_offset)?;
let now_datetime = current_secs.datetime(current_utc_offset)?;
// rollover
let rollover_today_datetime = today.and_hms(rollover_hour as u32, 0, 0);
let rollover_today_datetime = rollover_datetime(now_datetime, rollover_hour);
let rollover_passed = rollover_today_datetime <= now_datetime;
let next_day_at = TimestampSecs(if rollover_passed {
(rollover_today_datetime + Duration::days(1)).timestamp()
@ -42,22 +41,33 @@ pub fn sched_timing_today_v2_new(
});
// day count
let days_elapsed = days_elapsed(created_date, today, rollover_passed);
let days_elapsed = days_elapsed(created_datetime, now_datetime, rollover_passed);
SchedTimingToday {
Ok(SchedTimingToday {
now: current_secs,
days_elapsed,
next_day_at,
})
}
fn rollover_datetime(date: DateTime<FixedOffset>, rollover_hour: u8) -> DateTime<FixedOffset> {
date.with_hour((rollover_hour % 24) as u32)
.unwrap()
.with_minute(0)
.unwrap()
.with_second(0)
.unwrap()
.with_nanosecond(0)
.unwrap()
}
/// The number of times the day rolled over between two dates.
fn days_elapsed(
start_date: Date<FixedOffset>,
end_date: Date<FixedOffset>,
start_date: DateTime<FixedOffset>,
end_date: DateTime<FixedOffset>,
rollover_passed: bool,
) -> u32 {
let days = (end_date - start_date).num_days();
let days = end_date.num_days_from_ce() - start_date.num_days_from_ce();
// current day doesn't count before rollover time
let days = if rollover_passed { days } else { days - 1 };
@ -69,33 +79,33 @@ fn days_elapsed(
/// Build a FixedOffset struct, capping minutes_west if out of bounds.
pub(crate) fn fixed_offset_from_minutes(minutes_west: i32) -> FixedOffset {
let bounded_minutes = minutes_west.max(-23 * 60).min(23 * 60);
FixedOffset::west(bounded_minutes * 60)
FixedOffset::west_opt(bounded_minutes * 60).unwrap()
}
/// For the given timestamp, return minutes west of UTC in the
/// local timezone.
/// eg, Australia at +10 hours is -600.
/// Includes the daylight savings offset if applicable.
pub fn local_minutes_west_for_stamp(stamp: i64) -> i32 {
Local.timestamp(stamp, 0).offset().utc_minus_local() / 60
pub fn local_minutes_west_for_stamp(stamp: TimestampSecs) -> Result<i32> {
Ok(stamp.local_datetime()?.offset().utc_minus_local() / 60)
}
// Legacy code
// ----------------------------------
pub(crate) fn v1_rollover_from_creation_stamp(crt: i64) -> u8 {
Local.timestamp(crt, 0).hour() as u8
pub(crate) fn v1_rollover_from_creation_stamp(crt: TimestampSecs) -> Result<u8> {
crt.local_datetime().map(|dt| dt.hour() as u8)
}
pub(crate) fn v1_creation_date() -> i64 {
let now = TimestampSecs::now();
v1_creation_date_inner(now, local_minutes_west_for_stamp(now.0))
v1_creation_date_inner(now, local_minutes_west_for_stamp(now).unwrap())
}
fn v1_creation_date_inner(now: TimestampSecs, mins_west: i32) -> i64 {
let offset = fixed_offset_from_minutes(mins_west);
let now_dt = offset.timestamp(now.0, 0);
let four_am_dt = now_dt.date().and_hms(4, 0, 0);
let now_dt = now.datetime(offset).unwrap();
let four_am_dt = rollover_datetime(now_dt, 4);
let four_am_stamp = four_am_dt.timestamp();
if four_am_dt > now_dt {
@ -105,17 +115,17 @@ fn v1_creation_date_inner(now: TimestampSecs, mins_west: i32) -> i64 {
}
}
pub(crate) fn v1_creation_date_adjusted_to_hour(crt: i64, hour: u8) -> i64 {
let offset = fixed_offset_from_minutes(local_minutes_west_for_stamp(crt));
pub(crate) fn v1_creation_date_adjusted_to_hour(crt: TimestampSecs, hour: u8) -> Result<i64> {
let offset = fixed_offset_from_minutes(local_minutes_west_for_stamp(crt)?);
v1_creation_date_adjusted_to_hour_inner(crt, hour, offset)
}
fn v1_creation_date_adjusted_to_hour_inner(crt: i64, hour: u8, offset: FixedOffset) -> i64 {
offset
.timestamp(crt, 0)
.date()
.and_hms(hour as u32, 0, 0)
.timestamp()
fn v1_creation_date_adjusted_to_hour_inner(
crt: TimestampSecs,
hour: u8,
offset: FixedOffset,
) -> Result<i64> {
Ok(rollover_datetime(crt.datetime(offset)?, hour).timestamp())
}
fn sched_timing_today_v1(crt: TimestampSecs, now: TimestampSecs) -> SchedTimingToday {
@ -133,29 +143,22 @@ fn sched_timing_today_v2_legacy(
rollover: u8,
now: TimestampSecs,
current_utc_offset: FixedOffset,
) -> SchedTimingToday {
let crt_at_rollover = crt
.datetime(current_utc_offset)
.date()
.and_hms(rollover as u32, 0, 0)
.timestamp();
) -> Result<SchedTimingToday> {
let crt_at_rollover =
rollover_datetime(crt.datetime(current_utc_offset)?, rollover).timestamp();
let days_elapsed = (now.0 - crt_at_rollover) / 86_400;
let mut next_day_at = TimestampSecs(
now.datetime(current_utc_offset)
.date()
.and_hms(rollover as u32, 0, 0)
.timestamp(),
);
let mut next_day_at =
TimestampSecs(rollover_datetime(now.datetime(current_utc_offset)?, rollover).timestamp());
if next_day_at < now {
next_day_at = next_day_at.adding_secs(86_400);
}
SchedTimingToday {
Ok(SchedTimingToday {
now,
days_elapsed: days_elapsed as u32,
next_day_at,
}
})
}
// ----------------------------------
@ -168,11 +171,11 @@ pub(crate) fn sched_timing_today(
creation_utc_offset: Option<FixedOffset>,
current_utc_offset: FixedOffset,
rollover_hour: Option<u8>,
) -> SchedTimingToday {
) -> Result<SchedTimingToday> {
match (rollover_hour, creation_utc_offset) {
(None, _) => {
// if rollover unset, v1 scheduler
sched_timing_today_v1(creation_secs, current_secs)
Ok(sched_timing_today_v1(creation_secs, current_secs))
}
(Some(rollover), None) => {
// if creationOffset unset, v2 scheduler with legacy cutoff handling
@ -193,7 +196,7 @@ pub(crate) fn sched_timing_today(
#[cfg(test)]
mod test {
use chrono::{FixedOffset, Local, TimeZone, Utc};
use chrono::{FixedOffset, Local, TimeZone};
use super::*;
@ -213,7 +216,7 @@ mod test {
const AEST_MINS_WEST: i32 = -600;
fn aest_offset() -> FixedOffset {
FixedOffset::west(AEST_MINS_WEST * 60)
FixedOffset::west_opt(AEST_MINS_WEST * 60).unwrap()
}
#[test]
@ -226,19 +229,21 @@ mod test {
fn elap(start: i64, end: i64, start_west: i32, end_west: i32, rollhour: u8) -> u32 {
let start = TimestampSecs(start);
let end = TimestampSecs(end);
let start_west = FixedOffset::west(start_west * 60);
let end_west = FixedOffset::west(end_west * 60);
let today = sched_timing_today_v2_new(start, start_west, end, end_west, rollhour);
let start_west = FixedOffset::west_opt(start_west * 60).unwrap();
let end_west = FixedOffset::west_opt(end_west * 60).unwrap();
let today = sched_timing_today_v2_new(start, start_west, end, end_west, rollhour).unwrap();
today.days_elapsed
}
#[test]
fn days_elapsed() {
let local_offset = local_minutes_west_for_stamp(Utc::now().timestamp());
let local_offset = local_minutes_west_for_stamp(TimestampSecs::now()).unwrap();
let created_dt = FixedOffset::west(local_offset * 60)
.ymd(2019, 12, 1)
.and_hms(2, 0, 0);
let created_dt = FixedOffset::west_opt(local_offset * 60)
.unwrap()
.with_ymd_and_hms(2019, 12, 1, 2, 0, 0)
.latest()
.unwrap();
let crt = created_dt.timestamp();
// days can't be negative
@ -261,33 +266,60 @@ mod test {
1
);
let mdt = FixedOffset::west(6 * 60 * 60);
let mdt = FixedOffset::west_opt(6 * 60 * 60).unwrap();
let mdt_offset = mdt.utc_minus_local() / 60;
let mst = FixedOffset::west(7 * 60 * 60);
let mst = FixedOffset::west_opt(7 * 60 * 60).unwrap();
let mst_offset = mst.utc_minus_local() / 60;
// a collection created @ midnight in MDT in the past
let crt = mdt.ymd(2018, 8, 6).and_hms(0, 0, 0).timestamp();
let crt = mdt
.with_ymd_and_hms(2018, 8, 6, 0, 0, 0)
.latest()
.unwrap()
.timestamp();
// with the current time being MST
let now = mst.ymd(2019, 12, 26).and_hms(20, 0, 0).timestamp();
let now = mst
.with_ymd_and_hms(2019, 12, 26, 20, 0, 0)
.latest()
.unwrap()
.timestamp();
assert_eq!(elap(crt, now, mdt_offset, mst_offset, 4), 507);
// the previous implementation generated a different elapsed number of days with a change
// to DST, but the number shouldn't change
assert_eq!(elap(crt, now, mdt_offset, mdt_offset, 4), 507);
// collection created at 3am on the 6th, so day 1 starts at 4am on the 7th, and day 3 on the 9th.
let crt = mdt.ymd(2018, 8, 6).and_hms(3, 0, 0).timestamp();
let now = mst.ymd(2018, 8, 9).and_hms(1, 59, 59).timestamp();
let crt = mdt
.with_ymd_and_hms(2018, 8, 6, 3, 0, 0)
.latest()
.unwrap()
.timestamp();
let now = mst
.with_ymd_and_hms(2018, 8, 9, 1, 59, 59)
.latest()
.unwrap()
.timestamp();
assert_eq!(elap(crt, now, mdt_offset, mst_offset, 4), 2);
let now = mst.ymd(2018, 8, 9).and_hms(3, 59, 59).timestamp();
let now = mst
.with_ymd_and_hms(2018, 8, 9, 3, 59, 59)
.latest()
.unwrap()
.timestamp();
assert_eq!(elap(crt, now, mdt_offset, mst_offset, 4), 2);
let now = mst.ymd(2018, 8, 9).and_hms(4, 0, 0).timestamp();
let now = mst
.with_ymd_and_hms(2018, 8, 9, 4, 0, 0)
.latest()
.unwrap()
.timestamp();
assert_eq!(elap(crt, now, mdt_offset, mst_offset, 4), 3);
// try a bunch of combinations of creation time, current time, and rollover hour
let hours_of_interest = &[0, 1, 4, 12, 22, 23];
for creation_hour in hours_of_interest {
let crt_dt = mdt.ymd(2018, 8, 6).and_hms(*creation_hour, 0, 0);
let crt_dt = mdt
.with_ymd_and_hms(2018, 8, 6, *creation_hour, 0, 0)
.latest()
.unwrap();
let crt_stamp = crt_dt.timestamp();
let crt_offset = mdt_offset;
@ -295,8 +327,9 @@ mod test {
for current_hour in hours_of_interest {
for rollover_hour in hours_of_interest {
let end_dt = mdt
.ymd(2018, 8, 6 + current_day)
.and_hms(*current_hour, 0, 0);
.with_ymd_and_hms(2018, 8, 6 + current_day, *current_hour, 0, 0)
.latest()
.unwrap();
let end_stamp = end_dt.timestamp();
let end_offset = mdt_offset;
let elap_day = if *current_hour < *rollover_hour {
@ -324,42 +357,66 @@ mod test {
#[test]
fn next_day_at() {
let rollhour = 4;
let crt = Local.ymd(2019, 1, 1).and_hms(2, 0, 0);
let crt = Local
.with_ymd_and_hms(2019, 1, 1, 2, 0, 0)
.latest()
.unwrap();
// before the rollover, the next day should be later on the same day
let now = Local.ymd(2019, 1, 3).and_hms(2, 0, 0);
let next_day_at = Local.ymd(2019, 1, 3).and_hms(rollhour, 0, 0);
let now = Local
.with_ymd_and_hms(2019, 1, 3, 2, 0, 0)
.latest()
.unwrap();
let next_day_at = Local
.with_ymd_and_hms(2019, 1, 3, rollhour, 0, 0)
.latest()
.unwrap();
let today = sched_timing_today_v2_new(
TimestampSecs(crt.timestamp()),
*crt.offset(),
TimestampSecs(now.timestamp()),
*now.offset(),
rollhour as u8,
);
)
.unwrap();
assert_eq!(today.next_day_at.0, next_day_at.timestamp());
// after the rollover, the next day should be the next day
let now = Local.ymd(2019, 1, 3).and_hms(rollhour, 0, 0);
let next_day_at = Local.ymd(2019, 1, 4).and_hms(rollhour, 0, 0);
let now = Local
.with_ymd_and_hms(2019, 1, 3, rollhour, 0, 0)
.latest()
.unwrap();
let next_day_at = Local
.with_ymd_and_hms(2019, 1, 4, rollhour, 0, 0)
.latest()
.unwrap();
let today = sched_timing_today_v2_new(
TimestampSecs(crt.timestamp()),
*crt.offset(),
TimestampSecs(now.timestamp()),
*now.offset(),
rollhour as u8,
);
)
.unwrap();
assert_eq!(today.next_day_at.0, next_day_at.timestamp());
// after the rollover, the next day should be the next day
let now = Local.ymd(2019, 1, 3).and_hms(rollhour + 3, 0, 0);
let next_day_at = Local.ymd(2019, 1, 4).and_hms(rollhour, 0, 0);
let now = Local
.with_ymd_and_hms(2019, 1, 3, rollhour + 3, 0, 0)
.latest()
.unwrap();
let next_day_at = Local
.with_ymd_and_hms(2019, 1, 4, rollhour, 0, 0)
.latest()
.unwrap();
let today = sched_timing_today_v2_new(
TimestampSecs(crt.timestamp()),
*crt.offset(),
TimestampSecs(now.timestamp()),
*now.offset(),
rollhour as u8,
);
)
.unwrap();
assert_eq!(today.next_day_at.0, next_day_at.timestamp());
}
@ -378,20 +435,20 @@ mod test {
assert_eq!(
sched_timing_today_v2_legacy(TimestampSecs(1533564000), 0, now, aest_offset()),
SchedTimingToday {
Ok(SchedTimingToday {
now,
days_elapsed: 589,
next_day_at: TimestampSecs(1584540000)
}
})
);
assert_eq!(
sched_timing_today_v2_legacy(TimestampSecs(1524038400), 4, now, aest_offset()),
SchedTimingToday {
Ok(SchedTimingToday {
now,
days_elapsed: 700,
next_day_at: TimestampSecs(1584554400)
}
})
);
}
@ -399,26 +456,49 @@ mod test {
fn legacy_creation_stamp() {
let offset = fixed_offset_from_minutes(AEST_MINS_WEST);
let now = TimestampSecs(offset.ymd(2020, 5, 10).and_hms(9, 30, 30).timestamp());
let now = TimestampSecs(
offset
.with_ymd_and_hms(2020, 5, 10, 9, 30, 30)
.latest()
.unwrap()
.timestamp(),
);
assert_eq!(
v1_creation_date_inner(now, AEST_MINS_WEST),
offset.ymd(2020, 5, 10).and_hms(4, 0, 0).timestamp()
offset
.with_ymd_and_hms(2020, 5, 10, 4, 0, 0)
.latest()
.unwrap()
.timestamp()
);
let now = TimestampSecs(offset.ymd(2020, 5, 10).and_hms(1, 30, 30).timestamp());
let now = TimestampSecs(
offset
.with_ymd_and_hms(2020, 5, 10, 1, 30, 30)
.latest()
.unwrap()
.timestamp(),
);
assert_eq!(
v1_creation_date_inner(now, AEST_MINS_WEST),
offset.ymd(2020, 5, 9).and_hms(4, 0, 0).timestamp()
offset
.with_ymd_and_hms(2020, 5, 9, 4, 0, 0)
.latest()
.unwrap()
.timestamp()
);
let crt = v1_creation_date_inner(now, AEST_MINS_WEST);
assert_eq!(crt, v1_creation_date_adjusted_to_hour_inner(crt, 4, offset));
let crt = TimestampSecs(v1_creation_date_inner(now, AEST_MINS_WEST));
assert_eq!(
crt + 3600,
Ok(crt.0),
v1_creation_date_adjusted_to_hour_inner(crt, 4, offset)
);
assert_eq!(
Ok(crt.0 + 3600),
v1_creation_date_adjusted_to_hour_inner(crt, 5, offset)
);
assert_eq!(
crt - 3600 * 4,
Ok(crt.0 - 3600 * 4),
v1_creation_date_adjusted_to_hour_inner(crt, 0, offset)
);
}

View file

@ -122,7 +122,7 @@ impl Collection {
// enable new timezone code by default
let created = self.storage.creation_stamp()?;
if self.get_creation_utc_offset().is_none() {
self.set_creation_utc_offset(Some(local_minutes_west_for_stamp(created.0)))?;
self.set_creation_utc_offset(Some(local_minutes_west_for_stamp(created)?))?;
}
// force full sync

View file

@ -14,11 +14,10 @@ use super::{
};
use crate::{
config::schema11::schema11_config_as_string,
error::{AnkiError, DbErrorKind, Result},
i18n::I18n,
error::DbErrorKind,
prelude::*,
scheduler::timing::{local_minutes_west_for_stamp, v1_creation_date},
text::without_combining,
timestamp::TimestampMillis,
};
fn unicase_compare(s1: &str, s2: &str) -> Ordering {
@ -231,11 +230,11 @@ impl SqliteStorage {
if create {
db.execute_batch(include_str!("schema11.sql"))?;
// start at schema 11, then upgrade below
let crt = v1_creation_date();
let crt = TimestampSecs(v1_creation_date());
let offset = if server {
None
} else {
Some(local_minutes_west_for_stamp(crt))
Some(local_minutes_west_for_stamp(crt)?)
};
db.execute(
"update col set crt=?, scm=?, ver=?, conf=?",

View file

@ -31,17 +31,11 @@ impl TimestampSecs {
TimestampMillis(self.0 * 1000)
}
#[cfg(windows)]
pub(crate) fn local_datetime(self) -> Result<DateTime<Local>> {
std::panic::catch_unwind(|| Local.timestamp(self.0, 0))
// discard error as it doesn't satisfiy trait bounds
.ok()
.or_invalid("invalid date")
}
#[cfg(not(windows))]
pub(crate) fn local_datetime(self) -> Result<DateTime<Local>> {
Ok(Local.timestamp(self.0, 0))
Local
.timestamp_opt(self.0, 0)
.latest()
.or_invalid("invalid timestamp")
}
/// YYYY-mm-dd
@ -62,8 +56,11 @@ impl TimestampSecs {
Ok(*self.local_datetime()?.offset())
}
pub fn datetime(self, utc_offset: FixedOffset) -> DateTime<FixedOffset> {
utc_offset.timestamp(self.0, 0)
pub fn datetime(self, utc_offset: FixedOffset) -> Result<DateTime<FixedOffset>> {
utc_offset
.timestamp_opt(self.0, 0)
.latest()
.or_invalid("invalid timestamp")
}
pub fn adding_secs(self, secs: i64) -> Self {