diff --git a/src/tablib/packages/dbfpy/fields.py b/src/tablib/packages/dbfpy/fields.py index 9726d958..869bc5f5 100644 --- a/src/tablib/packages/dbfpy/fields.py +++ b/src/tablib/packages/dbfpy/fields.py @@ -1,4 +1,4 @@ -import datetime +import datetime as dt import struct from functools import total_ordering @@ -363,7 +363,7 @@ class DbfDateFieldDef(DbfFieldDef): """Definition of the date field.""" typeCode = "D" - defaultValue = utils.classproperty(lambda cls: datetime.date.today()) + defaultValue = utils.classproperty(lambda cls: dt.date.today()) # "yyyymmdd" gives us 8 characters length = 8 @@ -397,7 +397,7 @@ class DbfDateTimeFieldDef(DbfFieldDef): # and GDN (Gregorian Day Number). note, that GDN < JDN JDN_GDN_DIFF = 1721425 typeCode = "T" - defaultValue = utils.classproperty(lambda cls: datetime.datetime.now()) + defaultValue = utils.classproperty(lambda cls: dt.datetime.now()) # two 32-bits integers representing JDN and amount of # milliseconds respectively gives us 8 bytes. # note, that values must be encoded in LE byteorder. @@ -409,8 +409,8 @@ def decodeValue(self, value): # LE byteorder _jdn, _msecs = struct.unpack("<2I", value) if _jdn >= 1: - _rv = datetime.datetime.fromordinal(_jdn - self.JDN_GDN_DIFF) - _rv += datetime.timedelta(0, _msecs / 1000.0) + _rv = dt.datetime.fromordinal(_jdn - self.JDN_GDN_DIFF) + _rv += dt.timedelta(0, _msecs / 1000.0) else: # empty date _rv = None diff --git a/src/tablib/packages/dbfpy/header.py b/src/tablib/packages/dbfpy/header.py index 3da49b8a..edb77ab2 100644 --- a/src/tablib/packages/dbfpy/header.py +++ b/src/tablib/packages/dbfpy/header.py @@ -1,4 +1,4 @@ -import datetime +import datetime as dt import io import struct import sys @@ -254,7 +254,7 @@ def toString(self): def setCurrentDate(self): """Update ``self.lastUpdate`` field with current date value.""" - self.lastUpdate = datetime.date.today() + self.lastUpdate = dt.date.today() def __getitem__(self, item): """Return a field definition by numeric index or name string""" diff --git a/src/tablib/packages/dbfpy/utils.py b/src/tablib/packages/dbfpy/utils.py index 46b626a1..6195acad 100644 --- a/src/tablib/packages/dbfpy/utils.py +++ b/src/tablib/packages/dbfpy/utils.py @@ -1,4 +1,4 @@ -import datetime +import datetime as dt import time __version__ = "$Revision: 1.4 $"[11:-2] @@ -56,25 +56,25 @@ def getDate(date=None): """ if date is None: # use current value - return datetime.date.today() - if isinstance(date, datetime.date): + return dt.date.today() + if isinstance(date, dt.date): return date - if isinstance(date, datetime.datetime): + if isinstance(date, dt.datetime): return date.date() if isinstance(date, (int, float)): # date is a timestamp - return datetime.date.fromtimestamp(date) + return dt.date.fromtimestamp(date) if isinstance(date, str): date = date.replace(" ", "0") if len(date) == 6: # yymmdd - return datetime.date(*time.strptime(date, "%y%m%d")[:3]) + return dt.date(*time.strptime(date, "%y%m%d")[:3]) # yyyymmdd - return datetime.date(*time.strptime(date, "%Y%m%d")[:3]) + return dt.date(*time.strptime(date, "%Y%m%d")[:3]) if hasattr(date, "__getitem__"): # a sequence (assuming date/time tuple) - return datetime.date(*date[:3]) - return datetime.date.fromtimestamp(date.ticks()) + return dt.date(*date[:3]) + return dt.date.fromtimestamp(date.ticks()) def getDateTime(value=None): @@ -103,20 +103,20 @@ def getDateTime(value=None): """ if value is None: # use current value - return datetime.datetime.today() - if isinstance(value, datetime.datetime): + return dt.datetime.today() + if isinstance(value, dt.datetime): return value - if isinstance(value, datetime.date): - return datetime.datetime.fromordinal(value.toordinal()) + if isinstance(value, dt.date): + return dt.datetime.fromordinal(value.toordinal()) if isinstance(value, (int, float)): # value is a timestamp - return datetime.datetime.fromtimestamp(value) + return dt.datetime.fromtimestamp(value) if isinstance(value, str): raise NotImplementedError("Strings aren't currently implemented") if hasattr(value, "__getitem__"): # a sequence (assuming date/time tuple) - return datetime.datetime(*tuple(value)[:6]) - return datetime.datetime.fromtimestamp(value.ticks()) + return dt.datetime(*tuple(value)[:6]) + return dt.datetime.fromtimestamp(value.ticks()) class classproperty(property): diff --git a/tests/test_tablib.py b/tests/test_tablib.py index e783d414..ebaa80c5 100755 --- a/tests/test_tablib.py +++ b/tests/test_tablib.py @@ -1,7 +1,7 @@ #!/usr/bin/env python """Tests for Tablib.""" -import datetime +import datetime as dt import doctest import json import pickle @@ -300,8 +300,8 @@ def test_datetime_append(self): """Passes in a single datetime and a single date and exports.""" new_row = ( - datetime.datetime.now(), - datetime.datetime.today(), + dt.datetime.now(), + dt.datetime.today(), ) data.append(new_row) @@ -1104,7 +1104,7 @@ def test_tsv_export(self): class ODSTests(BaseTestCase): def test_ods_export_datatypes(self): - date_time = datetime.datetime(2019, 10, 4, 12, 30, 8) + date_time = dt.datetime(2019, 10, 4, 12, 30, 8) data.append(('string', '004', 42, 21.55, Decimal('34.5'), date_time)) data.headers = ('string', 'start0', 'integer', 'float', 'decimal', 'date/time') # ODS is currently write-only, just test that output doesn't crash. @@ -1122,7 +1122,7 @@ def test_xls_date_import(self): xls_source = Path(__file__).parent / 'files' / 'dates.xls' with xls_source.open('rb') as fh: dset = tablib.Dataset().load(fh, 'xls') - self.assertEqual(dset.dict[0]['birth_date'], datetime.datetime(2015, 4, 12, 0, 0)) + self.assertEqual(dset.dict[0]['birth_date'], dt.datetime(2015, 4, 12, 0, 0)) def test_xlsx_import_set_skip_lines(self): data.append(('garbage', 'line', '')) @@ -1160,7 +1160,7 @@ def test_xlsx_format_detect(self): self.assertEqual(detect_format(in_stream), 'xlsx') def test_xlsx_import_set(self): - date_time = datetime.datetime(2019, 10, 4, 12, 30, 8) + date_time = dt.datetime(2019, 10, 4, 12, 30, 8) data.append(('string', '004', 42, 21.55, date_time)) data.headers = ('string', 'start0', 'integer', 'float', 'date/time') _xlsx = data.xlsx diff --git a/tests/test_tablib_dbfpy_packages_utils.py b/tests/test_tablib_dbfpy_packages_utils.py index ce40c4e7..df88795c 100644 --- a/tests/test_tablib_dbfpy_packages_utils.py +++ b/tests/test_tablib_dbfpy_packages_utils.py @@ -1,7 +1,7 @@ #!/usr/bin/env python """Tests for tablib.packages.dbfpy.""" -import datetime +import datetime as dt import unittest from tablib.packages.dbfpy import utils @@ -42,28 +42,28 @@ def test_getDate_none(self): output = utils.getDate(value) # Assert - self.assertIsInstance(output, datetime.date) + self.assertIsInstance(output, dt.date) def test_getDate_datetime_date(self): # Arrange - value = datetime.date(2019, 10, 19) + value = dt.date(2019, 10, 19) # Act output = utils.getDate(value) # Assert - self.assertIsInstance(output, datetime.date) + self.assertIsInstance(output, dt.date) self.assertEqual(output, value) def test_getDate_datetime_datetime(self): # Arrange - value = datetime.datetime(2019, 10, 19, 12, 00, 00) + value = dt.datetime(2019, 10, 19, 12, 00, 00) # Act output = utils.getDate(value) # Assert - self.assertIsInstance(output, datetime.date) + self.assertIsInstance(output, dt.date) self.assertEqual(output, value) def test_getDate_datetime_timestamp(self): @@ -74,8 +74,8 @@ def test_getDate_datetime_timestamp(self): output = utils.getDate(value) # Assert - self.assertIsInstance(output, datetime.date) - self.assertEqual(output, datetime.date(2019, 10, 19)) + self.assertIsInstance(output, dt.date) + self.assertEqual(output, dt.date(2019, 10, 19)) def test_getDate_datetime_string_yyyy_mm_dd(self): # Arrange @@ -85,8 +85,8 @@ def test_getDate_datetime_string_yyyy_mm_dd(self): output = utils.getDate(value) # Assert - self.assertIsInstance(output, datetime.date) - self.assertEqual(output, datetime.date(2019, 10, 19)) + self.assertIsInstance(output, dt.date) + self.assertEqual(output, dt.date(2019, 10, 19)) def test_getDate_datetime_string_yymmdd(self): # Arrange @@ -96,8 +96,8 @@ def test_getDate_datetime_string_yymmdd(self): output = utils.getDate(value) # Assert - self.assertIsInstance(output, datetime.date) - self.assertEqual(output, datetime.date(2019, 10, 19)) + self.assertIsInstance(output, dt.date) + self.assertEqual(output, dt.date(2019, 10, 19)) class UtilsGetDateTimeTestCase(unittest.TestCase): @@ -111,29 +111,29 @@ def test_getDateTime_none(self): output = utils.getDateTime(value) # Assert - self.assertIsInstance(output, datetime.datetime) + self.assertIsInstance(output, dt.datetime) def test_getDateTime_datetime_datetime(self): # Arrange - value = datetime.datetime(2019, 10, 19, 12, 00, 00) + value = dt.datetime(2019, 10, 19, 12, 00, 00) # Act output = utils.getDateTime(value) # Assert - self.assertIsInstance(output, datetime.date) + self.assertIsInstance(output, dt.date) self.assertEqual(output, value) def test_getDateTime_datetime_date(self): # Arrange - value = datetime.date(2019, 10, 19) + value = dt.date(2019, 10, 19) # Act output = utils.getDateTime(value) # Assert - self.assertIsInstance(output, datetime.date) - self.assertEqual(output, datetime.datetime(2019, 10, 19, 00, 00)) + self.assertIsInstance(output, dt.date) + self.assertEqual(output, dt.datetime(2019, 10, 19, 00, 00)) def test_getDateTime_datetime_timestamp(self): # Arrange @@ -143,7 +143,7 @@ def test_getDateTime_datetime_timestamp(self): output = utils.getDateTime(value) # Assert - self.assertIsInstance(output, datetime.datetime) + self.assertIsInstance(output, dt.datetime) def test_getDateTime_datetime_string(self): # Arrange