5 examples of 'python format timedelta' in Python

Every line of 'python format timedelta' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
259def format_as_timedelta(self):
260 """Format seconds as timedelta."""
261 # Do we want this as a Formatter type also?
262 tmp = datetime.timedelta(seconds=self)
263 # Filter out microseconds from str format.
264 # timedelta does not have a __format__ method, and we don't want to
265 # recode it (we would need to handle i18n of "days").
266 obj = datetime.timedelta(days=tmp.days, seconds=tmp.seconds)
267 return str(obj)
Important

Use secure code every time

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

75def formatTimedelta(td):
76 """Format timedelta to String
77 """
78 if not isinstance(td, timedelta):
79 return ""
80 days, seconds = td.days, td.seconds
81 hours = days * 24 + seconds // 3600
82 minutes = (seconds % 3600) // 60
83 seconds = seconds % 60
84 return "%d:%s:%s" % (hours, str(minutes).zfill(2), str(seconds).zfill(2))
57def human_friendly_timedelta(td):
58 days = td.days
59 hours, remainder = divmod(td.seconds, 3600)
60 minutes, seconds = divmod(remainder, 60)
61 pieces = []
62 if days:
63 pieces.append(("%d days" % days) if days > 1 else "1 day")
64 if hours:
65 pieces.append(("%d hours" % hours) if hours > 1 else "1 hour")
66 if minutes:
67 pieces.append(("%d minutes" % minutes) if minutes > 1 else "1 minute")
68 if seconds:
69 pieces.append(("%d seconds" % seconds) if seconds > 1 else "1 second")
70 return ", ".join(pieces)
89def date_interval_sql(self, timedelta):
90 """
91 Implements the interval functionality for expressions
92 format for Oracle:
93 INTERVAL '3 00:03:20.000000' DAY(1) TO SECOND(6)
94 """
95 minutes, seconds = divmod(timedelta.seconds, 60)
96 hours, minutes = divmod(minutes, 60)
97 days = str(timedelta.days)
98 day_precision = len(days)
99 fmt = "INTERVAL '%s %02d:%02d:%02d.%06d' DAY(%d) TO SECOND(6)"
100 return fmt % (days, hours, minutes, seconds, timedelta.microseconds,
101 day_precision), []
10def timedelta(value, arg=None):
11 if not value:
12 return ''
13 if arg:
14 cmp = arg
15 else:
16 cmp = datetime.now()
17 if settings.USE_TZ:
18 cmp = make_aware(cmp, get_default_timezone())
19 if value > cmp:
20 return "in %s" % timesince(cmp, value)
21 else:
22 return "%s ago" % timesince(value, cmp)

Related snippets