def parsedate_to_datetime(data): _3to2list = list(_parsedate_tz(data)) dtuple, tz, = [_3to2list[:-1]] + _3to2list[-1:] if tz is None: return datetime.datetime(*dtuple[:6]) return datetime.datetime(*dtuple[:6], tzinfo=datetime.timezone(datetime.timedelta(seconds=tz)))
def test_datetime_filter(self): from gecoscc.filters import datetime as datetime_filter dt = datetime(2007, 4, 1, 15, 30) self.assertEqual(datetime_filter(dt), '01/04/2007 17:30') self.assertEqual(datetime_filter(dt, 'full'), 'domingo 1/abr./2007 17:30')
def localtime(dt=None, isdst=-1): """Return local time as an aware datetime object. If called without arguments, return current time. Otherwise *dt* argument should be a datetime instance, and it is converted to the local time zone according to the system time zone database. If *dt* is naive (that is, dt.tzinfo is None), it is assumed to be in local time. In this case, a positive or zero value for *isdst* causes localtime to presume initially that summer time (for example, Daylight Saving Time) is or is not (respectively) in effect for the specified time. A negative value for *isdst* causes the localtime() function to attempt to divine whether summer time is in effect for the specified time. """ if dt is None: return datetime.datetime.now(datetime.timezone.utc).astimezone() if dt.tzinfo is not None: return dt.astimezone() # We have a naive datetime. Convert to a (localtime) timetuple and pass to # system mktime together with the isdst hint. System mktime will return # seconds since epoch. tm = dt.timetuple()[:-1] + (isdst,) seconds = time.mktime(tm) localtm = time.localtime(seconds) try: delta = datetime.timedelta(seconds=localtm.tm_gmtoff) tz = datetime.timezone(delta, localtm.tm_zone) except AttributeError: # Compute UTC offset and compare with the value implied by tm_isdst. # If the values match, use the zone name implied by tm_isdst. delta = dt - datetime.datetime(*time.gmtime(seconds)[:6]) dst = time.daylight and localtm.tm_isdst > 0 gmtoff = -(time.altzone if dst else time.timezone) if delta == datetime.timedelta(seconds=gmtoff): tz = datetime.timezone(delta, time.tzname[dst]) else: tz = datetime.timezone(delta) return dt.replace(tzinfo=tz)
def test_end_of_year(): assert datetime(2018, 12, 31, 23, 59, 59) == end_of_year(2018)
def test_start_of_year(): assert datetime(2018, 1, 1) == start_of_year(2018)
def end_of_year(year): return datetime(year, 12, 31, 23, 59, 59)
def start_of_year(year): return datetime(year, 1, 1)
def end_of_month(year, month): days_in_month = calendar.monthrange(year, month)[1] return datetime(year, month, days_in_month, 23, 59, 59)
def start_of_month(year, month): return datetime(year, month, 1)