fix default rollover

This commit is contained in:
Damien Elmes 2020-05-10 21:08:33 +10:00
parent 8af475ac6f
commit 2c7900989c
3 changed files with 45 additions and 21 deletions

View file

@ -4,7 +4,6 @@
from __future__ import annotations
import copy
import datetime
import os
import pprint
import random
@ -69,12 +68,6 @@ class _Collection:
self.tags = TagManager(self)
self.conf = ConfigManager(self)
self.load()
if not self.crt:
d = datetime.datetime.today()
d -= datetime.timedelta(hours=4)
d = datetime.datetime(d.year, d.month, d.day)
d += datetime.timedelta(hours=4)
self.crt = int(time.mktime(d.timetuple()))
self._loadScheduler()
def name(self) -> Any:

View file

@ -95,6 +95,24 @@ pub fn local_minutes_west_for_stamp(stamp: i64) -> i32 {
// Legacy code
// ----------------------------------
pub(crate) fn v1_creation_date() -> i64 {
let now = TimestampSecs::now();
v1_creation_date_inner(now, local_minutes_west_for_stamp(now.0))
}
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 four_am_stamp = four_am_dt.timestamp();
if four_am_dt > now_dt {
four_am_stamp - 86_400
} else {
four_am_stamp
}
}
fn sched_timing_today_v1(crt: i64, now: i64) -> SchedTimingToday {
let days_elapsed = (now - crt) / 86_400;
let next_day_at = crt + (days_elapsed + 1) * 86_400;
@ -164,15 +182,12 @@ pub(crate) fn sched_timing_today(
#[cfg(test)]
mod test {
use super::SchedTimingToday;
use crate::sched::cutoff::sched_timing_today_v1;
use crate::sched::cutoff::sched_timing_today_v2_legacy;
use crate::sched::cutoff::{
fixed_offset_from_minutes, local_minutes_west_for_stamp, normalized_rollover_hour,
sched_timing_today_v2_new,
};
use super::*;
use chrono::{FixedOffset, Local, TimeZone, Utc};
// static timezone for tests
const AEST_MINS_WEST: i32 = -600;
#[test]
fn rollover() {
assert_eq!(normalized_rollover_hour(4), 4);
@ -186,8 +201,8 @@ mod test {
#[test]
fn fixed_offset() {
let offset = fixed_offset_from_minutes(-600);
assert_eq!(offset.utc_minus_local(), -600 * 60);
let offset = fixed_offset_from_minutes(AEST_MINS_WEST);
assert_eq!(offset.utc_minus_local(), AEST_MINS_WEST * 60);
}
// helper
@ -340,7 +355,6 @@ mod test {
#[test]
fn legacy_timing() {
let now = 1584491078;
let mins_west = -600;
assert_eq!(
sched_timing_today_v1(1575226800, now),
@ -351,7 +365,7 @@ mod test {
);
assert_eq!(
sched_timing_today_v2_legacy(1533564000, 0, now, mins_west),
sched_timing_today_v2_legacy(1533564000, 0, now, AEST_MINS_WEST),
SchedTimingToday {
days_elapsed: 589,
next_day_at: 1584540000
@ -359,11 +373,28 @@ mod test {
);
assert_eq!(
sched_timing_today_v2_legacy(1524038400, 4, now, mins_west),
sched_timing_today_v2_legacy(1524038400, 4, now, AEST_MINS_WEST),
SchedTimingToday {
days_elapsed: 700,
next_day_at: 1584554400
}
);
}
#[test]
fn legacy_creation_stamp() {
let offset = fixed_offset_from_minutes(AEST_MINS_WEST);
let now = TimestampSecs(offset.ymd(2020, 05, 10).and_hms(9, 30, 30).timestamp());
assert_eq!(
v1_creation_date_inner(now, AEST_MINS_WEST),
offset.ymd(2020, 05, 10).and_hms(4, 0, 0).timestamp()
);
let now = TimestampSecs(offset.ymd(2020, 05, 10).and_hms(1, 30, 30).timestamp());
assert_eq!(
v1_creation_date_inner(now, AEST_MINS_WEST),
offset.ymd(2020, 05, 9).and_hms(4, 0, 0).timestamp()
);
}
}

View file

@ -5,7 +5,7 @@ use crate::config::schema11_config_as_string;
use crate::err::Result;
use crate::err::{AnkiError, DBErrorKind};
use crate::timestamp::{TimestampMillis, TimestampSecs};
use crate::{i18n::I18n, text::without_combining, types::Usn};
use crate::{i18n::I18n, sched::cutoff::v1_creation_date, text::without_combining, types::Usn};
use regex::Regex;
use rusqlite::{functions::FunctionFlags, params, Connection, NO_PARAMS};
use std::cmp::Ordering;
@ -182,7 +182,7 @@ impl SqliteStorage {
db.execute(
"update col set crt=?, ver=?, conf=?",
params![
TimestampSecs::now(),
v1_creation_date(),
SCHEMA_STARTING_VERSION,
&schema11_config_as_string()
],