[Pymoul-svn] SF.net SVN: pymoul: [164] pymoul/trunk/src/moul/time/dni.py
Status: Alpha
Brought to you by:
tiran
|
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.
|