10 examples of 'convert datetime to seconds' in Python

Every line of 'convert 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 )
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)
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
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)
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')
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())
669def _convert_to_timedelta(self, seconds, millis=True):
670 return timedelta(seconds=seconds)
41def seconds_to_time(seconds:int):
42 try:
43 seconds = int(seconds)
44 if(seconds<0):
45 return "-00:01"
46 except:
47 return "-00:01"
48
49 m, s = divmod(seconds, 60)
50 h, m = divmod(m, 60)
51 if(h==0):
52 return "%02d:%02d"%(m,s)
53 else:
54 return "%d:%02d:%02d" % (h, m, s)
31def changeTime(dateTime, secondsPassed):
32 delta = datetime.timedelta(seconds = secondsPassed)
33 return dateTime + delta

Related snippets