コード例 #1
0
    def test_to_timezone(self):
        # convert from naive to eastern
        naive = datetime.datetime(2002, 10, 27, 1, 30)
        with self.assertRaisesMessage(
                AssertionError, "Can't convert naive datetime. "
                "Use DateUtils.as_timezone() for that"):
            DateUtils.to_timezone(naive, self.eastern)

        # convert from eastern to UTC
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 27, 1, 30))
        self.assertEqual(in_eastern.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')
        in_utc = DateUtils.to_timezone(in_eastern, pytz.UTC)
        self.assertEqual(in_utc.strftime(self.fmt),
                         '2002-10-27 06:30:00 UTC+0000')

        # convert from UTC to eastern
        in_utc = datetime.datetime(2002, 10, 27, 6, 30, tzinfo=pytz.UTC)
        self.assertEqual(in_utc.strftime(self.fmt),
                         '2002-10-27 06:30:00 UTC+0000')
        in_eastern = DateUtils.to_timezone(in_utc, self.eastern)
        self.assertEqual(in_eastern.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')

        # convert from eastern to eastern (nothing changed)
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 27, 1, 30))
        self.assertEqual(in_eastern.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')
        in_eastern = DateUtils.to_timezone(in_eastern, self.eastern)
        self.assertEqual(in_eastern.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')
コード例 #2
0
    def test_date_to_datetime(self):
        date = datetime.date(2002, 10, 27)

        in_utc = DateUtils.date_to_datetime(date, pytz.UTC)
        self.assertEqual(in_utc.strftime(self.fmt),
                         '2002-10-27 00:00:00 UTC+0000')

        in_eastern = DateUtils.date_to_datetime(date, self.eastern)
        self.assertEqual(in_eastern.strftime(self.fmt),
                         '2002-10-27 00:00:00 EDT-0400')
コード例 #3
0
    def test_tz_now(self, mock_datetime):
        mock_datetime.datetime.utcnow.return_value = datetime.datetime(
            2002, 10, 27, 6, 30)

        in_utc = DateUtils.tz_now(pytz.UTC)
        self.assertEqual(in_utc.strftime(self.fmt),
                         '2002-10-27 06:30:00 UTC+0000')

        in_eastern = DateUtils.tz_now(self.eastern)
        self.assertEqual(in_eastern.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')
コード例 #4
0
    def test_utc_now(self, mock_datetime):
        mock_datetime.datetime.utcnow.return_value = datetime.datetime(
            2002, 10, 27, 1, 30)

        in_utc = DateUtils.utc_now()
        self.assertEqual(in_utc.strftime(self.fmt),
                         '2002-10-27 01:30:00 UTC+0000')
コード例 #5
0
 def test_is_zoned(self):
     self.assertEqual(DateUtils.is_zoned(datetime.datetime(2002, 10, 27)),
                      False)
     self.assertEqual(
         DateUtils.is_zoned(datetime.datetime(2002, 10, 27,
                                              tzinfo=pytz.UTC)), True)
     self.assertEqual(
         DateUtils.is_zoned(
             datetime.datetime(2002, 10, 27, tzinfo=pytz.timezone('GMT'))),
         True)
     self.assertEqual(
         DateUtils.is_zoned(
             pytz.timezone('GMT').localize(datetime.datetime(2002, 10,
                                                             27))), True)
     self.assertEqual(
         DateUtils.is_zoned(
             self.eastern.localize(datetime.datetime(2002, 10, 27))), True)
コード例 #6
0
    def test_to_millis(self):
        expect_millis = 1035682200000.0

        # from naive
        naive = datetime.datetime(2002, 10, 27, 1, 30)
        millis = DateUtils.to_millis(naive)
        self.assertEqual(millis, expect_millis)

        # from UTC
        in_utc = datetime.datetime(2002, 10, 27, 1, 30, tzinfo=pytz.UTC)
        millis = DateUtils.to_millis(in_utc)
        self.assertEqual(millis, expect_millis)

        # from eastern
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 26, 21, 30))
        millis = DateUtils.to_millis(in_eastern)
        self.assertEqual(millis, expect_millis)
コード例 #7
0
    def test_to_naive(self):
        # from naive
        naive = datetime.datetime(2002, 10, 27, 1, 30)
        start_of_day = DateUtils.to_naive(naive)
        self.assertEqual(start_of_day.strftime(self.fmt),
                         '2002-10-27 01:30:00 ')

        # from UTC
        in_utc = datetime.datetime(2002, 10, 27, 1, 30, tzinfo=pytz.UTC)
        start_of_day = DateUtils.to_naive(in_utc)
        self.assertEqual(start_of_day.strftime(self.fmt),
                         '2002-10-27 01:30:00 ')

        # from eastern
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 27, 1, 30))
        start_of_day = DateUtils.to_naive(in_eastern)
        self.assertEqual(start_of_day.strftime(self.fmt),
                         '2002-10-27 01:30:00 ')
