10 examples of 'python datetime to seconds' in Python

Every line of 'python datetime to seconds' 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
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 )
72def datetime_to_secs(value):
73 """Convert a datetime object to the number of seconds since the UNIX epoch.
74
75 Args:
76 value (datetime): The datetime to convert.
77
78 Returns:
79 int: The number of seconds since the UNIX epoch.
80 """
81 return calendar.timegm(value.utctimetuple())
77def from_unix_seconds(seconds: Union[int, float]) -> datetime:
78 while seconds > MS_WATERSHED:
79 seconds /= 1000
80 dt = EPOCH + timedelta(seconds=seconds)
81 return dt.replace(tzinfo=timezone.utc)
62def secs2dt(seconds_param):
63 '''
64 Helper function to convert seconds since epoch into datetime. Naive datetime is treated as UTC
65 '''
66 return datetime.fromtimestamp(seconds_param)
617def datetime2timestamp(dt, default_timezone=None):
618 """Calculate the timestamp based on the given datetime instance.
619
620 :type dt: datetime
621 :param dt: A datetime object to be converted into timestamp
622 :type default_timezone: tzinfo
623 :param default_timezone: If it is provided as None, we treat it as tzutc().
624 But it is only used when dt is a naive datetime.
625 :returns: The timestamp
626 """
627 epoch = datetime.datetime(1970, 1, 1)
628 if dt.tzinfo is None:
629 if default_timezone is None:
630 default_timezone = tzutc()
631 dt = dt.replace(tzinfo=default_timezone)
632 d = dt.replace(tzinfo=None) - dt.utcoffset() - epoch
633 if hasattr(d, "total_seconds"):
634 return d.total_seconds() # Works in Python 2.7+
635 return (d.microseconds + (d.seconds + d.days * 24 * 3600) * 10**6) / 10**6
145def addSeconds(date, value):
146 """Add or subtract an amount of seconds to a given date and time.
147
148 Args:
149 date (datetime): The starting date.
150 value (int): The number of units to add, or subtract if the
151 value is negative.
152
153 Returns:
154 datetime: A new date object offset by the integer passed to
155 the function.
156 """
157 return date + timedelta(seconds=value)
35def timestamp_to_seconds(timestamp: str) -> int:
36 """Convert a timestamp string into a seconds value
37 Args:
38 timestamp: An ISO 8601 format string returned by calling isoformat()
39 on a `datetime.datetime` type timestamp.
40
41 Returns:
42 Timestamp in seconds.
43 """
44 ISO_DATETIME_REGEX = '%Y-%m-%dT%H:%M:%S.%fZ'
45 timestamp_str = datetime.datetime.strptime(timestamp, ISO_DATETIME_REGEX)
46 epoch_time_secs = calendar.timegm(timestamp_str.timetuple())
47 return epoch_time_secs
66def seconds_to_timestamp(seconds):
67 return pd.Timestamp(seconds, unit='s', tz='UTC')
669def _convert_to_timedelta(self, seconds, millis=True):
670 return timedelta(seconds=seconds)
237def toSeconds(time):#converts times to second of music (seconds/120)
238 time = datetime.datetime.strptime(time, "%Y-%m-%dT%H:%M:%S")
239 time = (time-starttime).total_seconds()
240 time = float(time)/120
241 return time

Related snippets