10 examples of 'convert unix timestamp to datetime' in Python

Every line of 'convert unix timestamp 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
this disclaimer
150def utc_timestamp_to_datetime(timestamp):
151 """
152 Converts the given timestamp to a datetime instance.
153
154 :type timestamp: float
155 :rtype: datetime
156 """
157
158 if timestamp is not None:
159 return datetime.fromtimestamp(timestamp, utc)
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

16def timestamp2datetime(timestamp: int):
17 date = datetime.datetime.fromtimestamp(timestamp / 1000)
18 return date
72def from_timestamp(unixtime):
73 """
74 Args:
75 unixtime (int):
76
77 Returns:
78 datetime.datetime:
79
80 """
81 if not unixtime:
82 return None
83
84 return datetime.fromtimestamp(unixtime)
11def to_datetime(timestamp):
12 """Return datetime object from timestamp."""
13 return dt.fromtimestamp(time.mktime(
14 time.localtime(int(str(timestamp)[:10]))))
36def datetime_to_timestamp(from_datetime):
37 return time.mktime(from_datetime.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)
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())
11def _epoch_utc_to_datetime(epoch_utc):
12 """
13 Helper function for converting epoch timestamps (as stored in JWTs) into
14 python datetime objects (which are easier to use with sqlalchemy).
15 """
16 return datetime.fromtimestamp(epoch_utc)
14def datetimeAsTimestamp(dt):
15 return dt.timestamp()
83def datetime_to_timestamp(dt_):
84 """Convert given datetime object to timestamp in seconds."""
85 return dt_.replace(tzinfo=dt.timezone.utc).timestamp()

Related snippets