10 examples of 'string to timestamp python' in Python

Every line of 'string to timestamp 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
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)
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

47def timestamp_str(timestamp):
48 return timestamp.isoformat() if timestamp else None
117def timestamp_to_str(timestamp):
118 """Convert epoch timestamp to a time string in UTC.
119
120 Args:
121 timestamp: The epoch time, in seconds, to convert.
122 Returns:
123 String representation of the timestamp in UTC timezone.
124 """
125 if not timestamp:
126 return ''
127 if timestamp == sys.maxint:
128 # sys.maxint represents infinity.
129 return 'inf'
130 utc = pytz.timezone('UTC')
131 return datetime.datetime.fromtimestamp(timestamp, tz=utc).strftime(
132 '%Y-%m-%d %H:%M:%S %Z')
11def to_datetime(timestamp):
12 """Return datetime object from timestamp."""
13 return dt.fromtimestamp(time.mktime(
14 time.localtime(int(str(timestamp)[:10]))))
169def read_timestamp(timestamp_string):
170 """
171 Parse a timestamp string into a datetime object
172 :param timestamp_string:
173 :return:
174 """
175 try :
176 timestamp = dt.datetime.strptime(timestamp_string, '%a %b %d %H:%M:%S +0000 %Y').replace(
177 tzinfo=pytz.timezone('UTC'))
178
179 return timestamp.astimezone(pytz.timezone('UTC'))
180 except ValueError :
181 return None
14def datetimeAsTimestamp(dt):
15 return dt.timestamp()
16def timestamp2datetime(timestamp: int):
17 date = datetime.datetime.fromtimestamp(timestamp / 1000)
18 return date
50def timestamp_to_datetime(ts):
51 """
52 Convert timestamps to datetime objects
53 """
54 if ts is None:
55 return None
56 if isinstance(ts, (str, bytes)):
57 ts = float(ts)
58 return datetime.datetime.utcfromtimestamp(ts)
83def datetime_to_timestamp(dt_):
84 """Convert given datetime object to timestamp in seconds."""
85 return dt_.replace(tzinfo=dt.timezone.utc).timestamp()
11def timestamp_to_SQLstring(timestamp):
12 return str(timestamp)[:-6]

Related snippets