Created
April 8, 2016 08:27
-
-
Save rhizoome/ebacbfd5ccf1d711c93d92a6615f8f62 to your computer and use it in GitHub Desktop.
Shrinking for checking the test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def iso_first_week_start(iso_year, tzinfo=None): | |
"""The gregorian calendar date of the first day of the given ISO year | |
:param iso_year: Year to find the date of the first week. | |
:type iso_year: int""" | |
fourth_jan = datetime.datetime(iso_year, 1, 4, tzinfo=tzinfo) | |
delta = datetime.timedelta(fourth_jan.isoweekday() - 1) | |
return fourth_jan - delta | |
def iso_to_gregorian(iso_year, iso_week, iso_day, tzinfo=None): | |
"""Gregorian calendar date for the given ISO year, week and day | |
:param iso_year: ISO year | |
:type iso_year: int | |
:param iso_week: ISO week | |
:type iso_week: int | |
:param iso_day: ISO day | |
:type iso_day: int""" | |
year_start = iso_first_week_start(iso_year, tzinfo) | |
return year_start + datetime.timedelta( | |
days=iso_day - 1, | |
weeks=iso_week - 1 | |
) | |
def time_remove_tz(time): | |
"""Convert a :py:class`datetime.time` to :py:class`datetime.time` to | |
without tzinfo. | |
:param time: Time to convert | |
:type time: :py:class:`datetime.time` | |
:rtype: :py:class:`datetime.time` | |
""" | |
return datetime.time( | |
hour = time.hour, | |
minute = time.minute, | |
second = time.second, | |
microsecond = time.microsecond, | |
) | |
def time_delta_helper(time): | |
"""Convert a :py:class`datetime.time` to :py:class`datetime.datetime` to | |
calculate deltas | |
:param time: Time to convert | |
:type time: :py:class:`datetime.time` | |
:rtype: :py:class:`datetime.datetime` | |
""" | |
return datetime.datetime( | |
year = 2000, | |
month = 1, | |
day = 1, | |
hour = time.hour, | |
minute = time.minute, | |
second = time.second, | |
microsecond = time.microsecond, | |
tzinfo = time.tzinfo, | |
) | |
def date_round_weekly(date, day_of_week=1, time=None): | |
"""Round datetime back (floor) to a given the of the week. | |
THIS FUNCTION IGNORES THE TZINFO OF TIME and assumes it is the same tz as | |
the date. | |
:param date: Datetime object to round | |
:type date: :py:class:`datetime.datetime` | |
:param day_of_week: ISO day of week: monday is 1 and sunday is 7 | |
:type day_of_week: int | |
:param time: Roundpoint in the day (tzinfo ignored) | |
:type time: :py:class:`datetime.time` | |
:rtype: :py:class:`datetime.datetime`""" | |
if time: | |
time = time_remove_tz(time) | |
else: # pragma: no cover | |
time = datetime.time(hour=0, minute=0) | |
delta = datetime.timedelta( | |
days = day_of_week - 1, | |
hours = time.hour, | |
minutes = time.minute, | |
seconds = time.second, | |
microseconds = time.microsecond, | |
) | |
raster_date = date - delta | |
iso = raster_date.isocalendar() | |
rounded_date = iso_to_gregorian(iso[0], iso[1], 1, date.tzinfo) | |
return rounded_date + delta | |
def date_round_daily(date, time=None): | |
"""Round datetime to day back (floor) to the roundpoint (time) in the day | |
THIS FUNCTION IGNORES THE TZINFO OF TIME and assumes it is the same tz as | |
the date. | |
:param date: Datetime object to round | |
:type date: :py:class:`datetime.datetime` | |
:param time: Roundpoint in the day (tzinfo ignored) | |
:type time: :py:class:`datetime.time` | |
:rtype: :py:class:`datetime.datetime`""" | |
if time: | |
time = time_remove_tz(time) | |
else: # pragma: no cover | |
time = datetime.time(hour=0, minute=0) | |
delta = datetime.timedelta( | |
hours = time.hour, | |
minutes = time.minute, | |
seconds = time.second, | |
microseconds = time.microsecond, | |
) | |
raster_date = date - delta | |
rounded_date = datetime.datetime( | |
year = raster_date.year, | |
month = raster_date.month, | |
day = raster_date.day, | |
tzinfo = raster_date.tzinfo | |
) | |
return rounded_date + delta |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def ise(i, tzinfo): | |
n = datetime.datetime(i, 1, 4, tzinfo=tzinfo) | |
a = datetime.timedelta(n.isoweekday() - 1) | |
return n - a | |
def iso_to_gregorian(i, iso_week, iso_day, tzinfo=None): | |
at = ise(i, tzinfo) | |
return at + datetime.timedelta( | |
iso_day - 1, | |
weeks=iso_week - 1 | |
) | |
def time_delta_helper(time): | |
return datetime.datetime( | |
2, | |
1, | |
1, | |
time.hour, | |
time.minute, | |
time.second, | |
time.microsecond | |
) | |
def date_round_weekly(date, dek, time): | |
a = datetime.timedelta(dek - 1, hours=time.hour) | |
r = date - a | |
iso = r.isocalendar() | |
r = iso_to_gregorian(iso[0], iso[1], 1, date.tzinfo) | |
return r + a | |
def date_round_daily(date, time): | |
a = datetime.timedelta(hours=time.hour) | |
r = date - a | |
r = datetime.datetime( | |
r.year, | |
r.month, | |
r.day, | |
tzinfo=r.tzinfo | |
) | |
return r + a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def ise(i,tzinfo): | |
n=datetime.datetime(i,1,4,tzinfo=tzinfo) | |
a=datetime.timedelta(n.isoweekday()-1) | |
return n-a | |
def iso_to_gregorian(i,iso_week,iso_day,tzinfo=None): | |
at=ise(i,tzinfo) | |
return at+datetime.timedelta(iso_day-1,weeks=iso_week-1) | |
def time_delta_helper(time):return datetime.datetime(2,1,1,time.hour,time.minute,time.second,time.microsecond) | |
def date_round_weekly(date,dek,time): | |
a=datetime.timedelta(dek-1,hours=time.hour) | |
r=date-a | |
iso=r.isocalendar() | |
r=iso_to_gregorian(iso[0],iso[1],1,date.tzinfo) | |
return r+a | |
def date_round_daily(date,time): | |
a=datetime.timedelta(hours=time.hour) | |
r=date-a | |
r=datetime.datetime(r.year,r.month,r.day,tzinfo=r.tzinfo) | |
return r+a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment