Skip to content
Snippets Groups Projects
Commit 78e0090a authored by Karl-Hermann Wieners's avatar Karl-Hermann Wieners
Browse files

python: add missing methods to Date/Time/Delta objects

* expose attributes (year, month, etc.)
* fix binding mismatch and encoding for TimeDelta.sign
parent 6f505869
No related tags found
1 merge request!84Add missing methods to Python Date/Time/Delta objects
Pipeline #97790 passed
...@@ -59,6 +59,9 @@ class Date(MTime): ...@@ -59,6 +59,9 @@ class Date(MTime):
def __str__(self): def __str__(self):
return libmtime.dateToString(self._my).decode() return libmtime.dateToString(self._my).decode()
def __getattr__(self, name):
return getattr(self._my.contents, name)
class Time(MTime): class Time(MTime):
def __init__(self, spec): def __init__(self, spec):
...@@ -74,6 +77,9 @@ class Time(MTime): ...@@ -74,6 +77,9 @@ class Time(MTime):
def __str__(self): def __str__(self):
return libmtime.timeToString(self._my).decode() return libmtime.timeToString(self._my).decode()
def __getattr__(self, name):
return getattr(self._my.contents, name)
class DateTime(MTime): class DateTime(MTime):
def __init__(self, spec): def __init__(self, spec):
...@@ -113,9 +119,21 @@ class DateTime(MTime): ...@@ -113,9 +119,21 @@ class DateTime(MTime):
) )
return result return result
def __getattr__(self, name):
try:
return getattr(self._my.contents.date, name)
except AttributeError:
return getattr(self._my.contents.time, name)
def data(self): def data(self):
return self._my return self._my
def items(self):
for name, ctype in self._my.contents.date._fields_:
yield (name, getattr(self._my.contents.date, name))
for name, ctype in self._my.contents.time._fields_:
yield (name, getattr(self._my.contents.time, name))
@property @property
def date(self): def date(self):
return Date( return Date(
...@@ -162,9 +180,17 @@ class TimeDelta(MTime): ...@@ -162,9 +180,17 @@ class TimeDelta(MTime):
) )
return result.contents.quotient return result.contents.quotient
def __getattr__(self, name):
return getattr(self._my.contents, name)
def data(self): def data(self):
return self._my return self._my
def items(self): def items(self):
for name, ctype in self._my.contents._fields_: for name, ctype in self._my.contents._fields_:
yield (name, self._my.contents.__getattribute__(name)) if not name.startswith("_"):
yield (name, getattr(self, name))
@property
def sign(self):
return self._my.contents.sign.decode()
...@@ -461,6 +461,7 @@ max_timedelta_str_len = c_int(32).value ...@@ -461,6 +461,7 @@ max_timedelta_str_len = c_int(32).value
class _timedelta(Structure): class _timedelta(Structure):
_fields_ = [ _fields_ = [
("_flag_std_form", c_int),
("sign", c_char), ("sign", c_char),
("year", c_long), ("year", c_long),
("month", c_int), ("month", c_int),
......
...@@ -21,52 +21,72 @@ exec @PYTHON@ "$0" ...@@ -21,52 +21,72 @@ exec @PYTHON@ "$0"
":""" ":"""
import mtime import mtime
# Test initialization and time delta arithmetics # Setup, testing initialization and time delta arithmetics
print("-" * 80) initial_date = mtime.DateTime("1850-01-23T12:34:56.789")
initial_date = mtime.DateTime("1850-01-01")
start_date = initial_date + mtime.TimeDelta("P1Y") start_date = initial_date + mtime.TimeDelta("P1Y")
print(initial_date) timestep = mtime.TimeDelta("PT15M")
print(start_date) next_date = start_date + mtime.TimeDelta("P1Y")
after_date = next_date + mtime.TimeDelta("P1Y")
# Test before/after tests on datetime objects
print("-" * 80) print("-" * 80)
test_result = start_date == start_date tests = ""
print(str(start_date) + " == " + str(start_date) + ": " + str(test_result))
assert test_result
test_result = start_date > initial_date
print(str(start_date) + " > " + str(initial_date) + ": " + str(test_result))
assert test_result
test_result = start_date == initial_date # Test string conversion and show setup values
print(str(start_date) + " == " + str(initial_date) + ": " + str(test_result)) tests += """
assert not test_result str(initial_date) == "1850-01-23T12:34:56.789"
str(start_date) == "1851-01-23T12:34:56.789"
str(timestep) == "PT15M"
str(next_date) == "1852-01-23T12:34:56.789"
str(after_date) == "1853-01-23T12:34:56.789"
"""
test_result = start_date < initial_date # Test before/after tests on datetime objects
print(str(start_date) + " < " + str(initial_date) + ": " + str(test_result)) tests += """
assert not test_result start_date == start_date
start_date > initial_date
not (start_date == initial_date)
not (start_date < initial_date)
"""
# Test division by delta # Test division by delta
print("-" * 80) tests += """
timestep = mtime.TimeDelta("PT15M") (start_date, initial_date)//timestep == 35040
test_result = (start_date, initial_date) // timestep (after_date, next_date)//timestep == 35136
print( """
"({0}, {1})//{2}: {3}".format(
start_date, # Test attribute selection, also for date and time only parts
initial_date, tests += """
timestep, str(start_date.date) == "1851-01-23"
test_result, str(start_date.time) == "12:34:56.789"
) start_date.year == 1851
) start_date.month == 1
assert test_result == 35040 start_date.day == 23
start_date.hour == 12
next_date = start_date + mtime.TimeDelta("P1Y") start_date.minute == 34
after_date = next_date + mtime.TimeDelta("P1Y") start_date.second == 56
test_result = (after_date, next_date) // timestep start_date.ms == 789
print( start_date.date.year == 1851
"({0}, {1})//{2}: {3}".format(after_date, next_date, timestep, test_result) start_date.date.month == 1
) start_date.date.day == 23
assert test_result == 35136 start_date.time.hour == 12
start_date.time.minute == 34
start_date.time.second == 56
start_date.time.ms == 789
timestep.sign == "+"
timestep.year == 0
timestep.month == 0
timestep.day == 0
timestep.hour == 0
timestep.minute == 15
timestep.second == 0
timestep.ms == 0
"""
test_result = False
for test in filter(None, tests.split("\n")):
test_result = eval(test)
print("{0}: {1}".format(test, test_result))
assert test_result
# Test re-initializing of calendar, showing Feb 28th and successing days # Test re-initializing of calendar, showing Feb 28th and successing days
...@@ -124,17 +144,3 @@ assert next_day == mtime.DateTime("1852-02-30") ...@@ -124,17 +144,3 @@ assert next_day == mtime.DateTime("1852-02-30")
next_day += mtime.TimeDelta("P1D") next_day += mtime.TimeDelta("P1D")
print(next_day) print(next_day)
assert next_day == mtime.DateTime("1852-03-01") assert next_day == mtime.DateTime("1852-03-01")
# Test selection and string conversion for date and time only parts
print("-" * 80)
test_result = str(start_date)
print(test_result)
assert test_result == "1851-01-01T00:00:00.000"
test_result = str(start_date.date)
print(test_result)
assert test_result == "1851-01-01"
test_result = str(start_date.time)
print(test_result)
assert test_result == "00:00:00.000"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment