From ccc325f87b1aa1441dba947ed66a7aad06fe24b1 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 20 Mar 2011 06:06:28 +0900 Subject: [PATCH] remove utcOffset; make it a property of crt instead --- anki/deck.py | 29 +++++++++++++++-------------- anki/exporting.py | 2 +- anki/sched.py | 17 ++++------------- anki/storage.py | 19 +++++++++++++------ tests/test_cards.py | 2 +- tests/test_deck.py | 5 ++++- 6 files changed, 38 insertions(+), 36 deletions(-) diff --git a/anki/deck.py b/anki/deck.py index 997861a05..24e52b926 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -2,7 +2,7 @@ # Copyright: Damien Elmes # License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html -import time, os, random, re, stat, simplejson +import time, os, random, re, stat, simplejson, datetime from anki.lang import _, ngettext from anki.utils import parseTags, tidyHTML, ids2str, hexifyID, \ @@ -62,10 +62,12 @@ class _Deck(object): self.db = db self.path = db._path self.load() - if self.utcOffset == -2: - # shared deck; reset timezone and creation date - self.utcOffset = time.timezone + 60*60*4 - self.crt = intTime() + 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.undoEnabled = False self.sessionStartReps = 0 self.sessionStartTime = 0 @@ -86,15 +88,14 @@ class _Deck(object): def load(self): (self.crt, self.mod, - self.schema, + self.scm, self.syncName, self.lastSync, - self.utcOffset, self.qconf, self.conf, self.data) = self.db.first(""" -select crt, mod, schema, syncName, lastSync, -utcOffset, qconf, conf, data from deck""") +select crt, mod, scm, syncName, lastSync, +qconf, conf, data from deck""") self.qconf = simplejson.loads(self.qconf) self.conf = simplejson.loads(self.conf) self.data = simplejson.loads(self.data) @@ -104,10 +105,10 @@ utcOffset, qconf, conf, data from deck""") self.mod = intTime() self.db.execute( """update deck set -mod=?, schema=?, syncName=?, lastSync=?, utcOffset=?, +mod=?, scm=?, syncName=?, lastSync=?, qconf=?, conf=?, data=?""", - self.mod, self.schema, self.syncName, self.lastSync, - self.utcOffset, simplejson.dumps(self.qconf), + self.mod, self.scm, self.syncName, self.lastSync, + simplejson.dumps(self.qconf), simplejson.dumps(self.conf), simplejson.dumps(self.data)) def save(self): @@ -144,11 +145,11 @@ qconf=?, conf=?, data=?""", if not self.schemaDirty(): # next sync will be full self.emptyTrash() - self.schema = intTime() + self.scm = intTime() def schemaDirty(self): "True if schema changed since last sync, or syncing off." - return self.schema > self.lastSync + return self.scm > self.lastSync # Object creation helpers ########################################################################## diff --git a/anki/exporting.py b/anki/exporting.py index 2f0f968d3..253db5199 100644 --- a/anki/exporting.py +++ b/anki/exporting.py @@ -94,7 +94,7 @@ class AnkiExporter(Exporter): self.newDeck.rebuildCounts() # FIXME #self.exportedCards = self.newDeck.cardCount - self.newDeck.utcOffset = -1 + self.newDeck.crt = 0 self.newDeck.db.commit() self.newDeck.close() diff --git a/anki/sched.py b/anki/sched.py index 9fe32f22c..d2c18cbdf 100644 --- a/anki/sched.py +++ b/anki/sched.py @@ -448,19 +448,10 @@ queue = 2 %s and due <= :lim order by %s limit %d""" % ( ########################################################################## def _updateCutoff(self): - d = datetime.datetime.utcfromtimestamp( - time.time() - self.deck.utcOffset) + datetime.timedelta(days=1) - d = datetime.datetime(d.year, d.month, d.day) - newday = self.deck.utcOffset - time.timezone - d += datetime.timedelta(seconds=newday) - cutoff = time.mktime(d.timetuple()) - # cutoff must not be in the past - while cutoff < time.time(): - cutoff += 86400 - # cutoff must not be more than 24 hours in the future - cutoff = min(time.time() + 86400, cutoff) - self.dayCutoff = cutoff - self.today = int(cutoff/86400 - self.deck.crt/86400) + # days since deck created + self.today = int((time.time() - self.deck.crt) / 86400) + # end of day cutoff + self.dayCutoff = self.deck.crt + (self.today+1)*86400 def _checkDay(self): # check if the day has rolled over diff --git a/anki/storage.py b/anki/storage.py index 55cf57d4a..40cabefe6 100644 --- a/anki/storage.py +++ b/anki/storage.py @@ -4,7 +4,7 @@ CURRENT_VERSION = 100 -import os, time, simplejson, re +import os, time, simplejson, re, datetime from anki.lang import _ from anki.utils import intTime from anki.db import DB @@ -57,11 +57,10 @@ create table if not exists deck ( id integer primary key, crt integer not null, mod integer not null, + scm integer not null, ver integer not null, - schema integer not null, syncName text not null, lastSync integer not null, - utcOffset integer not null, qconf text not null, conf text not null, data text not null @@ -149,8 +148,8 @@ create table if not exists tags ( ); insert or ignore into deck -values(1,%(t)s,%(t)s,%(t)s,%(v)s,'',0,-2,'', '', ''); -""" % ({'t': intTime(), 'v':CURRENT_VERSION})) +values(1,0,0,0,%(v)s,'',0,'', '', ''); +""" % ({'v':CURRENT_VERSION})) import anki.deck import anki.groups # create a default group/configuration, which should not be removed @@ -355,7 +354,7 @@ def _migrateDeckTbl(db): db.execute(""" insert or replace into deck select id, cast(created as int), :t, :t, 99, ifnull(syncName, ""), cast(lastSync as int), -utcOffset, "", "", "" from decks""", t=intTime()) +"", "", "" from decks""", t=intTime()) # update selective study qconf = anki.deck.defaultQconf.copy() # delete old selective study settings, which we can't auto-upgrade easily @@ -484,7 +483,15 @@ def _fixupModels(deck): def _postSchemaUpgrade(deck): "Handle the rest of the upgrade to 2.0." import anki.deck + # adjust models _fixupModels(deck) + # fix creation time + d = datetime.datetime.today() + d -= datetime.timedelta(hours=4) + d = datetime.datetime(d.year, d.month, d.day) + d += datetime.timedelta(hours=4) + d -= datetime.timedelta(days=1+int((time.time()-deck.crt)/86400)) + deck.crt = int(time.mktime(d.timetuple())) # update uniq cache deck.updateFieldCache(deck.db.list("select id from facts")) # remove old views diff --git a/tests/test_cards.py b/tests/test_cards.py index ef79652f5..2309b1928 100644 --- a/tests/test_cards.py +++ b/tests/test_cards.py @@ -68,7 +68,7 @@ def test_delete(): deck.addFact(f) assert deck.cardCount() == 1 # mark the schema as clean - deck.lastSync = deck.schema + 1 + deck.lastSync = deck.scm + 1 # cards/facts should go in the deletion log instead cid = f.cards()[0].id deck.delCard(cid) diff --git a/tests/test_deck.py b/tests/test_deck.py index 8f4e9c2a1..e4cc240a4 100644 --- a/tests/test_deck.py +++ b/tests/test_deck.py @@ -1,6 +1,6 @@ # coding: utf-8 -import os, re +import os, re, datetime from tests.shared import assertException, getEmptyDeck, testDir from anki import Deck @@ -121,6 +121,9 @@ def test_upgrade(): print "upgrade to", dst shutil.copy(src, dst) deck = Deck(dst) + # creation time should have been adjusted + d = datetime.datetime.fromtimestamp(deck.crt) + assert d.hour == 4 and d.minute == 0 # 3 new, 2 failed, 1 due assert deck.sched.counts() == (3,2,1) # now's a good time to test the integrity check too