# How to use the pendulum.Time function in pendulum

## To help you get started, we’ve selected a few pendulum examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

sdispater / pendulum / tests / time_tests / test_testing_aids.py View on Github
``````def test_context_manager(self):
test_now = Time.now().subtract(hours=1)

with Time.test(test_now):
self.assertEqual(test_now, Time.now())

self.assertNotEqual(test_now, Time.now())``````
sdispater / pendulum / tests / time / test_diff.py View on Github
``````def test_diff_in_hours_negative_with_sign():
dt = Time(12, 34, 56)
assert dt.diff(dt.subtract(hours=2).add(seconds=3600), False).in_hours() == -1``````
Delgan / loguru / tests / test_rotation.py View on Github
``````    (pendulum.Time(18, 30, 11, 123), [1, 5.51, 20, 24, 40]),
("2 h", [1, 2, 0.9, 0.5, 10]),
("1 hour", [0.5, 1, 0.1, 100, 1000]),
("7 days", [24 * 7 - 1, 1, 48, 24 * 10, 24 * 365]),
("1h 30 minutes", [1.4, 0.2, 1, 2, 10]),
("1 w, 2D", [24 * 8, 24 * 2, 24, 24 * 9, 24 * 9]),
("1.5d", [30, 10, 0.9, 48, 35]),
("1.222 hours, 3.44s", [1.222, 0.1, 1, 1.2, 2]),
(datetime.timedelta(hours=1), [0.9, 0.2, 0.7, 0.5, 3]),
(pendulum.Interval(minutes=30), [0.48, 0.04, 0.07, 0.44, 0.5]),
(lambda t: t.add(months=1).start_of('month').at(13, 00, 00), [12 * 24, 26, 31 * 24 - 2, 2, 24 * 60]),
('hourly', [0.9, 0.2, 0.8, 3, 1]),
('daily', [11, 1, 23, 1, 24]),
('WEEKLY', [11, 2, 24 * 6, 24, 24 * 7]),
(' mOnthLY', [0, 24 * 13, 29 * 24, 60 * 24, 24 * 35]),
('yearly ', [100, 24 * 7 * 30, 24 * 300, 24 * 100, 24 * 400]),
])``````
sdispater / pendulum / tests / test_global.py View on Github
``````def test_set_locale_is_global(self):
dt = pendulum.create(2000, 11, 10, 12, 34, 56, 123456)
pendulum.set_formatter('alternative')
pendulum.set_locale('fr')

self.assertEqual(pendulum.DateTime.get_locale(), 'fr')
self.assertEqual(pendulum.Date.get_locale(), 'fr')
self.assertEqual(pendulum.Time.get_locale(), 'fr')

self.assertEqual(
dt.format('MMMM'),
'novembre'
)
self.assertEqual(
dt.date().format('MMMM'),
'novembre'
)``````
sdispater / pendulum / tests / test_parsing.py View on Github
``````assert isinstance(dt, pendulum.DateTime)
assert_datetime(dt, 2016, 10, 16, 12, 34, 56, 123456)
assert 5400 == dt.offset

text = "2016-10-16"

dt = pendulum.parse(text, exact=True)

assert isinstance(dt, pendulum.Date)
assert_date(dt, 2016, 10, 16)

text = "12:34:56.123456"

dt = pendulum.parse(text, exact=True)

assert isinstance(dt, pendulum.Time)
assert_time(dt, 12, 34, 56, 123456)

text = "13:00"

dt = pendulum.parse(text, exact=True)

assert isinstance(dt, pendulum.Time)
assert_time(dt, 13, 0, 0)``````
sdispater / pendulum / tests / time / test_diff.py View on Github
``````def test_diff_in_minutes_positive_big():
dt = Time(12, 34, 56)
sdispater / pendulum / tests / datetime / test_getters.py View on Github
``````def test_time():
dt = pendulum.datetime(2016, 10, 20, 10, 40, 34, 123456)
t = dt.time()
assert isinstance(t, pendulum.Time)
assert_time(t, 10, 40, 34, 123456)``````
sdispater / pendulum / tests / time / test_strings.py View on Github
``````def test_to_string():
d = Time(1, 2, 3)
assert str(d) == "01:02:03"
d = Time(1, 2, 3, 123456)
assert str(d) == "01:02:03.123456"``````
sdispater / pendulum / tests / time / test_sub.py View on Github
``````def test_subtract_time():
t = Time(12, 34, 56)
t1 = Time(1, 1, 1)
t2 = time(1, 1, 1)
t3 = time(1, 1, 1, tzinfo=pytz.timezone("Europe/Paris"))

diff = t - t1
assert isinstance(diff, pendulum.Duration)
assert_duration(diff, 0, hours=11, minutes=33, seconds=55)

diff = t1 - t
assert isinstance(diff, pendulum.Duration)
assert_duration(diff, 0, hours=-11, minutes=-33, seconds=-55)

diff = t - t2
assert isinstance(diff, pendulum.Duration)
assert_duration(diff, 0, hours=11, minutes=33, seconds=55)``````
sdispater / pendulum / tests / time / test_fluent_setters.py View on Github
``````def test_replace():
t = Time(12, 34, 56, 123456)
t = t.replace(1, 2, 3, 654321)

assert isinstance(t, Time)
assert_time(t, 1, 2, 3, 654321)``````

## pendulum

Python datetimes made easy

MIT