10 examples of 'python seconds to datetime' in Python

Every line of 'python seconds to datetime' 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
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())
11def to_datetime(timestamp):
12 """Return datetime object from timestamp."""
13 return dt.fromtimestamp(time.mktime(
14 time.localtime(int(str(timestamp)[:10]))))
263def pyCalendarToSQLTimestamp(pydt):
264
265 if pydt.isDateOnly():
266 return date(year=pydt.getYear(), month=pydt.getMonth(), day=pydt.getDay())
267 else:
268 return datetime(
269 year=pydt.getYear(),
270 month=pydt.getMonth(),
271 day=pydt.getDay(),
272 hour=pydt.getHours(),
273 minute=pydt.getMinutes(),
274 second=pydt.getSeconds(),
275 tzinfo=None
276 )
37def time_to_datetime(t):
38 return _combine_date_time(date.today(), t)
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
32def datetime2timestamp(dt=None, timezone='Asia/Shanghai'):
33 if dt is None:
34 dt = datetime.now()
35 tz = pytz.timezone(timezone)
36 dt = dt.replace(tzinfo=pytz.utc).astimezone(tz)
37 return calendar.timegm(dt.timetuple())
7def timestamp_to_datetime(s):
8 """
9 Convert a Core Data timestamp to a datetime. They're all a float of seconds since 1 Jan 2001. We calculate
10 the seconds in offset.
11 """
12 if not s:
13 return None
14
15 OFFSET = (datetime.datetime(2001, 1, 1, 0, 0, 0) - datetime.datetime.fromtimestamp(0)).total_seconds()
16 return datetime.datetime.fromtimestamp(s + OFFSET)
66def seconds_to_timestamp(seconds):
67 return pd.Timestamp(seconds, unit='s', tz='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)
36def datetime_to_timestamp(from_datetime):
37 return time.mktime(from_datetime.timetuple())

Related snippets