[Pymoul-svn] SF.net SVN: pymoul: [32] pymoul/trunk/src/moul/time
Status: Alpha
Brought to you by:
tiran
|
From: <ti...@us...> - 2007-01-16 11:13:44
|
Revision: 32
http://pymoul.svn.sourceforge.net/pymoul/?rev=32&view=rev
Author: tiran
Date: 2007-01-16 03:13:44 -0800 (Tue, 16 Jan 2007)
Log Message:
-----------
* Created CavernTime calculator class
Modified Paths:
--------------
pymoul/trunk/src/moul/time/cavern.py
pymoul/trunk/src/moul/time/tests/__init__.py
Added Paths:
-----------
pymoul/trunk/src/moul/time/tests/test_cavern.py
Modified: pymoul/trunk/src/moul/time/cavern.py
===================================================================
--- pymoul/trunk/src/moul/time/cavern.py 2007-01-16 00:03:34 UTC (rev 31)
+++ pymoul/trunk/src/moul/time/cavern.py 2007-01-16 11:13:44 UTC (rev 32)
@@ -56,3 +56,107 @@
CAVERN_TZ_NAME = 'US/Mountain' # MDT / MST
CAVERN_TZ = timezone(CAVERN_TZ_NAME)
+def diffTD(td1, td2):
+ """Difference of two timedelta objects -> int
+
+ >>> type(diffTD(timedelta(0, 3600), timedelta(0, -3600)))
+ <type 'int'>
+ >>> diffTD(timedelta(0, 3600), timedelta(0, -3600))
+ 7200
+ >>> diffTD(timedelta(0, 3600), timedelta(0, 3600))
+ 0
+ >>> diffTD(timedelta(0, -3600), timedelta(0, -3600))
+ 0
+ >>> diffTD(timedelta(0, -7200), timedelta(0, -3600))
+ -3600
+ >>> diffTD(timedelta(0, -3600), timedelta(0, -7200))
+ 3600
+ >>> diffTD(timedelta(0, 3600, 1), timedelta(0, -3600))
+ Traceback (most recent call last):
+ ...
+ ValueError: Can't handle microseconds
+ """
+ if td1.microseconds or td2.microseconds:
+ raise ValueError("Can't handle microseconds")
+ return (td1.seconds + 86400 * td1.days) - (td2.seconds + 86400 * td2.days)
+
+def td2int(td):
+ """timedelta to int
+ >>> td2int(timedelta(0, 3600))
+ 3600
+ >>> td2int(timedelta(0, -3600))
+ -3600
+ """
+ return td.seconds + 86400 * td.days
+
+class CavernTime(object):
+ """Cavern time calculator
+
+ Calculates the cavern time and local time based on a given local time
+ zone. Call a CavernTime object:
+
+ utc
+ datetime -- Current time in UTC as <datetime> object
+ cavern
+ datetime -- Current time in cavern TZ as <datetime> object
+ tz -- Cavern time zone object
+ utcoffset -- (hours, fraction) offset from UTC
+ cavern
+ datetime -- Current time in local TZ as <datetime> object
+ tz -- Local time zone object
+ utcoffset -- (hours, fraction) offset from UTC
+
+ >>> ct = CavernTime(local="Europe/Berlin")
+ >>> result = ct()
+ >>> 'utc' in result, 'local' in result, 'cavern' in result
+ (True, True, True)
+ >>> coff = result['cavern']['utcoffset']
+ >>> coff == (-7, 0.0) or coff == (-8, 0.0) or coff
+ True
+ >>> loff = result['local']['utcoffset']
+ >>> loff == (1, 0.0) or loff == (2, 0.0) or loff
+ True
+ """
+ _timezones = TIMEZONE_NAMES
+ _cavern = CAVERN_TZ
+ _local = None
+
+ def __init__(self, local):
+ self.setLocal(local)
+
+ def setLocal(self, local):
+ """Set local time zone
+ """
+ self._local = timezone(local)
+
+ @property
+ def timezones(self):
+ """Available time zones as strings
+ """
+ return self._timezones
+
+ @staticmethod
+ def _utcnow():
+ """Get current time in UTC
+ """
+ return UTC.localize(datetime.utcnow())
+
+ @staticmethod
+ def _normalize(tz, utc_dt=None):
+ """Normalize a datetime object with UTC tz using another tz
+ """
+ if utc_dt is None:
+ utc_dt = self._utcnow()
+ return tz.normalize(utc_dt.astimezone(tz))
+
+ def __call__(self):
+ now = self._utcnow()
+ result = {}
+ for id, tz in (('cavern', self._cavern), ('local', self._local)):
+ info = result.setdefault(id, {})
+ utcoffset = td2int(tz.utcoffset(now))
+ info['tz'] = tz
+ info['utcoffset'] = int(utcoffset/3600), float((utcoffset%3600)/3600.0)
+ info['datetime'] = self._normalize(tz, now)
+ result['utc'] = {'datetime' : now}
+ return result
Modified: pymoul/trunk/src/moul/time/tests/__init__.py
===================================================================
--- pymoul/trunk/src/moul/time/tests/__init__.py 2007-01-16 00:03:34 UTC (rev 31)
+++ pymoul/trunk/src/moul/time/tests/__init__.py 2007-01-16 11:13:44 UTC (rev 32)
@@ -1,2 +1,2 @@
-# testing package
-
+# testing package
+
Added: pymoul/trunk/src/moul/time/tests/test_cavern.py
===================================================================
--- pymoul/trunk/src/moul/time/tests/test_cavern.py (rev 0)
+++ pymoul/trunk/src/moul/time/tests/test_cavern.py 2007-01-16 11:13:44 UTC (rev 32)
@@ -0,0 +1,30 @@
+# pyMoul - Python interface to Myst Online URU Live
+# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de>
+
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc., 59
+# Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+"""moul.time.cavern unit tests
+"""
+import os
+import unittest
+from doctest import DocTestSuite
+
+def test_suite():
+ return unittest.TestSuite((
+ DocTestSuite('moul.time.cavern'),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest="test_suite")
Property changes on: pymoul/trunk/src/moul/time/tests/test_cavern.py
___________________________________________________________________
Name: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|