diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-01-05 10:38:34 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-01-05 10:38:34 +0000 |
commit | e3bdad36cc3a1a00c1e6772ca1c1898085ab73e0 (patch) | |
tree | 34512072a667ae716fd262e7b37e733e60fe4d89 /docs/docs/comparison.md | |
parent | Adding upstream version 2.1.2. (diff) | |
download | pendulum-e3bdad36cc3a1a00c1e6772ca1c1898085ab73e0.tar.xz pendulum-e3bdad36cc3a1a00c1e6772ca1c1898085ab73e0.zip |
Adding upstream version 3.0.0~a1.upstream/3.0.0_a1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'docs/docs/comparison.md')
-rw-r--r-- | docs/docs/comparison.md | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/docs/docs/comparison.md b/docs/docs/comparison.md new file mode 100644 index 0000000..6be8d0d --- /dev/null +++ b/docs/docs/comparison.md @@ -0,0 +1,77 @@ +# Comparison + +Simple comparison is offered up via the basic operators. +Remember that the comparison is done in the UTC timezone +so things aren't always as they seem. + +```python +>>> import pendulum + +>>> first = pendulum.datetime(2012, 9, 5, 23, 26, 11, 0, tz='America/Toronto') +>>> second = pendulum.datetime(2012, 9, 5, 20, 26, 11, 0, tz='America/Vancouver') + +>>> first.to_datetime_string() +'2012-09-05 23:26:11' +>>> first.timezone_name +'America/Toronto' +>>> second.to_datetime_string() +'2012-09-05 20:26:11' +>>> second.timezone_name +'America/Vancouver' + +>>> first == second +True +>>> first != second +False +>>> first > second +False +>>> first >= second +True +>>> first < second +False +>>> first <= second +True + +>>> first = first.on(2012, 1, 1).at(0, 0, 0) +>>> second = second.on(2012, 1, 1).at(0, 0, 0) +# tz is still America/Vancouver for second + +>>> first == second +False +>>> first != second +True +>>> first > second +False +>>> first >= second +False +>>> first < second +True +>>> first <= second +True +``` + +To handle the most used cases there are some simple helper functions. +For the methods that compare to `now()` (ex. `is_today()`) in some manner +the `now()` is created in the same timezone as the instance. + +```python +>>> import pendulum + +>>> dt = pendulum.now() + +>>> dt.is_past() +>>> dt.is_leap_year() + +>>> born = pendulum.datetime(1987, 4, 23) +>>> not_birthday = pendulum.datetime(2014, 9, 26) +>>> birthday = pendulum.datetime(2014, 4, 23) +>>> past_birthday = pendulum.now().subtract(years=50) + +>>> born.is_birthday(not_birthday) +False +>>> born.is_birthday(birthday) +True +>>> past_birthday.is_birthday() +# Compares to now by default +True +``` |