Coverage for /Users/davegaeddert/Developer/dropseed/plain/plain/plain/utils/duration.py: 18%

1 import datetime

2

3

4 def _get_duration_components(duration):

5 days = duration.days

6 seconds = duration.seconds

7 microseconds = duration.microseconds

8

9 minutes = seconds // 60

10 seconds %= 60

11

12 hours = minutes // 60

13 minutes %= 60

14

15 return days, hours, minutes, seconds, microseconds

16

17

18 def duration_string(duration):

19 """Version of str(timedelta) which is not English specific."""

20 days, hours, minutes, seconds, microseconds = _get_duration_components(duration)

21

22 string = f"{hours:02d}:{minutes:02d}:{seconds:02d}"

23 if days:

24 string = f"{days} " + string

25 if microseconds:

26 string += f".{microseconds:06d}"

27

28 return string

29

30

31 def duration_iso_string(duration):

32 if duration < datetime.timedelta(0):

33 sign = "-"

34 duration *= -1

35 else:

36 sign = ""

37

38 days, hours, minutes, seconds, microseconds = _get_duration_components(duration)

39 ms = f".{microseconds:06d}" if microseconds else ""

40 return f"{sign}P{days}DT{hours:02d}H{minutes:02d}M{seconds:02d}{ms}S"

41

42

43 def duration_microseconds(delta):

44 return (24 * 60 * 60 * delta.days + delta.seconds) * 1000000 + delta.microseconds