在 Python 中将天十进制数数组转换为 unix 时间戳
Converting a day decimal number array to unix timestamp in Python
我有一组数字(例如 279.341、279.345、279.348),它们与 2017 年的日期和时间相关(应该是 2017 年 10 月 6 日)。为了能够将此数据与另一个数据集进行比较,我需要将该数组转换为 UNIX 时间戳数组。
我已经在 matlab 中成功完成了类似的操作(下面的代码),但不知道如何将其转换为 Python。
MatLab:
adcpTimeStr = datestr(adcp.adcp_day_num,'2017 mmm dd HH:MM:SS');
adcpTimeRaw = datetime(adcpTimeStr,'InputFormat','yyyy MMM dd HH:mm:ss');
adcpTimenumRaw = datenum(adcpTimeRaw)';
将数组转换为 UNIX 时间戳的好方法是什么?
假设这些数字是一年中的小数天数 (UTC) 并且年份是 2017 年,在 Python 中你会做
from datetime import datetime, timedelta, timezone
year = datetime(2017,1,1, tzinfo=timezone.utc) # the starting point
doy = [279.341, 279.345, 279.348]
# add days to starting point as timedelta and call timestamp() method:
unix_t = [(year+timedelta(d)).timestamp() for d in doy]
# [1507363862.4, 1507364208.0, 1507364467.2]
我有一组数字(例如 279.341、279.345、279.348),它们与 2017 年的日期和时间相关(应该是 2017 年 10 月 6 日)。为了能够将此数据与另一个数据集进行比较,我需要将该数组转换为 UNIX 时间戳数组。
我已经在 matlab 中成功完成了类似的操作(下面的代码),但不知道如何将其转换为 Python。
MatLab:
adcpTimeStr = datestr(adcp.adcp_day_num,'2017 mmm dd HH:MM:SS');
adcpTimeRaw = datetime(adcpTimeStr,'InputFormat','yyyy MMM dd HH:mm:ss');
adcpTimenumRaw = datenum(adcpTimeRaw)';
将数组转换为 UNIX 时间戳的好方法是什么?
假设这些数字是一年中的小数天数 (UTC) 并且年份是 2017 年,在 Python 中你会做
from datetime import datetime, timedelta, timezone
year = datetime(2017,1,1, tzinfo=timezone.utc) # the starting point
doy = [279.341, 279.345, 279.348]
# add days to starting point as timedelta and call timestamp() method:
unix_t = [(year+timedelta(d)).timestamp() for d in doy]
# [1507363862.4, 1507364208.0, 1507364467.2]