コード例 #8
0
    def test_as_timezone(self):
        # convert from naive to UTC
        naive = datetime.datetime(2002, 10, 27, 1, 30)
        in_utc = DateUtils.as_timezone(naive, pytz.UTC)
        self.assertEqual(in_utc.strftime(self.fmt),
                         '2002-10-27 01:30:00 UTC+0000')

        # convert from naive to eastern
        in_eastern = DateUtils.as_timezone(naive, self.eastern)
        self.assertEqual(in_eastern.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')

        # convert from eastern to eastern (nothing changed)
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 27, 1, 30))
        self.assertEqual(in_eastern.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')
        in_eastern = DateUtils.as_timezone(in_eastern, self.eastern)
        self.assertEqual(in_eastern.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')
コード例 #9
0
    def test_date_at_time(self):
        date = datetime.date(2002, 10, 27)

        # time is naive
        naive = datetime.datetime(2002, 10, 27, 6, 30)
        date_at_time = DateUtils.date_at_time(date, naive)
        self.assertEqual(date_at_time.strftime(self.fmt),
                         '2002-10-27 06:30:00 ')

        # time in UTC
        in_utc = datetime.datetime(2002, 10, 27, 6, 30, tzinfo=pytz.UTC)
        date_at_time = DateUtils.date_at_time(date, in_utc)
        self.assertEqual(date_at_time.strftime(self.fmt),
                         '2002-10-27 06:30:00 UTC+0000')

        # time in eastern
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 27, 1, 30))
        date_at_time = DateUtils.date_at_time(date, in_eastern)
        self.assertEqual(date_at_time.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')
コード例 #10
0
 def test_to_sf_datetime(self):
     nf_tz = pytz.timezone('Canada/Newfoundland')
     # test converting 2 local datetimes (with DST and without DST) into UTC
     summer_dt = DateUtils.as_timezone(
         datetime.datetime(2000, 7, 1, 1, 1, 1), nf_tz)
     winter_dt = DateUtils.as_timezone(
         datetime.datetime(2000, 1, 1, 1, 1, 1), nf_tz)
     summer_dt_converted = DateUtils.to_sf_datetime(summer_dt, pytz.utc)
     winter_dt_converted = DateUtils.to_sf_datetime(winter_dt, pytz.utc)
     self.assertEqual(summer_dt_converted, '2000-07-01T03:31:01+00:00')
     self.assertEqual(winter_dt_converted, '2000-01-01T04:31:01+00:00')
     # test converting 2 UTC datetimes
     summer_utc = datetime.datetime(2000, 7, 1, 1, 1, 1, tzinfo=pytz.UTC)
     winter_utc = datetime.datetime(2000, 1, 1, 1, 1, 1, tzinfo=pytz.UTC)
     summer_nf = DateUtils.to_sf_datetime(summer_utc, nf_tz)
     winter_nf = DateUtils.to_sf_datetime(winter_utc, nf_tz)
     self.assertEqual(
         parse_datetime(summer_nf).strftime('%Y-%m-%d %H:%M'),
         '2000-06-30 22:31')
     self.assertEqual(
         parse_datetime(winter_nf).strftime('%Y-%m-%d %H:%M'),
         '1999-12-31 21:31')
     self.assertEqual(
         parse_datetime(summer_nf).tzinfo._offset.total_seconds(), -9000)
     self.assertEqual(
         parse_datetime(winter_nf).tzinfo._offset.total_seconds(), -12600)
コード例 #11
0
    def test_as_date(self):
        # convert from naive to date
        naive = datetime.datetime(2002, 10, 27, 1, 30)
        date = DateUtils.as_date(naive)
        self.assertEqual(date.strftime(self.fmt), '2002-10-27 00:00:00 ')

        # convert from UTC to date
        in_utc = datetime.datetime(2002, 10, 27, 6, 30, tzinfo=pytz.UTC)
        date = DateUtils.as_date(in_utc)
        self.assertEqual(date.strftime(self.fmt), '2002-10-27 00:00:00 ')

        # convert from eastern to date
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 27, 1, 30))
        date = DateUtils.as_date(in_eastern)
        self.assertEqual(date.strftime(self.fmt), '2002-10-27 00:00:00 ')

        # convert from date to date
        date = datetime.date(2002, 10, 27)
        self.assertEqual(date.strftime(self.fmt), '2002-10-27 00:00:00 ')
        date = DateUtils.as_date(date)
        self.assertEqual(date.strftime(self.fmt), '2002-10-27 00:00:00 ')

        # convert from empty to date
        self.assertIsNone(DateUtils.as_date(None))
        # noinspection PyTypeChecker
        self.assertIsNone(DateUtils.as_date(''))
