def test_timestamp_cmp(self): t1 = kron.timestamp(1457128501) t2 = kron.timestamp(1457128501.987349) t3 = kron.timestamp(1457128501.987349) t4 = kron.timestamp(1457128502) # t1 < t2 == t3 < t4 self.assertLess(t1, t2) # t1 < t2 self.assertLess(t1, t4) # t1 < t4 self.assertLessEqual(t1, t2) # t1 <= t2 self.assertLessEqual(t1, t4) # t1 <= t4 self.assertNotEqual(t1, t2) # t1 != t2 self.assertNotEqual(t1, t4) # t1 != t4 self.assertGreater(t4, t3) # t4 > t3 self.assertGreater(t4, t1) # t4 > t1 self.assertGreaterEqual(t4, t3) # t4 >= t3 self.assertGreaterEqual(t4, t1) # t4 >= t1 self.assertNotEqual(t4, t3) # t4 != t3 self.assertNotEqual(t4, t1) # t4 != t1 self.assertEqual(t2, t3) # t2 == t3 self.assertLessEqual(t2, t3) # t2 <= t3 self.assertGreaterEqual(t2, t3) # t2 >= t3 def cmp_int(i): t1 > i self.assertRaises(kron.TimestampComparisonError, cmp_int, 6) def cmp_float(f): t1 < f self.assertRaises(kron.TimestampComparisonError, cmp_float, 7.2) def cmp_str(s): t1 == s self.assertRaises(kron.TimestampComparisonError, cmp_str, 'asdf')
def test_timestamp_math(self): t1 = kron.timestamp(1457128501) t2 = kron.timestamp(1457128501.987349) t3 = kron.timestamp(1457128501.987349) t4 = kron.timestamp(1457128502) i = 1 f = 0.987349 # t1 < t2 == t3 < t4 self.assertEqual(t1 + (t2 - t1), t3) # t1 + (t2 - t1) = t3 self.assertEqual(t1 + f, t2) # t1 + f = t2 self.assertEqual(t1 + i, t4) # t1 + i = t4 self.assertEqual(t4 - (t4 - t3), t2) # t4 - (t4 - t3) = t2 self.assertEqual(t4 - (1 - f), t3) # t4 - (1 - f) = t3 self.assertEqual(t4 - i, t1) # t4 - i = t1 # t1 * any raises def mul_ts(ts, y): ts * y self.assertRaises(kron.TimestampMultiplyError, mul_ts, t1, t2) self.assertRaises(kron.TimestampMultiplyError, mul_ts, t1, i) self.assertRaises(kron.TimestampMultiplyError, mul_ts, t1, f) # t1 / any raises def div_ts(ts, y): ts / y self.assertRaises(kron.TimestampDivideError, div_ts, t1, t2) self.assertRaises(kron.TimestampDivideError, div_ts, t1, i) self.assertRaises(kron.TimestampDivideError, div_ts, t1, f)
def test_timestamp_math(self): t1 = kron.timestamp(1457128501) t2 = kron.timestamp(1457128501.987349) t3 = kron.timestamp(1457128501.987349) t4 = kron.timestamp(1457128502) i = 1 f = 0.987349 # t1 < t2 == t3 < t4 self.assertEqual(t1 + (t2 - t1), t3) # t1 + (t2 - t1) = t3 self.assertEqual(t1 + f, t2) # t1 + f = t2 self.assertEqual(t1 + i, t4) # t1 + i = t4 self.assertEqual(t4 - (t4 - t3), t2) # t4 - (t4 - t3) = t2 self.assertEqual(t4 - (1 - f), t3) # t4 - (1 - f) = t3 self.assertEqual(t4 - i, t1) # t4 - i = t1 # t1 * any raises def mul_ts(ts, y): ts * y self.assertRaises(kron.TimestampMultiplyError, mul_ts, t1, t2) self.assertRaises(kron.TimestampMultiplyError, mul_ts, t1, i) self.assertRaises(kron.TimestampMultiplyError, mul_ts, t1, f) # t1 / any raises def div_ts(ts, y): ts / y self.assertRaises(kron.TimestampDivideError, div_ts, t1, t2) self.assertRaises(kron.TimestampDivideError, div_ts, t1, i) self.assertRaises(kron.TimestampDivideError, div_ts, t1, f) def sub_str(): t1 - 'impossible' self.assertRaises(kron.TimestampSubtractError, sub_str) def add_str(): t1 + 'impossible' self.assertRaises(kron.TimestampAddError, add_str)
def test_timestamp_default(self): h = kron.timestamp() self.assertIsInstance(h.value, float) r = r'^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d UTC$' if sys.version_info >= (3,): self.assertRegex(h.str('UTC'), r) else: self.assertRegexpMatches(h.str('UTC'), r)
def test_timestamp_utc(self): h = kron.timestamp(1457128501) w = '2016-03-04 21:55:01 UTC' self.assertEqual(h.str('UTC'), w) self.assertEqual(h.utc(), w) self.assertEqual(h.utc('basetz'), w) w = '2016-03-04 21:55:01' self.assertEqual(h.utc('base'), w)
def test_timestamp_default(self): h = kron.timestamp() self.assertIsInstance(h.value, float) r = r'^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d UTC$' if sys.version_info >= (3, ): self.assertRegex(h.str('UTC'), r) else: self.assertRegexpMatches(h.str('UTC'), r)
def test_timestamp_float(self): w = 1457128501.987349 h = kron.timestamp(w) self.assertEqual(h.value, w) self.assertIsInstance(h.value, float) w = dict( EST='2016-03-04 16:55:01', UTC='2016-03-04 21:55:01', CET='2016-03-04 22:55:01', ) for tz, v in list(w.items()): self.assertEqual(h.str(tz), v + ' ' + tz) self.assertEqual(h.str(tz, 'basetz'), v + ' ' + tz) self.assertEqual(h.str(tz, 'base'), v)
def test_timestamp_float(self): w = 1457128501.987349 h = kron.timestamp(w) self.assertEqual(h.value, w) self.assertIsInstance(h.value, float) w = dict( UTC='2016-03-04 16:55:01', EST='2016-03-04 11:55:01', CET='2016-03-04 17:55:01', ) for tz, v in w.items(): self.assertEqual(h.str(tz), v + ' ' + tz) self.assertEqual(h.str(tz, 'local'), v + ' ' + tz) self.assertEqual(h.str(tz, 'base'), v)
def test_timestamp_int(self): w = 1457128501 h = kron.timestamp(w) self.assertEqual(h.value, w) self.assertIsInstance(h.value, float) w = dict( EST='2016-03-04 16:55:01', UTC='2016-03-04 21:55:01', CET='2016-03-04 22:55:01', ) for tz, v in list(w.items()): self.assertEqual(h.str(tz), v + ' ' + tz) self.assertEqual(h.str(tz, 'basetz'), v + ' ' + tz) self.assertEqual(h.str(tz, 'base'), v)
def test_timestamp_int(self): w = 1457128501 h = kron.timestamp(w) self.assertEqual(h.value, w) self.assertIsInstance(h.value, float) w = dict( UTC='2016-03-04 16:55:01', EST='2016-03-04 11:55:01', CET='2016-03-04 17:55:01', ) for tz, v in w.items(): self.assertEqual(h.str(tz), v + ' ' + tz) self.assertEqual(h.str(tz, 'local'), v + ' ' + tz) self.assertEqual(h.str(tz, 'base'), v)
def test_duration_math(self): d1 = kron.duration(1111111.111) d2 = kron.duration(2222222.222) d3 = kron.duration(2222222.222) d4 = kron.duration(3333333.333) d5 = kron.duration(1111116.111) d6 = kron.duration(1111106.111) d7 = kron.duration(5555555.555) d8 = kron.duration(2469135801975.3086) t1 = kron.timestamp(1457128501) t2 = d1 + t1 i = 5 f = 2222222.222 self.assertEqual(d1 + d2, d4) # d1 + d2 = d4 self.assertEqual(d1 + i, d5) # d1 + i = d5 self.assertEqual(d1 + f, d4) # d1 + f = d4 self.assertEqual(d4 - d3, d1) # d4 - d3 = d1 self.assertEqual(d1 - i, d6) # d1 - i = d6 self.assertEqual(d4 - f, d1) # d4 - f = d1 self.assertEqual(d1 * i, d7) # d1 * i = d7 self.assertEqual(d1 * f, d8) # d1 * f = d8 self.assertEqual(d7 / i, d1) # d7 / i = d1 self.assertEqual(d8 / f, d1) # d8 / f = d1 def multiply_durations(): return d1 * d2 self.assertRaises(kron.DurationMultiplyError, multiply_durations) def divide_durations(): return d1 / d2 self.assertRaises(kron.DurationDivideError, divide_durations) def add_duration_string(): return d1 + 'impossible' self.assertRaises(kron.DurationAddError, add_duration_string) def subtract_duration_string(): return d1 - 'impossible' self.assertRaises(kron.DurationSubtractError, subtract_duration_string) self.assertIsInstance(t2, kron.timestamp) self.assertEqual(d1.value + t1.value, t2.value) def subtract_duration_timestamp(): return d1 - t2 self.assertRaises(kron.DurationSubtractError, \ subtract_duration_timestamp)
def test_timestamp_json(self): h = kron.timestamp(1457128501) w = {} w['UTC'] = dict(local='2016-03-04 16:55:01 UTC', ) self.assertEqual(h.json('UTC'), kron._json(w)) w['UTC']['base'] = '2016-03-04 16:55:01' w['EST'] = dict( base='2016-03-04 11:55:01', local='2016-03-04 11:55:01 EST', ) w['CET'] = dict( base='2016-03-04 17:55:01', local='2016-03-04 17:55:01 CET', ) self.assertEqual(h.json(w.keys(), ['base', 'local']), kron._json(w))
def test_timestamp_json(self): h = kron.timestamp(1457128501) w = {} w['UTC'] = dict( local='2016-03-04 16:55:01 UTC', ) self.assertEqual(h.json('UTC'), kron._json(w)) w['UTC']['base'] = '2016-03-04 16:55:01' w['EST'] = dict( base='2016-03-04 11:55:01', local='2016-03-04 11:55:01 EST', ) w['CET'] = dict( base='2016-03-04 17:55:01', local='2016-03-04 17:55:01 CET', ) self.assertEqual(h.json(w.keys(), ['base', 'local']), kron._json(w))
def test_timestamp_dict(self): h = kron.timestamp(1457128501) w = {} w['UTC'] = dict(basetz='2016-03-04 21:55:01 UTC', ) self.assertEqual(h.dict('UTC'), w) w['UTC']['base'] = '2016-03-04 21:55:01' self.assertEqual(h.dict('UTC', ['base', 'basetz']), w) self.assertEqual(h.dict('UTC', ['basetz', 'base']), w) w['EST'] = dict( base='2016-03-04 16:55:01', basetz='2016-03-04 16:55:01 EST', ) self.assertEqual(h.dict(['EST', 'UTC'], ['base', 'basetz']), w) self.assertEqual(h.dict(['UTC', 'EST'], ['basetz', 'base']), w) w['CET'] = dict( base='2016-03-04 22:55:01', basetz='2016-03-04 22:55:01 CET', ) self.assertEqual(h.dict(list(w.keys()), ['base', 'basetz']), w)
def test_timestamp_dict(self): h = kron.timestamp(1457128501) w = {} w['UTC'] = dict( basetz='2016-03-04 21:55:01 UTC', ) self.assertEqual(h.dict('UTC'), w) w['UTC']['base'] = '2016-03-04 21:55:01' self.assertEqual(h.dict('UTC', ['base', 'basetz']), w) self.assertEqual(h.dict('UTC', ['basetz', 'base']), w) w['EST'] = dict( base='2016-03-04 16:55:01', basetz='2016-03-04 16:55:01 EST', ) self.assertEqual(h.dict(['EST', 'UTC'], ['base', 'basetz']), w) self.assertEqual(h.dict(['UTC', 'EST'], ['basetz', 'base']), w) w['CET'] = dict( base='2016-03-04 22:55:01', basetz='2016-03-04 22:55:01 CET', ) self.assertEqual(h.dict(list(w.keys()), ['base', 'basetz']), w)
def test_time_ntp_with_network(self): self.assertIsInstance(kron.time_ntp(), float) for s in additional_ntp_servers: self.assertIsInstance(kron.time_ntp(s), float) self.assertIsInstance(kron.time(ntp=True), float) self.assertIsInstance(kron.timestamp(ntp=True), kron.timestamp)
def test_timestamp_formats(self): w = 1457139301.123456 h = kron.timestamp(w) tz = 'CST' self.assertEqual(h.value, w) self.assertIsInstance(h.value, float) w = dict( base='2016-03-04 13:55:01', local='2016-03-04 13:55:01 CST', HH='13', HH_MM='13:55', HH_MM_SS='13:55:01', HHMM='1355', HHMMSS='135501', MM='55', SS='01', abbr_date='Fri, Mar 04, 2016', abbr_month='Mar', abbr_weekday='Fri', ampm='PM', ccyy='2016', ccyymm='201603', ccyymmdd='20160304', date='Friday, March 04, 2016', dd='04', hh='01', hh_MM='01:55', hh_MM_ampm='01:55 PM', hh_MM_SS='01:55:01', hh_MM_SS_ampm='01:55:01 PM', hours='13', hours12='01', hours24='13', iso8601='2016-03-04T19:55:01Z', julian='064', microseconds='123456', minutes='55', mm='03', mm_dd_yy='03/04/16', mmdd='0304', mon='Mar', month='March', national='Fri Mar 04 13:55:01 CST 2016', national_date='03/04/16', national_time='13:55:01', rfc2822='Fri, 04 Mar 2016 13:55:01 -0600', seconds='01', tz='CST', tz_offset='-0600', week_number_mon='09', week_number_sun='09', weekday='Friday', year='2016', yy='16', yymm='1603', yymmdd='160304', yyyy='2016', yyyy_mm_dd='2016/03/04', yyyymm='201603', yyyymmdd='20160304', ) for fmt, v in w.items(): self.assertEqual(h.str(tz, fmt), v)
def test_timestamp_str4(self): v = '1457128501' h = kron.timestamp(v) self.assertEqual(h.value, float(v)) self.assertIsInstance(h.value, float)
def test_timestamp_rfc2822(self): h = kron.timestamp(1457128501) w = 'Fri, 04 Mar 2016 21:55:01 +0000' self.assertEqual(h.str('UTC', 'rfc2822'), w) self.assertEqual(h.rfc2822('UTC'), w)
def test_timestamp_pseudo_format_all(self): a = '1457128501' h = kron.timestamp(a) w = { a: dict(UTC=dict( HH='21', HHMM='2155', HHMMSS='215501', HH_MM='21:55', HH_MM_SS='21:55:01', MM='55', SS='01', abbr_date='Fri, Mar 04, 2016', abbr_month='Mar', abbr_weekday='Fri', ampm='PM', base='2016-03-04 21:55:01', basetz='2016-03-04 21:55:01 UTC', ccyy='2016', ccyymm='201603', ccyymmdd='20160304', date='Friday, March 04, 2016', dd='04', hh='09', hh_MM='09:55', hh_MM_SS='09:55:01', hh_MM_SS_ampm='09:55:01 PM', hh_MM_ampm='09:55 PM', hours='21', hours12='09', hours24='21', iso8601='2016-03-04T21:55:01Z', julian='064', microseconds='000000', minutes='55', mm='03', mm_dd_yy='03/04/16', mmdd='0304', mon='Mar', month='March', national='Fri Mar 04 21:55:01 UTC 2016', national_date='03/04/16', national_time='21:55:01', rfc2822='Fri, 04 Mar 2016 21:55:01 +0000', rfc2822_='Fri, 04 Mar 2016 21:55:01', seconds='01', tz='UTC', tz_offset='+0000', week_number_mon='09', week_number_sun='09', weekday='Friday', year='2016', yy='16', yymm='1603', yymmdd='160304', yyyy='2016', yyyy_mm_dd='2016/03/04', yyyymm='201603', yyyymmdd='20160304', )) } self.maxDiff = None self.assertEqual(h.dict('UTC', 'all'), w[a]) self.assertEqual(kron._cli(['-t', 'UTC', '-f', 'all', a]), \ kron._json(w))
def test_timestamp_str2(self): h = kron.timestamp('2016-03-04 16:55:01', 'EST') w = 1457128501 self.assertEqual(h.value, w) self.assertIsInstance(h.value, float)
def test_timestamp_iso8601(self): h = kron.timestamp(1457128501) w = '2016-03-04T21:55:01Z' self.assertEqual(h.str(fmt='iso8601'), w) self.assertEqual(h.iso8601(), w)
def test_timestamp_pseudo_format_all(self): a = '1457128501' h = kron.timestamp(a) w = {a:dict(UTC=dict( HH='21', HHMM='2155', HHMMSS='215501', HH_MM='21:55', HH_MM_SS='21:55:01', MM='55', SS='01', abbr_date='Fri, Mar 04, 2016', abbr_month='Mar', abbr_weekday='Fri', ampm='PM', base='2016-03-04 21:55:01', basetz='2016-03-04 21:55:01 UTC', ccyy='2016', ccyymm='201603', ccyymmdd='20160304', date='Friday, March 04, 2016', dd='04', hh='09', hh_MM='09:55', hh_MM_SS='09:55:01', hh_MM_SS_ampm='09:55:01 PM', hh_MM_ampm='09:55 PM', hours='21', hours12='09', hours24='21', iso8601='2016-03-04T21:55:01Z', julian='064', microseconds='000000', minutes='55', mm='03', mm_dd_yy='03/04/16', mmdd='0304', mon='Mar', month='March', national='Fri Mar 04 21:55:01 UTC 2016', national_date='03/04/16', national_time='21:55:01', rfc2822='Fri, 04 Mar 2016 21:55:01 +0000', rfc2822_='Fri, 04 Mar 2016 21:55:01', seconds='01', tz='UTC', tz_offset='+0000', week_number_mon='09', week_number_sun='09', weekday='Friday', year='2016', yy='16', yymm='1603', yymmdd='160304', yyyy='2016', yyyy_mm_dd='2016/03/04', yyyymm='201603', yyyymmdd='20160304', ))} self.maxDiff = None self.assertEqual(h.dict('UTC', 'all'), w[a]) self.assertEqual(kron._cli(['-t', 'UTC', '-f', 'all', a]), \ kron._json(w))
def test_timestamp_str5(self): h = kron.timestamp('2016-03-04T21:55:01Z', fmt='iso8601') w = 1457128501 self.assertEqual(h.value, w) self.assertIsInstance(h.value, float)
def test_timestamp_default(self): h = kron.timestamp() self.assertIsInstance(h.value, float) r = r'^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d UTC$' self.assertRegexpMatches(h.str('UTC'), r)
def test_timestamp_str6(self): h = kron.timestamp('Fri, 04 Mar 2016 21:55:01', 'UTC', 'rfc2822_') w = 1457128501 self.assertEqual(h.value, w) self.assertIsInstance(h.value, float)
def test_timestamp_str3(self): h = kron.timestamp('2016-03-04 17:55:01', 'Madrid') w = 1457128501 self.assertEqual(h.value, w) self.assertIsInstance(h.value, float)
def test_timestamp_str1(self): h = kron.timestamp('2016-03-04 21:55:01', 'UTC') w = 1457128501 self.assertEqual(h.value, w) self.assertIsInstance(h.value, float)
def test_timestamp_formats(self): w = 1457139301.123456 h = kron.timestamp(w) tz = 'CST' self.assertEqual(h.value, w) self.assertIsInstance(h.value, float) w = dict( HH='18', HHMM='1855', HHMMSS='185501', HH_MM='18:55', HH_MM_SS='18:55:01', MM='55', SS='01', abbr_date='Fri, Mar 04, 2016', abbr_month='Mar', abbr_weekday='Fri', ampm='PM', base='2016-03-04 18:55:01', basetz='2016-03-04 18:55:01 CST', ccyy='2016', ccyymm='201603', ccyymmdd='20160304', date='Friday, March 04, 2016', dd='04', hh='06', hh_MM='06:55', hh_MM_SS='06:55:01', hh_MM_SS_ampm='06:55:01 PM', hh_MM_ampm='06:55 PM', hours='18', hours12='06', hours24='18', iso8601='2016-03-05T00:55:01Z', julian='064', microseconds='123456', minutes='55', mm='03', mm_dd_yy='03/04/16', mmdd='0304', mon='Mar', month='March', national='Fri Mar 04 18:55:01 CST 2016', national_date='03/04/16', national_time='18:55:01', rfc2822='Fri, 04 Mar 2016 18:55:01 -0600', seconds='01', tz='CST', tz_offset='-0600', week_number_mon='09', week_number_sun='09', weekday='Friday', year='2016', yy='16', yymm='1603', yymmdd='160304', yyyy='2016', yyyy_mm_dd='2016/03/04', yyyymm='201603', yyyymmdd='20160304', ) for fmt, v in list(w.items()): self.assertEqual(h.str(tz, fmt), v)
def test_timestamp_str3(self): h = kron.timestamp('2016-03-04 22:55:01', 'Madrid') w = 1457128501 self.assertEqual(h.value, w) self.assertIsInstance(h.value, float)