From 1c5ec91b64d01a53b005125a02c2944a9a9dbc05 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Thu, 12 Mar 2015 23:47:48 -0400 Subject: [PATCH] * Added method idatetime.join_values() . --- TESTS/test_datetime.py | 8 +++++++- datetime0_idt.py | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/TESTS/test_datetime.py b/TESTS/test_datetime.py index 27d9dc2..be754ed 100644 --- a/TESTS/test_datetime.py +++ b/TESTS/test_datetime.py @@ -6,7 +6,7 @@ from time import localtime, gmtime, strftime from wpylib.datetime0_idt import idatetime from wpylib.datetime import utime_to_iso - +from pprint import pprint def test_idt_01(): @@ -17,6 +17,12 @@ def test_idt_01(): idt = idatetime(iidt) print "integer value = %+18d" % idt.idt print "iso8601 format = %s" % idt.str_iso8601() + R = idt.split_values() + print("split values:") + pprint(R.__dict__) + + iidt_rejoined = idt.join_values(R) + print("iidt rejoined = %+18d" % iidt_rejoined) def test_u2i_01(): diff --git a/datetime0_idt.py b/datetime0_idt.py index 73c417d..01c3151 100644 --- a/datetime0_idt.py +++ b/datetime0_idt.py @@ -32,6 +32,7 @@ where: The time is always expressed in terms of UTC to eliminate ambiguity of time zone, daylight savings time, etc. CAVEAT: The resolution of the time is only on the order of millisecond. +Linux OS, for example, allows resolution down to nanoseconds! This is a 17-decimal-digit integer, which fits in a 64-bit range (9.22e+18). @@ -71,6 +72,26 @@ class idatetime(object): R.microsecond = R.millisecond * 1000 return R + def join_values(self, R=None): + """Joins the values contained in data structure 'R' to create a + composite integer date. + This is the converse of function 'split_values'. + """ + if R is None: + R = self.R + if hasattr(R, 'microsecond'): + millisecond = R.microsecond // 1000 + elif hasattr(R, 'millisecond'): + millisecond = R.millisecond + + return R.year * 10000000000000 \ + + R.month * 100000000000 \ + + R.day * 1000000000 \ + + R.hour * 10000000 \ + + R.minute * 100000 \ + + R.second * 1000 \ + + millisecond + def to_datetime(self): """Converts the object value to standard python datetime object. """