コード例 #12
0
def parse_date(date_string, format_, as_utc=True):
    """
    Return a datetime corresponding to date_string, parsed according to format.

    For example, to re-display a date string in another format::

        {{ "01/01/1970"|parse_date:"%m/%d/%Y"|date:"F jS, Y" }}
    """
    try:
        parsed_dt = datetime.datetime.strptime(date_string, format_)
        return DateUtils.as_utc(parsed_dt) if as_utc else parsed_dt
    except ValueError:
        pass
コード例 #13
0
 def test_created_modified(self):
     utc_now = DateUtils.utc_now()
     res = mommy.make(Resident)
     self.assertLess((res.created_at - utc_now).total_seconds(), 10)
     original_created_at = res.created_at
     original_modified_at = res.modified_at
     res.first_name = 'John'
     res.save()
     res.refresh_from_db()
     self.assertEqual(res.created_at, original_created_at)
     self.assertGreater(res.modified_at, original_modified_at)
     self.assertLess(
         (res.modified_at - original_created_at).total_seconds(), 10)
コード例 #14
0
    def test_as_utc(self):
        # convert from naive to UTC
        naive = datetime.datetime(2002, 10, 27, 1, 30)
        in_utc = DateUtils.as_utc(naive)
        self.assertEqual(in_utc.strftime(self.fmt),
                         '2002-10-27 01:30:00 UTC+0000')

        # convert from UTC to UTC (nothing changed)
        in_utc = datetime.datetime(2002, 10, 27, 1, 30, tzinfo=pytz.UTC)
        self.assertEqual(in_utc.strftime(self.fmt),
                         '2002-10-27 01:30:00 UTC+0000')
        in_utc = DateUtils.as_utc(in_utc)
        self.assertEqual(in_utc.strftime(self.fmt),
                         '2002-10-27 01:30:00 UTC+0000')

        # hard-replace from localized to UTC (simply ignore original tz)
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 27, 1, 30))
        self.assertEqual(in_eastern.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')
        in_utc = DateUtils.as_utc(in_eastern)
        self.assertEqual(in_utc.strftime(self.fmt),
                         '2002-10-27 01:30:00 UTC+0000')
コード例 #15
0
    def test_is_dst(self):
        # from naive to UTC
        naive = datetime.datetime(2002, 10, 27, 1, 30)
        is_dst = DateUtils.is_dst(naive, pytz.UTC)
        self.assertEqual(is_dst, False)

        # from naive to eastern-summer
        naive = datetime.datetime(2002, 10, 27, 5, 30)
        is_dst = DateUtils.is_dst(naive, self.eastern)
        self.assertEqual(is_dst, True)

        # from naive to eastern-winter
        naive = datetime.datetime(2002, 10, 27, 6, 30)
        is_dst = DateUtils.is_dst(naive, self.eastern)
        self.assertEqual(is_dst, False)

        # from UTC to eastern-summer
        in_utc = datetime.datetime(2002, 10, 27, 5, 30, tzinfo=pytz.UTC)
        is_dst = DateUtils.is_dst(in_utc, self.eastern)
        self.assertEqual(is_dst, True)

        # from UTC to eastern-winter
        in_utc = datetime.datetime(2002, 10, 27, 6, 30, tzinfo=pytz.UTC)
        is_dst = DateUtils.is_dst(in_utc, self.eastern)
        self.assertEqual(is_dst, False)

        # from eastern-summer to eastern-summer
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 27, 0, 30))
        is_dst = DateUtils.is_dst(in_eastern, self.eastern)
        self.assertEqual(is_dst, True)

        # from eastern-winter to eastern-winter
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 27, 1, 30))
        is_dst = DateUtils.is_dst(in_eastern, self.eastern)
        self.assertEqual(is_dst, False)
