def test_on_last_transition(): tz = pendulum.timezone("Europe/Paris") dt = pendulum.naive(2037, 10, 25, 2, 30) dt = tz.convert(dt, dst_rule=pendulum.POST_TRANSITION) assert dt.year == 2037 assert dt.month == 10 assert dt.day == 25 assert dt.hour == 2 assert dt.minute == 30 assert dt.second == 0 assert dt.microsecond == 0 assert dt.utcoffset().total_seconds() == 3600 dt = pendulum.naive(2037, 10, 25, 2, 30) dt = tz.convert(dt, dst_rule=pendulum.PRE_TRANSITION) assert dt.year == 2037 assert dt.month == 10 assert dt.day == 25 assert dt.hour == 2 assert dt.minute == 30 assert dt.second == 0 assert dt.microsecond == 0 assert dt.utcoffset().total_seconds() == 7200
def test_naive(): dt = pendulum.naive(2018, 2, 2, 12, 34, 56, 123456) assert_datetime(dt, 2018, 2, 2, 12, 34, 56, 123456) assert dt.tzinfo is None assert dt.timezone is None assert dt.timezone_name is None
def test_subtraction(): d = pendulum.naive(2016, 7, 5, 12, 32, 25, 0) future_dt = datetime(2016, 7, 5, 13, 32, 25, 0) future = d.add(hours=1) assert 3600 == (future - d).total_seconds() assert 3600 == (future_dt - d).total_seconds()
def test_to_strings(): dt = pendulum.naive(2013, 3, 31, 2, 30) assert dt.isoformat() == "2013-03-31T02:30:00" assert dt.to_iso8601_string() == "2013-03-31T02:30:00" assert dt.to_rfc3339_string() == "2013-03-31T02:30:00" assert dt.to_atom_string() == "2013-03-31T02:30:00" assert dt.to_cookie_string() == "Sunday, 31-Mar-2013 02:30:00 "
def test_subtraction_aware_naive(): dt = pendulum.datetime(2016, 7, 5, 12, 32, 25, 0) future_dt = datetime(2016, 7, 5, 13, 32, 25, 0) with pytest.raises(TypeError): future_dt - dt future_dt = pendulum.naive(2016, 7, 5, 13, 32, 25, 0) with pytest.raises(TypeError): future_dt - dt
def test_timezones_extension_can_be_disabled(): tz = pendulum.timezone("Europe/Paris", extended=False) dt = tz.convert(pendulum.naive(2134, 2, 13, 1)) assert_datetime(dt, 2134, 2, 13, 1) assert dt.utcoffset().total_seconds() == 3600 assert dt.dst() == timedelta() dt = tz.convert( pendulum.naive(2134, 3, 28, 2, 30), dst_rule=pendulum.POST_TRANSITION ) assert_datetime(dt, 2134, 3, 28, 2, 30) assert dt.utcoffset().total_seconds() == 3600 assert dt.dst() == timedelta() dt = tz.convert(pendulum.naive(2134, 7, 11, 2, 30)) assert_datetime(dt, 2134, 7, 11, 2, 30) assert dt.utcoffset().total_seconds() == 3600 assert dt.dst() == timedelta() dt = tz.convert( pendulum.naive(2134, 10, 31, 2, 30), dst_rule=pendulum.PRE_TRANSITION ) assert_datetime(dt, 2134, 10, 31, 2, 30) assert dt.utcoffset().total_seconds() == 3600 assert dt.dst() == timedelta() dt = tz.convert( pendulum.naive(2134, 10, 31, 2, 30), dst_rule=pendulum.POST_TRANSITION ) assert_datetime(dt, 2134, 10, 31, 2, 30) assert dt.utcoffset().total_seconds() == 3600 assert dt.dst() == timedelta()
def __rsub__(self, other): if not isinstance(other, datetime.datetime): return NotImplemented if not isinstance(other, self.__class__): if other.tzinfo is None: other = pendulum.naive( other.year, other.month, other.day, other.hour, other.minute, other.second, other.microsecond, ) else: other = pendulum.instance(other) return self.diff(other, False)
def __sub__( self, other: Union[datetime.datetime, datetime.timedelta] ) -> Union["DateTime", Period]: if isinstance(other, datetime.timedelta): return self._subtract_timedelta(other) if not isinstance(other, datetime.datetime): return NotImplemented if not isinstance(other, self.__class__): if other.tzinfo is None: other = pendulum.naive( other.year, other.month, other.day, other.hour, other.minute, other.second, other.microsecond, ) else: other = pendulum.instance(other) return other.diff(self, False)
def test_subtract(): dt = pendulum.naive(2013, 3, 31, 2, 30) new = dt.subtract(days=3) assert_datetime(new, 2013, 3, 28, 2, 30)
def test_add(): dt = pendulum.naive(2013, 3, 31, 2, 30) new = dt.add(days=3) assert_datetime(new, 2013, 4, 3, 2, 30)
def test_naive_in_timezone_dst(): dt = pendulum.naive(2013, 3, 31, 2, 30) new = dt.in_timezone("Europe/Paris") assert_datetime(new, 2013, 3, 31, 3, 30) assert new.timezone_name == "Europe/Paris"
def test_naive_subtract(): dt = pendulum.naive(2013, 3, 31, 1, 30) new = dt.subtract(hours=1) assert_datetime(new, 2013, 3, 31, 0, 30)
def test_naive_add(): dt = pendulum.naive(2013, 3, 31, 1, 30) new = dt.add(hours=1) assert_datetime(new, 2013, 3, 31, 2, 30)
import pendulum dt = pendulum.datetime(2019, 5, 1) print(type(dt)) pendulum.datetime(2019, 5, 1, tz="Asia/Seoul") dt = pendulum.local(2019, 5, 1) print(dt.timezone.name) now = pendulum.now() print(now) naive = pendulum.naive(2019, 5, 1) print(naive.timezone) dt = pendulum.parse('2019-05-01T22:00:00') print(dt) pendulum.set_locale('ko') print(pendulum.now().add(years=1).diff_for_humans()) dt = pendulum.parse('2019-05-01T22:00:00') print(dt.year) print(dt.month) print(dt.day) print(dt.hour) print(dt.minute) print(dt.second) print(dt.microsecond)
def get_start_date(): return pendulum.naive(2018, 1, 17)
def test_naive_in_timezone(): dt = pendulum.naive(2013, 3, 31, 1, 30) new = dt.in_timezone('Europe/Paris') assert_datetime(new, 2013, 3, 31, 1, 30) assert new.timezone_name == 'Europe/Paris'