Created
April 26, 2020 04:17
-
-
Save wassname/67ba9c8a1de0c1b5059838bdae9a4b9f to your computer and use it in GitHub Desktop.
date utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def row2date(row, tz="Australia/Perth"): | |
"""Parse time columns.""" | |
return pd.Timestamp( | |
year=int(row.Year), | |
month=int(row.Month), | |
day=int(row.Day), | |
hour=int(row.Hour), | |
minute=int(row.Minute), | |
second=int(row.Seconds), | |
tz=tz, | |
) # .tz_convert(0) | |
def mat_datednum2timestamp(n): | |
"""Convert matlab timenum to python timestamps""" | |
c = 62167305600 # offset in seconds | |
s = n * (60 * 60 * 24) # convert from days to seconds | |
return s - c | |
# Datetimes to file name safe strings | |
# also see https://gist.github.com/wassname/1393c4a57cfcbf03641dbc31886123b8 | |
# see https://strftime.org/ | |
import datetime # don't use | |
datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).strftime('%Y-%m-%d_%H%M%S%z') | |
# '2020-04-26_041329+0000' | |
import arrow | |
arrow.utcnow().format('YYYY-MM-DD_HHmmssZ') | |
#'2020-04-26_040742+0000' | |
# | |
import pandas as pd | |
pd.Timestamp.utcnow().strftime("%Y-%m-%d_%H:%M:%S%z").replace(":", "") | |
# '2020-04-26T041610+0000' | |
pd.Timestamp.utcnow().isoformat('_').replace(':','') | |
# '2020-04-26_041700.704937+0000' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment