Thread: [Pymoul-svn] SF.net SVN: pymoul: [161] pymoul/trunk/src/moul/time/dni.py
Status: Alpha
Brought to you by:
tiran
|
From: <ti...@us...> - 2007-02-12 17:19:23
|
Revision: 161
http://pymoul.svn.sourceforge.net/pymoul/?rev=161&view=rev
Author: tiran
Date: 2007-02-12 09:15:11 -0800 (Mon, 12 Feb 2007)
Log Message:
-----------
Better round()
Added method to calculate seconds to next bell
Modified Paths:
--------------
pymoul/trunk/src/moul/time/dni.py
Modified: pymoul/trunk/src/moul/time/dni.py
===================================================================
--- pymoul/trunk/src/moul/time/dni.py 2007-02-12 16:06:42 UTC (rev 160)
+++ pymoul/trunk/src/moul/time/dni.py 2007-02-12 17:15:11 UTC (rev 161)
@@ -34,10 +34,10 @@
>>> def compareDT(dt1, dt2):
... delta = td2sec(dt1 - dt2)
... # smaller than 5 seconds
-... if abs(delta) < 5:
+... if abs(delta) >0: # 1 off is fine
+... return delta
+... else:
... return True
-... else:
-... return delta
>>> dni = DniTime()
>>> dni.fromUTC(LEEFO_1_TABLE[1])
@@ -51,13 +51,20 @@
>>> other = datetime(2007, 2, 12, 13, 55, 10, 0, tzinfo=UTC)
>>> dni.fromUTC(other)
>>> dni.get()
-(9662, 9, 4, 4, 21, 24, 21)
+(9662, 9, 4, 4, 21, 24, 22)
>>> str(dni)
-'04:21:24:21, Leevotar 4, 9662'
+'04:21:24:22, Leevotar 4, 9662'
>>> compareDT(dni.toUTC(), other)
True
+>>> dni.bell
+24
+>>> dni.secondsToBell(25)
+2616
+>>> dni.secondsToBell(18)
+80964
+
"""
__author__ = "Christian Heimes"
__version__ = "$Id$"
@@ -185,17 +192,29 @@
if utc_dt is None:
utc_dt = utcnow()
sec = td2sec(utc_dt - BASE)
- prorahn = int(sec * FACTOR_PS)
+ prorahn = int(round(sec * FACTOR_PS))
self.set(hahr=BASE_HAHR, prorahn=prorahn)
def toUTC(self):
"""Convert to UTC datetime value
"""
hahr_sec = (self.hahr - BASE_HAHR) * SECONDS_PER_HAHR
- prorahn_sec = int(self._prorahn * FACTOR_SP)
+ prorahn_sec = int(round(self._prorahn * FACTOR_SP))
sec = hahr_sec + BASE_SEC + prorahn_sec
return UTC.localize(datetime.utcfromtimestamp(sec))
+ def secondsToBell(self, bell):
+ """Calculate seconds until bell
+ """
+ if bell < 1 or bell > 25:
+ raise ValueError("%s too large or small" % bell)
+ pro = self.prorahnOfYahr() # prorahn of day
+ probells = bell * 3125 # prorahn of start of bell
+ diff = probells - pro
+ if diff < 0:
+ diff = diff + 78125
+ return int(round(diff * FACTOR_SP))
+
def __str__(self):
"""00:00:00:00, Leefo 1, 9654
"""
@@ -228,10 +247,6 @@
self._addProrahn(value * 78125)
yahr = property(_getYahr, _setYahr)
- def _getBell(self):
- return (self.gahrtahvo // 5) % 25
- bell = property(_getBell)
-
def _getGahrtahvo(self):
return (self._prorahn // 15625) % 5
@valueCheck(int, 0, 5)
@@ -239,6 +254,10 @@
self._addProrahn(value * 15625)
gahrtahvo = property(_getGahrtahvo, _setGahrtahvo)
+ def _getBell(self):
+ return (self._prorahn // 3125) % 25
+ bell = property(_getBell)
+
def _getTahvo(self):
return (self._prorahn // 625) % 25
@valueCheck(int, 0, 25)
@@ -261,9 +280,15 @@
prorahn = property(_getProrahn, _setProrahn)
def _addProrahn(self, value):
- value = int(value)
+ value = int(round(value))
addhahr = value // PRORAHN_PER_HAHR
if addhahr:
self._hahr += addhahr
value = value - (addhahr * PRORAHN_PER_HAHR)
self._prorahn = value
+
+ def prorahnOfYahr(self):
+ """Get numbers of prorahntee since start of yahr
+ """
+ return (self.gahrtahvo * 15625 + self.tahvo * 625 +
+ self.gorahn * 25 + self.prorahn)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ti...@us...> - 2007-02-13 15:32:55
|
Revision: 164
http://pymoul.svn.sourceforge.net/pymoul/?rev=164&view=rev
Author: tiran
Date: 2007-02-13 07:32:47 -0800 (Tue, 13 Feb 2007)
Log Message:
-----------
Dni time cleanup
Added fromString method
Modified Paths:
--------------
pymoul/trunk/src/moul/time/dni.py
Modified: pymoul/trunk/src/moul/time/dni.py
===================================================================
--- pymoul/trunk/src/moul/time/dni.py 2007-02-13 03:16:06 UTC (rev 163)
+++ pymoul/trunk/src/moul/time/dni.py 2007-02-13 15:32:47 UTC (rev 164)
@@ -70,13 +70,24 @@
>>> compareDT(dni.toUTC(), other)
True
->>> dni.bell
+>>> dni.pahrtovo
24
->>> dni.secondsToBell(25)
+>>> dni.secondsToPahrtovo(25)
2616
->>> dni.secondsToBell(18)
+>>> dni.secondsToPahrtovo(18)
80964
+>>> CET = timezone('CET')
+>>> dni.fromString("2007-02-12 14:55:10", tzinfo=CET)
+>>> str(dni)
+'04:21:24:22, Leevotar 4, 9662'
+>>> dni.fromString("2007-02-12 6:55:10", tzinfo='MST')
+>>> str(dni)
+'04:21:24:22, Leevotar 4, 9662'
+>>> dni.fromString("2007-02-12 13:55:10")
+>>> str(dni)
+'04:21:24:22, Leevotar 4, 9662'
+
"""
__author__ = "Christian Heimes"
__version__ = "$Id$"
@@ -84,28 +95,13 @@
from datetime import datetime
from pytz import utc as UTC
+from pytz import timezone
from time import mktime
import sys
from moul.time.utils import td2sec
from moul.time.utils import utcnow
-
-FAHRAH_1 = -7656 # hahrtee fahrah 1 starts at April 21st 7656 B.C
-
-# name -> amount of smaller element, e.g. a hartee fahrah as 625 hahr
-DNI_FACTORS = (
- ('hahrtee fahrah', 625),
- ('hahr', 10), # year
- ('vai-lee', 29), # month
- ('yahr', 5), # day (~30h and 14min)
- # pahr-to-vo (bell) each yahr has 25 bells (~74 min)
- ('gahr-tah-vo', 25), # ~6h 3min
- ('tah-vo', 25), # ~14,5 min
- ('gor-ahn', 25), # ~34,8 seconds
- ('pro-rahn', None), # ~1.39 seconds
- )
-
# list of month names with approx. dates
VAILEETEE = (
'Leefo', # 1: April 21st to May 27th
@@ -120,15 +116,24 @@
'Leenovoo', # 10: March 16th to April 21st
)
-# 00:00:00:00, Leefo 1, 9654 DE
-BASE = datetime(1998, 4, 21, 10, 35, 18, 0, tzinfo=UTC)
+# 00:00:00:00, Leefo 1, 9654 DE = 10:35:18 1998/4/21
+BASE_GREGORIAN = datetime(1998, 4, 21, 10, 35, 18, 0, tzinfo=UTC)
+BASE_HAHR = 9654
+FAHRAH_1 = -7656 # hahrtee fahrah 1 starts ~April 21st 7656 B.C
# timestamp 0 - start of unix time
UNIX_0 = datetime(1970, 1, 1, 0, 0, 0, 0, tzinfo=UTC)
-BASE_SEC = td2sec(BASE-UNIX_0) # WTF? datetime has no totimestamp() method
-BASE_HAHR = 9654
-BASE_YEAR = 1998
+BASE_SEC = td2sec(BASE_GREGORIAN - UNIX_0) # WTF? no tottimestamp in datetime
+
+# factors
+HAHR_PER_HAHRTEE_FAHRAH = 625 # ~ 625 years
SECONDS_PER_HAHR = 31556925
-PRORAHN_PER_HAHR = 22656250
+PRORAHN_PER_HAHR = 22656250 # ~ 1 year, 25 * 25 * 25 * 5 * 29 * 10
+PRORAHN_PER_VAILEE = 2265625 # ~ 36 days, 25 * 25 * 25 * 5 * 29
+PRORAHN_PER_YAHR = 78125 # ~30h 14min, 25 * 25 * 25 * 5
+PRORAHN_PER_GAHRTAHVO = 15625 # ~6h 3min, 25 * 25 * 25
+PRORAHN_PER_PAHRTOVO = 3125 # ~74min, 25 * 25 * 5
+PRORAHN_PER_TAHVO = 625 # ~14,5 min, 25 * 25
+PRORAHN_PER_GORAHN = 25 # ~34,8 sec, 25
FACTOR_SP = float(SECONDS_PER_HAHR) / float(PRORAHN_PER_HAHR) # 1.39285737931
FACTOR_PS = float(PRORAHN_PER_HAHR) / float(SECONDS_PER_HAHR) # 0.717948596069
@@ -149,34 +154,32 @@
class DniTime(object):
"""D'ni time representation
-
+
The D'ni were using a complex calendar based on a pentovigesimal (25)
numbering system.
-
+
The DniTime class assumes the following rules:
-
+
- Hahrtee Farah 1 started on 21st of April 7656 B.C.
- Hahrtee Fahrah 15 started 1719 A.C.
- - A new hahr starts on 21st of April.
- - The reference time for the calculation is Mountain Standard Time (MST)
- without (!) DST. The UTC offset is always UTC-7.
- - To compensate leap years a new hahr starts on midnight (0:00am) in every
- forth year (year % 4 == 0).
- - To simplify the code special cases like year % 100 and year % 400 are
- NOT taken into account. I'm assuming a year has 365,25 days (which is
- wrong). A year according to the Gregorian Calendar has 365,2425 days.
-
+ - A new hahr starts around midnight 21st of April.
+ - Each D'ni year has a fixed length
+ - The exact start of a D'ni year is calculated relatively to
+ 00:00:00:00, Leefo 1, 9654 DE = 10:35:18 1998/4/21
+
Overview
--------
-
+
fahrah millenium 625 years
hahr year 1 year
vailee month 10 per year
yahr day 29 per vailee, about 30h 14min
gahrtahvo section about 6h, 3min
+ pahrtovo hour about 1h 14min
tahvo quarter about 14,5min
gorahn minute 36 seconds
prorahn second about 1.3929 seconds
+
"""
def __init__(self, hahr=0, vailee=0, yahr=0, gahrtahvo=0, tahvo=0,
gorahn=0, prorahn=0):
@@ -202,10 +205,20 @@
"""
if utc_dt is None:
utc_dt = utcnow()
- sec = td2sec(utc_dt - BASE)
+ sec = td2sec(utc_dt - BASE_GREGORIAN)
prorahn = int(round(sec * FACTOR_PS))
self.set(hahr=BASE_HAHR, prorahn=prorahn)
+ def fromString(self, s, fmt="%Y-%m-%d %H:%M:%S", tzinfo=UTC):
+ """Convert date from string to Dni Time
+ """
+ if isinstance(tzinfo, basestring):
+ tzinfo = timezone(tzinfo)
+ dt = datetime.strptime(s, fmt)
+ dt = tzinfo.localize(dt)
+ utc_dt = UTC.normalize(dt.astimezone(UTC))
+ return self.fromUTC(utc_dt)
+
def toUTC(self):
"""Convert to UTC datetime value
"""
@@ -242,45 +255,45 @@
hahr = property(_getHahr, _setHahr)
def _getVailee(self):
- return ((self._prorahn // 2265625) % 10) +1
+ return ((self._prorahn // PRORAHN_PER_VAILEE) % 10) +1
@valueCheck(int, 0, 10)
def _setVailee(self, value):
- self._addProrahn(value * 2265625)
+ self._addProrahn(value * PRORAHN_PER_VAILEE)
vailee = property(_getVailee, _setVailee)
def getVaileeName(self):
return VAILEETEE[self.vailee-1]
def _getYahr(self):
- return ((self._prorahn // 78125) % 29) +1
+ return ((self._prorahn // PRORAHN_PER_YAHR) % 29) +1
@valueCheck(int, 0, 29)
def _setYahr(self, value):
- self._addProrahn(value * 78125)
+ self._addProrahn(value * PRORAHN_PER_YAHR)
yahr = property(_getYahr, _setYahr)
def _getGahrtahvo(self):
- return (self._prorahn // 15625) % 5
+ return (self._prorahn // PRORAHN_PER_GAHRTAHVO) % 5
@valueCheck(int, 0, 5)
def _setGahrtahvo(self, value):
- self._addProrahn(value * 15625)
+ self._addProrahn(value * PRORAHN_PER_GAHRTAHVO)
gahrtahvo = property(_getGahrtahvo, _setGahrtahvo)
def _getPahrtovo(self):
- return (self._prorahn // 3125) % 25
+ return (self._prorahn // PRORAHN_PER_PAHRTOVO) % 25
pahrtovo = property(_getPahrtovo)
def _getTahvo(self):
- return (self._prorahn // 625) % 25
+ return (self._prorahn // PRORAHN_PER_TAHVO) % 25
@valueCheck(int, 0, 25)
def _setTahvo(self, value):
- self._addProrahn(value * 625)
+ self._addProrahn(value * PRORAHN_PER_TAHVO)
tahvo = property(_getTahvo, _setTahvo)
def _getGorahn(self):
- return (self._prorahn // 25) % 25
+ return (self._prorahn // PRORAHN_PER_GORAHN) % 25
@valueCheck(int, 0, 25)
def _setGorahn(self, value):
- self._addProrahn(value * 25)
+ self._addProrahn(value * PRORAHN_PER_GORAHN)
gorahn = property(_getGorahn, _setGorahn)
def _getProrahn(self):
@@ -301,5 +314,6 @@
def prorahnOfYahr(self):
"""Get numbers of prorahntee since start of yahr
"""
- return (self.gahrtahvo * 15625 + self.tahvo * 625 +
- self.gorahn * 25 + self.prorahn)
+ return (self.gahrtahvo * PRORAHN_PER_GAHRTAHVO +
+ self.tahvo * PRORAHN_PER_TAHVO +
+ self.gorahn * PRORAHN_PER_GORAHN + self.prorahn)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ti...@us...> - 2007-02-26 15:45:52
|
Revision: 194
http://pymoul.svn.sourceforge.net/pymoul/?rev=194&view=rev
Author: tiran
Date: 2007-02-26 07:45:48 -0800 (Mon, 26 Feb 2007)
Log Message:
-----------
Fixed bug in DniTime algorithm. Yahr and vailee start with 1 and not with 0
Modified Paths:
--------------
pymoul/trunk/src/moul/time/dni.py
Modified: pymoul/trunk/src/moul/time/dni.py
===================================================================
--- pymoul/trunk/src/moul/time/dni.py 2007-02-26 15:16:22 UTC (rev 193)
+++ pymoul/trunk/src/moul/time/dni.py 2007-02-26 15:45:48 UTC (rev 194)
@@ -23,8 +23,6 @@
official sources say that:
00:00:00:00, Leefo 1, 9654 DE = 10:35:18 UTC, April 21, 1998 CE
->>> from datetime import timedelta
-
>>> LEEFO_1_TABLE = [
... datetime(1998, 4, 21, 10, 35, 18, 0, tzinfo=UTC),
... datetime(1999, 4, 21, 16, 24, 3, 0, tzinfo=UTC),
@@ -36,9 +34,8 @@
>>> def compareDT(dt1, dt2):
... delta = td2sec(dt1 - dt2)
-... # smaller than 5 seconds
-... if abs(delta) >0: # 1 off is fine
-... return delta
+... if abs(delta) > 0:
+... return dt1 - dt2
... else:
... return True
@@ -67,6 +64,16 @@
>>> dni.secondsToPahrtovo(18)
80964
+>>> dni = DniTime.fromDni(9655, 1, 1, 0, 0, 0, 0)
+>>> compareDT(dni.toUTC(), LEEFO_1_TABLE[1])
+True
+>>> dni.toUTC()
+datetime.datetime(1999, 4, 21, 16, 24, 3, tzinfo=<UTC>)
+
+>>> dni = DniTime.fromDni(9662, 9, 4, 4, 21, 24, 22)
+>>> compareDT(dni.toUTC(), other)
+True
+
>>> CET = timezone('CET')
>>> dni = DniTime.fromString("2007-02-12 14:55:10", tzinfo=CET)
>>> str(dni)
@@ -85,6 +92,7 @@
import sys
from datetime import datetime
+from datetime import timedelta
from time import mktime
from moul.time.utils import UNIX_0
@@ -122,7 +130,7 @@
BASE_GREGORIAN = datetime(1998, 4, 21, 10, 35, 18, 0, tzinfo=UTC)
BASE_HAHR = 9654
FAHRAH_1 = -7656 # hahrtee fahrah 1 starts ~April 21st 7656 B.C
-BASE_SEC = td2sec(BASE_GREGORIAN - UNIX_0) # WTF? no tottimestamp in datetime
+#BASE_SEC = td2sec(BASE_GREGORIAN - UNIX_0) # WTF? no tottimestamp in datetime
# factors
HAHR_PER_HAHRTEE_FAHRAH = 625 # ~ 625 years
@@ -210,8 +218,7 @@
"""Constructor from D'ni time
"""
self = cls(_initialize=True)
- self.set(hahr=0, vailee=0, yahr=0, gahrtahvo=0, tahvo=0,
- gorahn=0, prorahn=0)
+ self.set(hahr, vailee, yahr, gahrtahvo, tahvo, gorahn, prorahn)
return self
@classmethod
@@ -241,7 +248,7 @@
utc_dt = UTC.normalize(dt.astimezone(UTC))
return cls.fromUTC(utc_dt)
- def set(self, hahr=0, vailee=0, yahr=0, gahrtahvo=0, tahvo=0,
+ def set(self, hahr=0, vailee=1, yahr=1, gahrtahvo=0, tahvo=0,
gorahn=0, prorahn=0):
self._hahr = 0
self._prorahn = 0
@@ -261,10 +268,11 @@
def toUTC(self):
"""Convert to UTC datetime value
"""
+ #import pdb; pdb.set_trace()
hahr_sec = (self.hahr - BASE_HAHR) * SECONDS_PER_HAHR
prorahn_sec = int(round(self._prorahn * FACTOR_SP))
- sec = hahr_sec + BASE_SEC + prorahn_sec
- return UTC.localize(datetime.utcfromtimestamp(sec))
+ td = timedelta(days=0, seconds=hahr_sec + prorahn_sec)
+ return UTC.normalize(BASE_GREGORIAN + td)
def secondsToPahrtovo(self, bell):
"""Calculate seconds until bell (pahr-to-vo)
@@ -295,9 +303,9 @@
def _getVailee(self):
return ((self._prorahn // PRORAHN_PER_VAILEE) % 10) +1
- @valueCheck(int, 0, 10)
+ @valueCheck(int, 1, 10)
def _setVailee(self, value):
- self._addProrahn(value * PRORAHN_PER_VAILEE)
+ self._addProrahn((value-1) * PRORAHN_PER_VAILEE)
vailee = property(_getVailee, _setVailee)
def getVaileeName(self):
@@ -305,9 +313,9 @@
def _getYahr(self):
return ((self._prorahn // PRORAHN_PER_YAHR) % 29) +1
- @valueCheck(int, 0, 29)
+ @valueCheck(int, 1, 29)
def _setYahr(self, value):
- self._addProrahn(value * PRORAHN_PER_YAHR)
+ self._addProrahn((value-1) * PRORAHN_PER_YAHR)
yahr = property(_getYahr, _setYahr)
def _getGahrtahvo(self):
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ti...@us...> - 2007-02-26 17:33:15
|
Revision: 195
http://pymoul.svn.sourceforge.net/pymoul/?rev=195&view=rev
Author: tiran
Date: 2007-02-26 09:33:11 -0800 (Mon, 26 Feb 2007)
Log Message:
-----------
Adjust min and max year
Modified Paths:
--------------
pymoul/trunk/src/moul/time/dni.py
Modified: pymoul/trunk/src/moul/time/dni.py
===================================================================
--- pymoul/trunk/src/moul/time/dni.py 2007-02-26 15:45:48 UTC (rev 194)
+++ pymoul/trunk/src/moul/time/dni.py 2007-02-26 17:33:11 UTC (rev 195)
@@ -85,6 +85,11 @@
>>> str(dni)
'04:21:24:22, Leevotar 4, 9662'
+>>> from datetime import MINYEAR
+>>> from datetime import MAXYEAR
+
+#>>> str(DniTime.fromUTC(datetime(MINYEAR, 1, 1, tzinfo=UTC)))
+#>>> str(DniTime.fromUTC(datetime(MAXYEAR, 12, 31, tzinfo=UTC)))
"""
__author__ = "Christian Heimes"
__version__ = "$Id$"
@@ -101,6 +106,7 @@
from moul.time.utils import timezone
from moul.time.utils import utcnow
+
# list of month names with approx. dates
VAILEETEE = (
'Leefo', # 1: April 21st to May 27th
@@ -130,7 +136,6 @@
BASE_GREGORIAN = datetime(1998, 4, 21, 10, 35, 18, 0, tzinfo=UTC)
BASE_HAHR = 9654
FAHRAH_1 = -7656 # hahrtee fahrah 1 starts ~April 21st 7656 B.C
-#BASE_SEC = td2sec(BASE_GREGORIAN - UNIX_0) # WTF? no tottimestamp in datetime
# factors
HAHR_PER_HAHRTEE_FAHRAH = 625 # ~ 625 years
@@ -296,7 +301,7 @@
def _getHahr(self):
return self._hahr
- @valueCheck(int, 0, sys.maxint)
+ @valueCheck(int, 7657, 17654) # datetime.MINYEAR / MAXYEAR
def _setHahr(self, value):
self._hahr = value
hahr = property(_getHahr, _setHahr)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|