10 examples of 'extract date from timestamp python' in Python

Every line of 'extract date from 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
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])
4def parse_timestamp(timestamp):
5 """Convert an ISO 8601 timestamp into a datetime."""
6 return datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
48def timestamp_to_date(timestamp: int) -> datetime.date:
49 return datetime.datetime.fromtimestamp(timestamp).date()
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
54def totimestamp(inputdate, epoch=datetime(1970,1,1)):
55 dt = datetime.strptime(inputdate, '%Y-%m-%d')
56 td = dt - epoch
57 timestamp = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 1e6 # td.total_seconds()
58 return int(timestamp)
262def timestamp_parser(timestamp):
263 """Parse timestamp to datetime"""
264 return datetime.fromtimestamp(float(timestamp))
19def parse_date(date):
20 if not date:
21 return None
22 try:
23 return datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
24 except ValueError:
25 d = datetime.datetime.strptime(date, '%Y-%m-%d').date()
26 return d
26def parse_date(date):
27 try:
28 return datetime.strptime(date, "%Y-%m-%d").date()
29 except ValueError:
30 return None
53def timestamp2date(tstamp):
54 "Converts a unix timestamp to a Python datetime object"
55 dt = datetime.fromtimestamp(tstamp)
56 if not dt.hour + dt.minute + dt.second + dt.microsecond:
57 return dt.date()
58 else:
59 return dt
44def date_to_timestamp(d: datetime.date) -> int:
45 return (datetime.datetime.combine(d, datetime.datetime.min.time()) - epoch).total_seconds()

Related snippets