2009-11-01 12:53:51 +08:00
|
|
|
from datetime import datetime, date
|
2010-09-28 16:18:12 +08:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
|
2009-11-01 12:53:51 +08:00
|
|
|
from django.utils.dateformat import format
|
|
|
|
from django.utils.tzinfo import FixedOffset, LocalTimezone
|
2009-05-08 21:39:37 +08:00
|
|
|
|
2010-09-28 16:18:12 +08:00
|
|
|
class DateFormatTests(unittest.TestCase):
|
2009-11-01 12:53:51 +08:00
|
|
|
def test_date(self):
|
|
|
|
d = date(2009, 5, 16)
|
|
|
|
self.assertEquals(date.fromtimestamp(int(format(d, 'U'))), d)
|
2009-05-08 21:39:37 +08:00
|
|
|
|
2009-11-01 12:53:51 +08:00
|
|
|
def test_naive_datetime(self):
|
|
|
|
dt = datetime(2009, 5, 16, 5, 30, 30)
|
|
|
|
self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U'))), dt)
|
2009-05-08 21:39:37 +08:00
|
|
|
|
2009-11-01 12:53:51 +08:00
|
|
|
def test_datetime_with_local_tzinfo(self):
|
|
|
|
ltz = LocalTimezone(datetime.now())
|
|
|
|
dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=ltz)
|
|
|
|
self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt)
|
|
|
|
self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U'))), dt.replace(tzinfo=None))
|
2009-05-08 21:39:37 +08:00
|
|
|
|
2009-11-01 12:53:51 +08:00
|
|
|
def test_datetime_with_tzinfo(self):
|
|
|
|
tz = FixedOffset(-510)
|
|
|
|
ltz = LocalTimezone(datetime.now())
|
|
|
|
dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=tz)
|
|
|
|
self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), tz), dt)
|
|
|
|
self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt)
|
|
|
|
self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U'))), dt.astimezone(ltz).replace(tzinfo=None))
|
|
|
|
self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), tz).utctimetuple(), dt.utctimetuple())
|
|
|
|
self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), ltz).utctimetuple(), dt.utctimetuple())
|
|
|
|
|
|
|
|
def test_epoch(self):
|
|
|
|
utc = FixedOffset(0)
|
|
|
|
udt = datetime(1970, 1, 1, tzinfo=utc)
|
|
|
|
self.assertEquals(format(udt, 'U'), u'0')
|