def test_to_ts(self): ts = timeutil.ts() cases = ( ts, ts + 0.1, ts * 1000, ts * 1000 + 1, ts * 1000 + 0.1, ts * 1000 * 1000, ts * 1000 * 1000 + 1, ts * 1000 * 1000 + 0.1, ts * 1000 * 1000 * 1000, ts * 1000 * 1000 * 1000 + 1, ts * 1000 * 1000 * 1000 + 0.1, ) for inp in cases: dd(inp, ts) self.assertEqual(ts, timeutil.to_sec(inp), 'convert {inp} to second'.format(inp=repr(inp))) self.assertEqual(ts, timeutil.to_sec(str(inp)), 'convert {inp} to second'.format(inp=repr(inp)))
def test_ts_and_datetime_conversion(self): ts = timeutil.ts() dt = timeutil.ts_to_datetime(ts) converted_ts = timeutil.utc_datetime_to_ts(dt) self.assertEqual(ts, converted_ts)
def clean_binlog(self, before_days=5): ts = timeutil.ts() ts = ts - 86400 * before_days dt = timeutil.format_ts(ts, 'daily') pool = mysqlconnpool.make(self.mysql_addr) self.mysql_pool_query( pool, 'PURGE BINARY LOGS BEFORE "{dt} 00:00:00"'.format(dt=dt))
def test_convert_to_ts(self): ts = timeutil.ts() ms = ts * 1000 us = ts * 1000000 ns = ts * 1000000000 self.assertEqual(ts, timeutil.ms_to_ts(ms)) self.assertEqual(ts, timeutil.us_to_ts(us)) self.assertEqual(ts, timeutil.ns_to_ts(ns))
def make_arc_dir(self): now_ts = timeutil.ts() - time.timezone - self.time_toleration date_str = timeutil.format_ts(now_ts, 'daily') arc_dir = os.path.join(self.arc_base, date_str ) fsutil.makedirs(arc_dir) symlink_path = os.path.join(self.arc_base, 'current') while True: try: os.symlink(arc_dir, symlink_path) break except OSError as e: if e[0] == errno.EEXIST: os.unlink(symlink_path) else: raise return arc_dir
print timeutil.format(dt, 'utc') print timeutil.format(dt, 'iso') print timeutil.format(dt, 'daily') print ts = timeutil.utc_datetime_to_ts(dt) print ts print time_mysql = timeutil.format_ts(ts, 'mysql') print time_mysql time_iso = timeutil.format_ts(ts, 'iso') print time_iso print ts = timeutil.ts() print ts time_mysql = timeutil.format_ts(ts, 'mysql') print time_mysql print ms = timeutil.ms() us = timeutil.us() ns = timeutil.ns() print ms print us print ns print print timeutil.to_sec(ms) print timeutil.to_sec(us)