10 examples of 'add timedelta to datetime python' in Python

Every line of 'add timedelta to datetime python' 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
669def _convert_to_timedelta(self, seconds, millis=True):
670 return timedelta(seconds=seconds)
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

129def __get_timedelta_sec(self):
130 dt = self.__datetime.utcoffset()
131
132 return int(
133 dt.days * self.__DAYS_TO_SECONDS_COEF
134 + float(dt.seconds)
135 + dt.microseconds / self.__MICROSECONDS_TO_SECONDS_COEF
136 )
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), []
162def maybe_timedelta(delta):
163 """Convert integer to timedelta, if argument is an integer."""
164 if isinstance(delta, numbers.Real):
165 return timedelta(seconds=delta)
166 return delta
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))
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)
39def seconds_to_timedelta(value):
40 """
41 Converst the given value in secodns to datetime.timedelta.
42 """
43 _days = value // 86400
44 _hours = (value // 3600) % 24
45 _mins = (value // 60) % 60
46 _secs = value % 60
47 return timedelta(days=_days, hours=_hours, minutes=_mins, seconds=_secs)
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)
184def to_python(self, value):
185 if value is None or isinstance(value, datetime.timedelta):
186 return value
187
188 if isinstance(value, (int, long)):
189 return datetime.timedelta(seconds=value)
190
191 # Try to convert string time to timedelta instance
192 if isinstance(value, basestring):
193 new_value = str_to_timedelta(value)
194
195 if new_value is not None:
196 return new_value
197
198 # If cannot convert value to timedelta raise ``AssertionError``
199 assert False, (value, type(value))
42def utcoffset(self, dt):
43 return datetime.timedelta(0)

Related snippets