Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #91 (spec_date_to_iso8601) #93

Merged
merged 2 commits into from
Jun 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion silx/io/spech5.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def spec_date_to_iso8601(date, zone=None):
>>> spec_date_to_iso8601("Sat 2015/03/14 03:53:50")
'2015-03-14T03:53:50'
"""
months = ['Jan', 'Feb', 'Mar', 'Apr', 'Jun', 'Jul',
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

Expand Down
99 changes: 98 additions & 1 deletion silx/io/test/test_spech5.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
import sys
import tempfile
import unittest
import datetime
from functools import partial

from ..spech5 import SpecH5, SpecH5Group, SpecH5Dataset
from ..spech5 import (SpecH5, SpecH5Group,
SpecH5Dataset, spec_date_to_iso8601)

__authors__ = ["P. Knobel"]
__license__ = "MIT"
Expand Down Expand Up @@ -93,6 +96,98 @@
"""


class Test_spec_date(unittest.TestCase):
"""
Test of the spec_date_to_iso8601 function.
"""
# TODO : time zone tests
# TODO : error cases

@classmethod
def setUpClass(cls):
import locale
# FYI : not threadsafe
cls.locale_saved = locale.setlocale(locale.LC_TIME)
locale.setlocale(locale.LC_TIME, 'C')

@classmethod
def tearDownClass(cls):
import locale
# FYI : not threadsafe
locale.setlocale(locale.LC_TIME, cls.locale_saved)

def setUp(self):
# covering all week days
self.n_days = range(1, 10)
# covering all months
self.n_months = range(1, 13)

self.n_years = [1999, 2016, 2020]
self.n_seconds = [0, 5, 26, 59]
self.n_minutes = [0, 9, 42, 59]
self.n_hours = [0, 2, 17, 23]

self.formats = ['%a %b %d %H:%M:%S %Y', '%a %Y/%m/%d %H:%M:%S']

self.check_date_formats = partial(self.__check_date_formats,
year=self.n_years[0],
month=self.n_months[0],
day=self.n_days[0],
hour=self.n_hours[0],
minute=self.n_minutes[0],
second=self.n_seconds[0],
msg=None)

def __check_date_formats(self,
year,
month,
day,
hour,
minute,
second,
msg=None):
dt = datetime.datetime(year, month, day, hour, minute, second)
expected_date = dt.isoformat()

for i_fmt, fmt in enumerate(self.formats):
spec_date = dt.strftime(fmt)
iso_date = spec_date_to_iso8601(spec_date)
self.assertEqual(iso_date,
expected_date,
msg='Testing {0}. format={1}. '
'Expected "{2}", got "{3} ({4})" (dt={5}).'
''.format(msg,
i_fmt,
expected_date,
iso_date,
spec_date,
dt))

def test_years_nominal(self):
for year in self.n_years:
self.check_date_formats(year=year, msg='year')

def test_months_nominal(self):
for month in self.n_months:
self.check_date_formats(month=month, msg='month')

def test_days_nominal(self):
for day in self.n_days:
self.check_date_formats(day=day, msg='day')

def test_hours_nominal(self):
for hour in self.n_hours:
self.check_date_formats(hour=hour, msg='hour')

def test_minutes_nominal(self):
for minute in self.n_minutes:
self.check_date_formats(minute=minute, msg='minute')

def test_seconds_nominal(self):
for second in self.n_seconds:
self.check_date_formats(second=second, msg='second')


class TestSpecH5(unittest.TestCase):
@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -283,6 +378,8 @@ def suite():
test_suite = unittest.TestSuite()
test_suite.addTest(
unittest.defaultTestLoader.loadTestsFromTestCase(TestSpecH5))
test_suite.addTest(
unittest.defaultTestLoader.loadTestsFromTestCase(Test_spec_date))
return test_suite


Expand Down