Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import datetime
- def next_nice_datetime(dt, tzinfo=None):
- tzinfo = datetime.timezone.utc if dt.tzinfo == datetime.timezone.utc else tzinfo
- if dt.minute == 0 and dt.second == 0 and dt.microsecond == 0 and dt.hour in [
- 2, 10, 18
- ]:
- return dt.replace(tzinfo=tzinfo)
- elif 18 <= dt.hour:
- return (dt + datetime.timedelta(days=1)).replace(
- hour=2, minute=0, second=0, microsecond=0, tzinfo=tzinfo)
- elif 10 <= dt.hour:
- return dt.replace(
- hour=18, minute=0, second=0, microsecond=0, tzinfo=tzinfo)
- elif 2 <= dt.hour:
- return dt.replace(
- hour=10, minute=0, second=0, microsecond=0, tzinfo=tzinfo)
- else:
- return dt.replace(
- hour=2, minute=0, second=0, microsecond=0, tzinfo=tzinfo)
- for t1, t2 in [(dt, next_nice_datetime(dt)) for dt in [
- datetime.datetime.utcnow(),
- datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
- datetime.datetime.strptime('20181221T020000.000000',
- '%Y%m%dT%H%M%S.%f'),
- datetime.datetime.strptime('20181221T020000.000001',
- '%Y%m%dT%H%M%S.%f')
- ]]:
- print('{} ---> {}'.format(t1, t2))
- d = datetime.datetime.utcnow()
- print('{} ---> {}'.format(d, next_nice_datetime(
- d, tzinfo=datetime.timezone.utc)))
- for t1, t2 in [(dt, next_nice_datetime(dt)) for dt in [
- datetime.datetime.strptime('20181221T{:02d}0123.456789'.format(s),
- '%Y%m%dT%H%M%S.%f') for s in range(24)
- ]]:
- print('{} ---> {}'.format(t1, t2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement