-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
46 lines (33 loc) · 1.78 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Convert datetimes to and from strings.
NASA's dataset provides timestamps as naive datetimes (corresponding to UTC).
The `cd_to_datetime` function converts a string, formatted as the `cd` field of
NASA's close approach data, into a Python `datetime`
The `datetime_to_str` function converts a Python `datetime` into a string.
Although `datetime`s already have human-readable string representations, those
representations display seconds, but NASA's data (and our datetimes!) don't
provide that level of resolution, so the output format also will not.
"""
import datetime
def cd_to_datetime(calendar_date):
"""Convert a NASA-formatted calendar date/time description into a datetime.
NASA's format, at least in the `cd` field of close approach data, uses the
English locale's month names. For example, December 31st, 2020 at noon is:
2020-Dec-31 12:00
This will become the Python object
`datetime.datetime(2020, 12, 31, 12, 0)`.
:param calendar_date: A calendar date in YYYY-bb-DD hh:mm format.
:return: A naive `datetime` corresponding to the given calendar date and
time.
"""
return datetime.datetime.strptime(calendar_date, "%Y-%b-%d %H:%M")
def datetime_to_str(dt):
"""Convert a naive Python datetime into a human-readable string.
The default string representation of a datetime includes seconds; however,
our data isn't that precise, so this function only formats the year, month,
date, hour, and minute values. Additionally, this function provides the
date in the usual ISO 8601 YYYY-MM-DD format to avoid ambiguities with
locale-specific month names.
:param dt: A naive Python datetime.
:return: That datetime, as a human-readable string without seconds.
"""
return datetime.datetime.strftime(dt, "%Y-%m-%d %H:%M")