Пример #1
0
 def test_more_timetuple(self):
     # This tests fields beyond those tested by the TestDate.test_timetuple.
     t = self.theclass(2004, 12, 31, 6, 22, 33)
     self.assertEqual(t.timetuple(), (2004, 12, 31, 6, 22, 33, 4, 366, -1))
     self.assertEqual(
         t.timetuple(),
         (
             t.year,
             t.month,
             t.day,
             t.hour,
             t.minute,
             t.second,
             t.weekday(),
             t.toordinal() - date(t.year, 1, 1).toordinal() + 1,
             -1,
         ),
     )
     tt = t.timetuple()
     self.assertEqual(tt.tm_year, t.year)
     self.assertEqual(tt.tm_mon, t.month)
     self.assertEqual(tt.tm_mday, t.day)
     self.assertEqual(tt.tm_hour, t.hour)
     self.assertEqual(tt.tm_min, t.minute)
     self.assertEqual(tt.tm_sec, t.second)
     self.assertEqual(tt.tm_wday, t.weekday())
     self.assertEqual(tt.tm_yday,
                      t.toordinal() - date(t.year, 1, 1).toordinal() + 1)
     self.assertEqual(tt.tm_isdst, -1)
Пример #2
0
    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)
Пример #3
0
def fetchDate(wrds):
    word_idx = None

    ### alarm.sleep_memory isn't present on PyPortal
    ### Storing the word_idx in a register on the temperature sensor - niishhh!
    import adafruit_adt7410
    adafruit_adt7410.ADT7410.reset = lambda self: self  ### nobble the reset
    adt = adafruit_adt7410.ADT7410(board.I2C(), address=0x48)
    ### Look for a value (well) below default of 10 degrees
    if adt.low_temperature < 0.0:
        word_idx = round((0.0 - adt.low_temperature) * 128)
        wrds.selector_value = word_idx
        d_print(1, "Retrieved word_idx", word_idx)

    ### If word_idx isn't set need to fetch the data from t'Internet
    if word_idx is None:
        import supervisor
        import adafruit_datetime
        from adafruit_pyportal import PyPortal
        pyportal = PyPortal(status_neopixel=board.NEOPIXEL)
        pyportal.get_local_time()
        wordle_oday = adafruit_datetime.date(*wrds.selector_value).toordinal()
        now_oday = adafruit_datetime.datetime.now().toordinal()
        word_idx = now_oday - wordle_oday
        d_print(1, "Saving word_idx", word_idx)
        adt.low_temperature = (0 - word_idx ) / 128
        time.sleep(10)
        supervisor.reload()
Пример #4
0
    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)
Пример #5
0
                print(" done")
                print("")

            #if RTC_present:
            #  rtc.write_all((gnss_year, gnss_month, gnss_mday, gnss_hour, gnss_minutes, gnss_seconds))

            if BME_present:
                p_sensor = FR_PRESSURE
                prev_p = 0
            else:
                p_sensor = 'NA'

            igc_header = {
                'manufacturer_code': MF_CODE,
                'logger_id': uid,
                'date': datetime.date(gnss_year, gnss_month, gnss_mday),
                'fix_accuracy': 50,
                'pilot': info['pilot'],
                'copilot': info['copilot'],
                'glider_type': info['glider']['type'],
                'glider_id': info['glider']['id'],
                'firmware_version': FR_FW_ID,
                'hardware_version': RF_HW_ID,
                'logger_type': FR_TYPE,
                'gps_receiver': FR_GPS,
                'pressure_sensor': p_sensor,
                'competition_id': info['competition']['id'],
                'competition_class': info['competition']['_class'],
            }

            if DebugMode:
Пример #6
0
 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))
# 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-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())