从 Python 读取 TDMS 文件时的日期时间调整

DateTime adjustment when reading TDMS files from Python

我有一个 TDMS 文件,其中包含一堆具有相关检测数据的 DateTime 值。
我遇到的问题是:

TDMS file              >>>> Python Reads  
4/20/2021  12:00:01 AM >>>> 2021-04-20 04:00:00.597573  
4/20/2021  8:00:01 PM  >>>> 2021-04-21 00:00:00.570708 

由于不准确,这会扰乱向数据库的传输。

这是我的代码:

dfscaled = tdmsfile.__getitem__("Data (Scaled)").as_dataframe()  
for index, row in dfscaled.iterrows():
     print(row["Timestamp"])

我正在使用 NPTDMS 库。关于如何解决此问题的任何想法?

所以我最终联系了作者,他非常乐于提供回复:

TDMS files internally store times in UTC. You don't say how you're getting the values in the "TDMS file" column but I assume this is from a program that converts times into your local timezone to display them. There is an example of how to convert UTC times from a TDMS file into your local timezone in the documentation.

If you are storing these times in a database though, I would strongly recommend you store times in UTC rather than your local timezone as local times can be ambiguous due to daylight savings changes, and UTC is easily understood from anywhere in the world without having to know the local timezone where the data originated.

如果您仍然想从 UTC 更改为 EST,那么应该这样做:

fmt = "%Y-%m-%d %H:%M:%S.%f"
x=pd.to_datetime(row["Timestamp"]).tz_localize('UTC').tz_convert('US/Eastern').strftime(fmt)
dtm=pd.to_datetime(x[:-3])
print(dtm)