diff --git a/pylib/anki/foreign_data/__init__.py b/pylib/anki/foreign_data/__init__.py index 58f96e710..f878c0871 100644 --- a/pylib/anki/foreign_data/__init__.py +++ b/pylib/anki/foreign_data/__init__.py @@ -72,6 +72,17 @@ class ForeignNotetype: @dataclass class ForeignCard: + """Data for creating an Anki card. + + Usually a review card, as the default card generation routine will take care + of missing new cards. + + due -- UNIX timestamp + interval -- days + factor -- decimal fraction (2.5 corresponds to default ease) + """ + + # TODO: support new and learning cards? due: int = 0 interval: int = 1 factor: float = STARTING_FACTOR_FRACTION diff --git a/rslib/src/import_export/text/import.rs b/rslib/src/import_export/text/import.rs index fd219ae2e..47d841242 100644 --- a/rslib/src/import_export/text/import.rs +++ b/rslib/src/import_export/text/import.rs @@ -99,7 +99,8 @@ impl<'a> Context<'a> { notetype: &Notetype, deck_id: DeckId, ) -> Result { - let (mut note, mut cards) = foreign.into_native(notetype, deck_id); + let today = self.col.timing_today()?.days_elapsed; + let (mut note, mut cards) = foreign.into_native(notetype, deck_id, today); self.import_note(&mut note, notetype)?; self.import_cards(&mut cards, note.id)?; self.generate_missing_cards(notetype, deck_id, ¬e)?; @@ -161,7 +162,8 @@ impl Collection { } impl ForeignNote { - fn into_native(self, notetype: &Notetype, deck_id: DeckId) -> (Note, Vec) { + fn into_native(self, notetype: &Notetype, deck_id: DeckId, today: u32) -> (Note, Vec) { + // TODO: Handle new and learning cards let mut note = Note::new(notetype); note.tags = self.tags; note.fields_mut() @@ -172,21 +174,27 @@ impl ForeignNote { .cards .into_iter() .enumerate() - .map(|(idx, c)| c.into_native(NoteId(0), idx as u16, deck_id)) + .map(|(idx, c)| c.into_native(NoteId(0), idx as u16, deck_id, today)) .collect(); (note, cards) } } impl ForeignCard { - fn into_native(self, note_id: NoteId, template_idx: u16, deck_id: DeckId) -> Card { - let mut card = Card::new(note_id, template_idx, deck_id, self.due); + fn into_native(self, note_id: NoteId, template_idx: u16, deck_id: DeckId, today: u32) -> Card { + let mut card = Card::new(note_id, template_idx, deck_id, self.native_due(today)); card.interval = self.interval; card.ease_factor = (self.factor * 1000.).round() as u16; card.reps = self.reps; card.lapses = self.lapses; card } + + fn native_due(self, today: u32) -> i32 { + let remaining_secs = self.interval as i64 - TimestampSecs::now().0; + let remaining_days = remaining_secs / (60 * 60 * 24); + 0.max(remaining_days as i32 + today as i32) + } } impl ForeignNotetype {