-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtools.py
More file actions
56 lines (41 loc) · 1.49 KB
/
tools.py
File metadata and controls
56 lines (41 loc) · 1.49 KB
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
47
48
49
50
51
52
53
54
55
56
"""
This file is part of pysofar: A client for interfacing with Sofar Ocean's Spotter API
Contents: Functions useful for date/time related parsing and formatting
Copyright 2019-2024
Sofar Ocean Technologies
Authors: Mike Sosa et al.
"""
import time
import calendar
import datetime
def time_stamp_to_epoch(date_string):
"""
:param date_string: Date string formatted as iso
:return:
"""
return calendar.timegm(time.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%f%z'))
def parse_date(date_object):
"""
:param date_object: Give in utc format, either epoch, string, or datetime object
:return: String date formatted in ISO 8601 format
"""
_date = None
if isinstance(date_object, (int, float)):
_date = datetime.datetime.utcfromtimestamp(date_object)
elif isinstance(date_object, str):
# time includes microseconds
formatting = "%Y-%m-%dT%H:%M:%S.%f%z"
if "Z" not in date_object and "+" not in date_object:
formatting = "%Y-%m-%dT%H:%M:%S.%f"
if "." not in date_object:
formatting = "%Y-%m-%dT%H:%M:%S"
if "T" not in date_object:
formatting = "%Y-%m-%d"
_date = datetime.datetime.strptime(date_object, formatting)
elif isinstance(date_object, datetime.datetime):
_date = date_object
else:
raise Exception('Invalid Date Format')
# make zone unaware
f_string = _date.replace(tzinfo=None).isoformat(timespec="milliseconds")
return f"{f_string}Z"