def test_combine(self): d = date(2002, 3, 4) t = time(18, 45, 3, 1234) expected = self.theclass(2002, 3, 4, 18, 45, 3, 1234) combine = self.theclass.combine dt = combine(d, t) self.assertEqual(dt, expected) dt = combine(time=t, date=d) self.assertEqual(dt, expected) self.assertEqual(d, dt.date()) self.assertEqual(t, dt.time()) self.assertEqual(dt, combine(dt.date(), dt.time())) self.assertRaises(TypeError, combine) # need an arg self.assertRaises(TypeError, combine, d) # need two args self.assertRaises(TypeError, combine, t, d) # args reversed self.assertRaises(TypeError, combine, d, t, 1) # wrong tzinfo type self.assertRaises(TypeError, combine, d, t, 1, 2) # too many args self.assertRaises(TypeError, combine, "date", "time") # wrong types self.assertRaises(TypeError, combine, d, "time") # wrong type self.assertRaises(TypeError, combine, "date", t) # wrong type # tzinfo= argument dt = combine(d, t, timezone.utc) self.assertIs(dt.tzinfo, timezone.utc) dt = combine(d, t, tzinfo=timezone.utc) self.assertIs(dt.tzinfo, timezone.utc) t = time() dt = combine(dt, t) self.assertEqual(dt.date(), d) self.assertEqual(dt.time(), t)
def test_subclass_alternate_constructors_datetime(self): # Test that alternate constructors call the constructor class DateTimeSubclass(self.theclass): def __new__(cls, *args, **kwargs): result = self.theclass.__new__(cls, *args, **kwargs) result.extra = 7 return result args = (2003, 4, 14, 12, 30, 15, 123456) d_isoformat = "2003-04-14T12:30:15.123456" # Equivalent isoformat() utc_ts = 1050323415.123456 # UTC timestamp base_d = DateTimeSubclass(*args) self.assertIsInstance(base_d, DateTimeSubclass) self.assertEqual(base_d.extra, 7) # Timestamp depends on time zone, so we'll calculate the equivalent here ts = base_d.timestamp() test_cases = [ ("fromtimestamp", (ts, ), base_d), # See https://bugs.python.org/issue32417 ("fromtimestamp", (ts, timezone.utc), base_d.astimezone(timezone.utc)), ("utcfromtimestamp", (utc_ts, ), base_d), ("fromisoformat", (d_isoformat, ), base_d), ("strptime", (d_isoformat, "%Y-%m-%dT%H:%M:%S.%f"), base_d), ("combine", (date(*args[0:3]), time(*args[3:])), base_d), ] for constr_name, constr_args, expected in test_cases: for base_obj in (DateTimeSubclass, base_d): # Test both the classmethod and method with self.subTest(base_obj_type=type(base_obj), constr_name=constr_name): constructor = getattr(base_obj, constr_name) dt = constructor(*constr_args) # Test that it creates the right subclass self.assertIsInstance(dt, DateTimeSubclass) # Test that it's equal to the base object self.assertEqual(dt, expected) # Test that it called the constructor self.assertEqual(dt.extra, 7)
# SPDX-FileCopyrightText: 2001-2021 Python Software Foundation.All rights reserved. # SPDX-FileCopyrightText: 2000 BeOpen.com. All rights reserved. # SPDX-FileCopyrightText: 1995-2001 Corporation for National Research Initiatives. # All rights reserved. # SPDX-FileCopyrightText: 1995-2001 Corporation for National Research Initiatives. # All rights reserved. # SPDX-FileCopyrightText: 1991-1995 Stichting Mathematisch Centrum. All rights reserved. # SPDX-FileCopyrightText: 2021 Brent Rubell for Adafruit Industries # SPDX-License-Identifier: Python-2.0 # Example of working with a `time` object # from https://docs.python.org/3/library/datetime.html#examples-of-usage-time from adafruit_datetime import time, timezone # Create a new time object t = time(12, 10, 30, tzinfo=timezone.utc) # ISO 8601 formatted string iso_time = t.isoformat() print("ISO8601-Formatted Time:", iso_time) # Timezone name print("Timezone Name:", t.tzname())
gnss_hour = gps.timestamp_utc.tm_hour gnss_minutes = gps.timestamp_utc.tm_min gnss_seconds = gps.timestamp_utc.tm_sec gnss_lat = gps.latitude gnss_lon = gps.longitude gnss_alt = gps.altitude_m if BME_present: baro_alt = bme.altitude else: baro_alt = 0 if DebugMode: Serial_writer.write_fix( datetime.time(gnss_hour, gnss_minutes, gnss_seconds), latitude=gnss_lat, longitude=gnss_lon, valid=True, pressure_alt=baro_alt, gps_alt=gnss_alt, ) if not DebugMode: with open(file, 'a') as fp: Flash_writer = Writer(fp) Flash_writer.write_fix( datetime.time(gnss_hour, gnss_minutes, gnss_seconds), latitude=gnss_lat, longitude=gnss_lon, valid=True,
def test_extract(self): dt = self.theclass(2002, 3, 4, 18, 45, 3, 1234) self.assertEqual(dt.date(), date(2002, 3, 4)) self.assertEqual(dt.time(), time(18, 45, 3, 1234))
def at(self, time_str): """ Specify a particular time that the job should be run at. :param time_str: A string in one of the following formats: - For daily jobs -> 'HH:MM:SS' or 'HH:MM' - For hourly jobs -> 'MM:SS' or ':MM' - For minute jobs -> ':SS' The format must make sense given how often the job is repeating; for example, a job that repeats every minute should not be given a string in the form 'HH:MM:SS'. The difference between ':MM' and ':SS' is inferred from the selected time-unit (e.g. every().hour.at(':30') vs. every().minute.at(':30')). :return: The invoked job instance """ if self.unit not in ("days", "hours", "minutes") and not self.start_day: raise ScheduleValueError( "Invalid unit (valid units are `days`, `hours`, and `minutes`)" ) if not isinstance(time_str, str): raise TypeError("at() should be passed a string") if self.unit == "days" or self.start_day: if not re.match(r"^([0-2]\d:)?[0-5]\d:[0-5]\d$", time_str): raise ScheduleValueError( "Invalid time format for a daily job (valid format is HH:MM(:SS)?)" ) if self.unit == "hours": if not re.match(r"^([0-5]\d)?:[0-5]\d$", time_str): raise ScheduleValueError( "Invalid time format for an hourly job (valid format is (MM)?:SS)" ) if self.unit == "minutes": if not re.match(r"^:[0-5]\d$", time_str): raise ScheduleValueError( "Invalid time format for a minutely job (valid format is :SS)" ) time_values = time_str.split(":") if len(time_values) == 3: hour, minute, second = time_values elif len(time_values) == 2 and self.unit == "minutes": hour = 0 minute = 0 _, second = time_values elif len(time_values) == 2 and self.unit == "hours" and time_values[0]: hour = 0 minute, second = time_values else: hour, minute = time_values second = 0 if self.unit == "days" or self.start_day: hour = int(hour) if not 0 <= hour <= 23: raise ScheduleValueError( "Invalid number of hours ({} is not between 0 and 23)") elif self.unit == "hours": hour = 0 elif self.unit == "minutes": hour = 0 minute = 0 minute = int(minute) second = int(second) self.at_time = datetime.time(hour, minute, second) return self
# All rights reserved. # SPDX-FileCopyrightText: 1995-2001 Corporation for National Research Initiatives. # All rights reserved. # SPDX-FileCopyrightText: 1991-1995 Stichting Mathematisch Centrum. All rights reserved. # SPDX-FileCopyrightText: 2021 Brent Rubell for Adafruit Industries # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries # SPDX-License-Identifier: Python-2.0 # Example of working with a `datetime` object # from https://docs.python.org/3/library/datetime.html#examples-of-usage-datetime from adafruit_datetime import datetime, date, time # Using datetime.combine() d = date(2005, 7, 14) print(d) t = time(12, 30) print(datetime.combine(d, t)) # Using datetime.now() print("Current time (GMT +1):", datetime.now()) # Using datetime.timetuple() to get tuple of all attributes dt = datetime(2006, 11, 21, 16, 30) tt = dt.timetuple() for it in tt: print(it) print("Today is: ", dt.ctime()) iso_date_string = "2020-04-05T05:04:45.752301" print("Creating new datetime from ISO Date:", iso_date_string)