Пример #1
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)
Пример #2
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')