From 8a344d251d967246d5f99ea6379ab5ec39a3b0e0 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Sun, 23 Jun 2019 17:36:33 -0400 Subject: [PATCH] Python: more work on date/time and formatting of same. --- generate/template/astronomy.py | 5 +++++ generate/test.py | 12 ++++++++++-- source/python/astronomy.py | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/generate/template/astronomy.py b/generate/template/astronomy.py index ee703934..1cf42939 100644 --- a/generate/template/astronomy.py +++ b/generate/template/astronomy.py @@ -163,6 +163,11 @@ class astro_time_t: def AddDays(self, days): return astro_time_t(self.ut + days) + def __str__(self): + millis = round(self.ut * 86400000.0) + n = _EPOCH + datetime.timedelta(milliseconds=millis) + return '{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}.{:03d}Z'.format(n.year, n.month, n.day, n.hour, n.minute, n.second, math.floor(n.microsecond / 1000)) + _EPOCH = datetime.datetime(2000, 1, 1, 12) def CurrentTime(): diff --git a/generate/test.py b/generate/test.py index 110ede36..4bb90c1a 100644 --- a/generate/test.py +++ b/generate/test.py @@ -12,12 +12,20 @@ def Test_AstroTime(): if abs(diff) > 1.0e-12: print('Test_AstroTime: excessive UT error {}'.format(diff)) sys.exit(1) - diff = time.tt - expected_tt if abs(diff) > 1.0e-12: print('Test_AstroTime: excessive TT error {}'.format(diff)) sys.exit(1) - + time = astronomy.MakeTime(2018, 12, 31, 23, 59, 59.9994) + s = str(time) + if s != '2018-12-31T23:59:59.999Z': + print('Test_AstroTime: expected 2018-12-31T23:59:59.999Z but found {}'.format(s)) + sys.exit(1) + time = astronomy.MakeTime(2018, 12, 31, 23, 59, 59.9995) + s = str(time) + if s != '2019-01-01T00:00:00.000Z': + print('Test_AstroTime: expected 2019-01-01T00:00:00.000Z but found {}'.format(s)) + sys.exit(1) if len(sys.argv) == 2: if sys.argv[1] == 'time': diff --git a/source/python/astronomy.py b/source/python/astronomy.py index 5f624a69..69538da9 100644 --- a/source/python/astronomy.py +++ b/source/python/astronomy.py @@ -254,6 +254,11 @@ class astro_time_t: def AddDays(self, days): return astro_time_t(self.ut + days) + def __str__(self): + millis = round(self.ut * 86400000.0) + n = _EPOCH + datetime.timedelta(milliseconds=millis) + return '{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}.{:03d}Z'.format(n.year, n.month, n.day, n.hour, n.minute, n.second, math.floor(n.microsecond / 1000)) + _EPOCH = datetime.datetime(2000, 1, 1, 12) def CurrentTime():