-
-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
start_time broken with option hd_time_flags=1 #756
Comments
At first, thank you for taking up this issue! |
I can't see what else could be done |
Sorry for the late reaction, I was busy with other projects... I just tried the topic described above with version asammdf-7.3.12. The problem still exists: Please try to run the following code:
According to https://www.epochconverter.com I would expect The reason for this behaviour is (as described above) that asammdf converts the start time from the (guessed) local timezone to UTC in the start_time setter and back from UTC to the (once again guessed) local timezone in the start_time getter. This is not a problem as long as files are written and read only via asammdf (and only written and read in the same timezone) but may fail for 3rd party files which comply with the MDF spec. The solution would be to write the following timestamp to the header to suppress timezone conversion for tz-naive start_times: Thank you very much for your efforts in writing and maintaining this package! |
When I run this on my PC import asammdf
import datetime
mdf_file = asammdf.MDF(version="4.10")
start_time = datetime.datetime(2023, 4, 26, 8, 0, 0)
print(start_time.timestamp())
mdf_file.header.start_time = start_time
print(mdf_file.header.start_time.timestamp())
print(mdf_file.header.abs_time)
mdf_file.save(r"D:\TMP\local_time_test.mf4", overwrite=True) I get the following output
and the site gives this |
I can't wrap my head around this UTC/localtime conversion shit. Can you open a PR with a fix? |
I will try... |
Just created the PR (#857). Maybe there are more locations which need adjustment (file history, string representation, ...). EDIT: just added another commit with changes to file history time_stamp getter and setter and start_time_string function. If you want to see the effect, please open mf4 files from other sources than your asammdf lib (e.g. from the Vector MDF4 lib) which use hd_flags = 1. |
@markwaterbury: Did you see PR #857? There I tried to implement exactly the same: |
I did see your PR. It seemed like there might have still been some confusion from @danielhrisca, so I was just trying to provide additional context for this issue (which seems like it should still be open). |
Hello @chr-sy @markwaterbury and sorry for the long delay. I've merged the PR into the development branch. Thanks! |
This is working from the standpoint of not offsetting the time from UTC now. However, I just saw the following from above:
and I see that in the development branch, there is no timezone data on the start_time when time_flags==1 now. I think the behavior it had before ( Example, time should be 08:52:55 with the local timezone assumed: |
Sorry @markwaterbury, I disagree about that! If you have any way to get a valid tzlocal, why should you use time_flags=1? Please bear in mind that, according to the MDF spec, "(time_flags=1) should only be used if UTC time is unknown" (see Table 14: Structure of HDBLOCK). In my understanding, it is pretty straightforward to get a tz naive timestamp in such a case because a tz naive input is the only scenario to use time_flags=1. |
When the file was created, there was no way to get the local timezone, because it (and UTC time) were unknown. When I read the above from the spec, it seems that local time would have to be assumed whenever or wherever the file is read, even if that is different than the (unknown) timezone in which the file was created. I thought the best way to do this would be to assign |
I think there is another problem with start times which renders this function kind of broken.
The problem occurs for cases with hd_time_flags=1 (i.e. "local time") set.
According to the MDF spec, activating this bit means that "the start time stamp in nanoseconds represents the local time instead of the UTC time".
In contrast, asammdf produces the respective value (HeaderBlock.abs_time) by means of the datetime.datetime.timestamp() function (within the start_time setter function). This function produces an UTC timestamp (i.e. ns from Unix epoch in UTC) from the start time (which contradicts the MDF spec!)
During asammdf read, this value is processed via the datetime.datetime.fromtimestamp() function, which takes UTC timestamps and converts the resulting datetime to the local time zone.
Thus, times are processed correctly as long as you only use asammdf for read and write operations. Problems arise once you read files generated from 3rd party tools with asammdf. If these tools implement the MDF spec correctly, they write "local" timestamps to the file, which are interpreted as UTC timestamps by asammdf (or rather the underlying standard libs). As a result, time zone differences are added twice resulting in wrong times (as can be seen in the screenshots above).
Now how to resolve this issue?
I'd suggest to add some simple steps for cases with hd_time_flags=1:
Originally posted by @chr-sy in #675 (comment)
The text was updated successfully, but these errors were encountered: