Skip to content

add millisecond and microsecond methods to duration #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/docs/duration.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ you can use the appropriate methods.

>>> it.total_seconds()
101700084.001234

>>> it.total_milliseconds()
101700084001.234

>>> it.total_microseconds()
101700084001234
```

Similarly, the `in_xxx()` methods return the total duration in each
Expand All @@ -156,6 +162,12 @@ supported unit as a truncated integer.

>>> it.in_seconds()
101700084

>>> it.in_milliseconds()
101700084001

>>> it.in_microseconds()
10170084001234
```

It also has a handy `in_words()` method, which determines the duration representation when printed.
Expand Down
2 changes: 2 additions & 0 deletions pendulum/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
SECONDS_PER_MINUTE = 60
SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE
SECONDS_PER_DAY = HOURS_PER_DAY * SECONDS_PER_HOUR
MS_PER_SECOND = 1000
US_PER_SECOND = 1000000
US_PER_MS = 1000

# Formats
ATOM = "YYYY-MM-DDTHH:mm:ssZ"
Expand Down
14 changes: 13 additions & 1 deletion pendulum/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pendulum.utils._compat import PYPY
from pendulum.utils._compat import decode

from .constants import SECONDS_PER_DAY
from .constants import SECONDS_PER_DAY, US_PER_MS, MS_PER_SECOND
from .constants import SECONDS_PER_HOUR
from .constants import SECONDS_PER_MINUTE
from .constants import US_PER_SECOND
Expand Down Expand Up @@ -129,6 +129,12 @@ def total_seconds(self):
+ self._microseconds
) / US_PER_SECOND

def total_milliseconds(self):
return self.total_seconds() * MS_PER_SECOND + self._microseconds / US_PER_MS

def total_microseconds(self):
return self.total_seconds() * US_PER_SECOND + self._microseconds

@property
def years(self):
return self._years
Expand Down Expand Up @@ -209,6 +215,12 @@ def in_minutes(self):
def in_seconds(self):
return int(self.total_seconds())

def in_milliseconds(self):
return int(self.total_milliseconds())

def in_microseconds(self):
return int(self.total_microseconds())

def in_words(self, locale=None, separator=" "):
"""
Get the current interval in words in the current locale.
Expand Down
10 changes: 10 additions & 0 deletions tests/duration/test_in_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ def test_in_minutes():
def test_in_seconds():
it = pendulum.duration(seconds=72)
assert it.in_seconds() == 72


def test_in_milliseconds():
it = pendulum.duration(milliseconds=1234)
assert it.in_milliseconds() == 1234


def test_in_microseconds():
it = pendulum.duration(microseconds=1234567)
assert it.in_microseconds() == 1234567
10 changes: 10 additions & 0 deletions tests/duration/test_total_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ def test_in_minutes():
def test_in_seconds():
it = pendulum.duration(seconds=72, microseconds=123456)
assert it.total_seconds() == 72.123456


def test_in_milliseconds():
it = pendulum.duration(seconds=1, milliseconds=234)
assert it.total_milliseconds() == 1234


def test_in_microseconds():
it = pendulum.duration(seconds=1, milliseconds=234, microseconds=567)
assert it.total_microseconds() == 1234567