10 examples of 'convert string to timestamp python' in Python

Every line of 'convert 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
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)
47def timestamp_str(timestamp):
48 return timestamp.isoformat() if timestamp else None
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
75@staticmethod
76def convert_timestamp(str):
77
78 ts = time.strptime(str,'%a %b %d %H:%M:%S +0000 %Y')
79 ts = time.strftime('%Y-%m-%d %H:%M:%S', ts)
80
81 return ts
65def parse_timestamp(format, timestamp_str):
66 """Utility function to parse a timestamp into a datetime.datetime.
67
68 Python 2.5 and up have datetime.strptime, but Python 2.4 does not,
69 so we roll our own as per the documentation.
70
71 If passed a timestamp_str of None, we will return None as a convenience.
72 """
73 if not timestamp_str:
74 return None
75
76 return datetime.datetime(*time.strptime(timestamp_str, format)[0:6])
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]))))
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)
16def timestamp2datetime(timestamp: int):
17 date = datetime.datetime.fromtimestamp(timestamp / 1000)
18 return date
59def convert_timestamp_to_mysql(timestamp):
60 """
61 Convert timestamp to correct format for MySQL
62
63 :param timestamp: string containing timestamp in APIC format
64 :return: string containing timestamp in MySQL format
65 """
66 (resp_ts, remaining) = timestamp.split('T')
67 resp_ts += ' '
68 resp_ts = resp_ts + remaining.split('+')[0].split('.')[0]
69 return resp_ts

Related snippets