def test_norm_loc(input, itz, exp): """ input: dtspec output: normalized and localized datetime object """ pytest.dbgfunc() if itz: nub = dt(input, tz=itz) else: nub = dt(input) actual = nub._norm_loc_ize(input) assert actual == exp
def test_from_format(dtspec, itz, otz, exp): """ Exercise each of the supported formats and fail on at least one unsupported one """ pytest.dbgfunc() if isinstance(exp, dt_error): with pytest.raises(dt_error) as err: dt(dtspec, tz=itz) assert str(exp) in str(err.value) else: actual = dt(dtspec, tz=itz) assert actual("%F %T %Z", tz=otz) == exp
def utc_fr_local_tz(**kw): """ Compute utc from local time and timezone """ if kw['d']: pdb.set_trace() # pragma: no cover dtspec = kw['LOC_DTSPEC'] or 'now' if dtspec == 'now': dtspec = dt().strftime("%F %T") tzname = kw['TIMEZONE'] or 'local' utc = pytz.timezone('utc') nl = dt(dtspec, tz=tzname) print(nl("%F %T %Z", tz=utc))
def test_delta(left, right, exp): """ Tests for _delta """ pytest.dbgfunc() q = dt() assert q._delta(left, right) == exp
def local_fr_utc_tz(**kw): """ Compute local time from utc and timezone """ if kw['d']: pdb.set_trace() # pragma: no cover (dtspec, tz) = (kw['UTC_DTSPEC'], kw['TIMEZONE']) dtspec = dtspec or 'now' if dtspec == 'now': dtspec = dt(tz='utc').strftime("%F %T") if tz == '': tz = 'local' when = dt(dtspec, tz='utc') print(when("%F %T %Z", tz=tz))
def dtm_rdt(**kw): """ Generate a random date """ if kw['d']: # pragma: no cover pdb.set_trace() x = kw['EPOCH'] or random.randint(0, int(time.time() * 1.25)) thunk = dt(epoch=x) print("{} (epoch = {})".format(thunk(), thunk._utc))
def test_rdt(capsys): """ Test dtm_rdt """ inp = 1947230258 kw = {'EPOCH': inp, 'd': False} foo = dt(epoch=inp) dtmain.dtm_rdt(**kw) (out, err) = capsys.readouterr() exp = "{} (epoch = {})".format(foo("%F-%T"), inp) assert exp in out
def test_utl(dtspec, zone, expi, expf, expz, capsys): """ Test function to convert UTC time to another timezone """ pytest.dbgfunc() expi = expi or () exp = dt(*expi, tz=expz)(expf, tz=expz) kw = {'UTC_DTSPEC': dtspec, 'TIMEZONE': zone, 'd': False} dtmain.local_fr_utc_tz(**kw) (out, err) = capsys.readouterr() assert re.search(exp, out), "'{}' not found in '{}'".format(exp, out)
def test_ltu(dtspec, zone, expi, expf, expz, capsys): """ Test function to convert local time to UTC """ pytest.dbgfunc() expi = expi or () exp = dt(*expi, tz=expz)(expf, tz=expz) kw = {'LOC_DTSPEC': dtspec, 'TIMEZONE': zone, 'd': False} dtmain.utc_fr_local_tz(**kw) (out, err) = capsys.readouterr() assert exp in out, "'{}' not found in '{}'".format(exp, out)
def calendar(**kw): """ Generate calendars dtm calendar # calendar for current month dtm calendar DTSPEC # calendar for month containing DTSPEC """ if kw['d']: pdb.set_trace() # pragma: no cover if kw['DTSPEC']: when = dt(kw['DTSPEC']) else: when = dt() mday = int(when("%d")) # month day start = when.previous_day(mday - 1) # beginning of month end = when.next_day(32 - mday) # next month while end("%b") != start("%b"): # back to end of target month end = end.previous_day() mlen = int(end("%d")) # number of last day op = start("%B %Y") # month name and year lsp = (22 - len(op)) // 2 # count leading spaces wsp = lsp * " " # generate leading space op = wsp + op + wsp + "\n" # insert leading & trailing space op += " mo tu we th fr sa su\n" # weekday names lslots = weekday_ordinal(start.weekday()) - 1 # leading slots slot = 0 for n in range(lslots): # generate leading slots op += " " slot += 1 for day in range(1, mlen + 1): # add each day to the month op += " {:2d}".format(day) slot += 1 if slot % 7 == 0: # newline on every 7th slot op += "\n" while slot % 7 != 0: op += " " slot += 1 print(op)
def test_calendar_now(capsys): """ Test 'dtm calendar' for the current month """ kw = {'d': False, 'DTSPEC': None} when = dt() mday = int(when("%d")) start = when.previous_day(mday - 1) end = when.next_day(32 - mday) while end("%b") != start("%b"): end = end.previous_day() exp = month_ref(start.weekday(), int(end("%d"))) dtmain.calendar(**kw) (out, err) = capsys.readouterr() pytest.dbgfunc() assert exp in out, "\n{} \n not in \n\n{}".format(exp, out)
tz == timezone name => return pytz.timezone(tz) tz is None => return tzlocal.get_localzone() something else => dt_error( msg ) """ pytest.dbgfunc() if isinstance(exp, dt_error): with pytest.raises(dt_error) as err: dt._static_brew_tz(inp) assert str(exp) in str(err.value) else: assert dt._static_brew_tz(inp) == exp # ----------------------------------------------------------------------------- @pytest.mark.parametrize("obj, itz, exp", [ dtu.pp(dt(), pytz.timezone('est5edt'), pytz.timezone('est5edt'), id="tz -> tz"), dtu.pp(dt(), 'local', tzlocal.get_localzone(), id="'local' -> local tz"), dtu.pp(dt(), 'est5edt', pytz.timezone('est5edt'), id="tz name -> tz"), dtu.pp(dt(), None, tzlocal.get_localzone(), id="None -> local tz"), dtu.pp(dt(), 17, dt_error("tz must be timezone, timezone name, or None"), id="invalid timezone") ]) def test_brew_tz(obj, itz, exp): """ This function can take a pytz.BaseTzInfo object, a string containing 'local' or the name of a timezone, or None.
id=ppf("td+13: <int> + <td> => <td>", "T", w=48)), dtu.pp(283, td(-876), td(-593), id=ppf("td+14: <int> + <td> => <td>", "T", w=48)), dtu.pp(-677, td(285), td(-392), id=ppf("td+15: <int> + <td> => <td>", "T", w=48)), dtu.pp(-848, td(-180), td(-1028), id=ppf("td+16: <int> + <td> => <td>", "T", w=48)), dtu.pp(td(17), datetime(2015, 1, 1, 8, 0, 0), dt("2015.0101 08:00:17"), id=ppf("dt+17: <td> + <datetime> => <dt>", "T", w=48)), dtu.pp(td(-552), datetime(2009, 4, 5, 6, 7, 8), dt("2009.0405 05:57:56"), id=ppf("dt+18: <td> + <datetime> => <dt>", "T", w=48)), dtu.pp(datetime(1986, 6, 2, 13, 42, 40), td(910), dt("1986.0602 13:57:50"), id=ppf("dt+19: <datetime> + <td> => <dt>", "T", w=48)), dtu.pp(datetime(1977, 9, 14, 12, 17, 2), td(-2655), dt("1977.0914 11:32:47"), id=ppf("dt+20: <datetime> + <td> => <dt>", "T", w=48)), dtu.pp(td(), [1, 2, 3], dtu.unsupp('+', 'td', 'list'),