コード例 #16
0
 def test_weekdays_until(self, _):
     self.assertEqual(DateUtils.weekdays_until(datetime.date(2019, 5, 13)),
                      2)
     self.assertEqual(
         DateUtils.weekdays_until(datetime.datetime(2019, 5, 10)), 1)
コード例 #17
0
    def test_to_sf_datetime(self):
        self.assertEqual(DateUtils.to_sf_datetime(dt=None, tz=None), None)

        # from naive + from UTC
        for src_dt in (datetime.datetime(2002, 10, 27, 1, 30, 0, 999),
                       datetime.datetime(2002,
                                         10,
                                         27,
                                         1,
                                         30,
                                         0,
                                         999,
                                         tzinfo=pytz.UTC)):

            sf_dt = DateUtils.to_sf_datetime(dt=src_dt,
                                             tz=None,
                                             convert_to_isoformat=False)
            self.assertEqual(sf_dt.strftime(self.fmt),
                             '2002-10-27 01:30:00 UTC+0000')
            sf_dt = DateUtils.to_sf_datetime(dt=src_dt,
                                             tz=None,
                                             convert_to_isoformat=True)
            self.assertEqual(sf_dt, '2002-10-27T01:30:00+00:00')

            sf_dt = DateUtils.to_sf_datetime(dt=src_dt,
                                             tz=pytz.UTC,
                                             convert_to_isoformat=False)
            self.assertEqual(sf_dt.strftime(self.fmt),
                             '2002-10-27 01:30:00 UTC+0000')
            sf_dt = DateUtils.to_sf_datetime(dt=src_dt,
                                             tz=pytz.UTC,
                                             convert_to_isoformat=True)
            self.assertEqual(sf_dt, '2002-10-27T01:30:00+00:00')

            sf_dt = DateUtils.to_sf_datetime(dt=src_dt,
                                             tz=self.eastern,
                                             convert_to_isoformat=False)
            self.assertEqual(sf_dt.strftime(self.fmt),
                             '2002-10-26 21:30:00 EDT-0400')
            sf_dt = DateUtils.to_sf_datetime(dt=src_dt,
                                             tz=self.eastern,
                                             convert_to_isoformat=True)
            self.assertEqual(sf_dt, '2002-10-26T21:30:00-04:00')

        # from eastern
        in_eastern = self.eastern.localize(
            datetime.datetime(2002, 10, 27, 1, 30, 0, 999))
        sf_dt = DateUtils.to_sf_datetime(dt=in_eastern,
                                         tz=None,
                                         convert_to_isoformat=False)
        self.assertEqual(sf_dt.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')
        sf_dt = DateUtils.to_sf_datetime(dt=in_eastern,
                                         tz=None,
                                         convert_to_isoformat=True)
        self.assertEqual(sf_dt, '2002-10-27T01:30:00-05:00')

        sf_dt = DateUtils.to_sf_datetime(dt=in_eastern,
                                         tz=pytz.UTC,
                                         convert_to_isoformat=False)
        self.assertEqual(sf_dt.strftime(self.fmt),
                         '2002-10-27 06:30:00 UTC+0000')
        sf_dt = DateUtils.to_sf_datetime(dt=in_eastern,
                                         tz=pytz.UTC,
                                         convert_to_isoformat=True)
        self.assertEqual(sf_dt, '2002-10-27T06:30:00+00:00')

        sf_dt = DateUtils.to_sf_datetime(dt=in_eastern,
                                         tz=self.eastern,
                                         convert_to_isoformat=False)
        self.assertEqual(sf_dt.strftime(self.fmt),
                         '2002-10-27 01:30:00 EST-0500')
        sf_dt = DateUtils.to_sf_datetime(dt=in_eastern,
                                         tz=self.eastern,
                                         convert_to_isoformat=True)
        self.assertEqual(sf_dt, '2002-10-27T01:30:00-05:00')