7 examples of 'timedelta to string' in Python

Every line of 'timedelta to string' 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
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)
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))
318def __str__(self):
319 return "period of {delta}, from {s.since} to {s.until}".format(
320 delta=format_timedelta(self.delta, locale='en_US'), s=self
321 )
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)
15def _delta_str(delta):
16 """Convert a timedelta to a nice string.
17
18 timedelta.__str__ prints out days and times awkwardly.
19
20 """
21 days, rem = divmod(delta.total_seconds(), SECONDS_PER_DAY)
22 hours, rem = divmod(rem, SECONDS_PER_HOUR)
23 minutes, rem = divmod(rem, SECONDS_PER_MINUTE)
24
25 result = []
26
27 if days:
28 result.append('{0} day(s)'.format(days))
29 if hours:
30 result.append('{0} hour(s)'.format(hours))
31 if minutes:
32 result.append('{0} minute(s)'.format(minutes))
33 return ', '.join(result)
40def __repr__(self):
41 if self.n == 1:
42 pretty_unit = self.unit_name
43 else:
44 pretty_unit = '{0}s'.format(self.unit_name)
45
46 return ''.format(self.n, pretty_unit)
17def secondsToStr(elapsed=None):
18 if elapsed is None:
19 return strftime("%Y-%m-%d %H:%M:%S", localtime())
20 else:
21 return str(timedelta(seconds=elapsed))

Related snippets