-
Notifications
You must be signed in to change notification settings - Fork 32
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
[Bug]: loss of relevant time zone information in datetime objects #558
Comments
By newest do you mean the newest release or the tip of master? I would recommend the tip of master. Also, as mentioned in the error, you will need to set the relevant time zone using the TimeZone property in your datetime object. The setter should do this automatically, however. |
Hi Lawrence, thank you very much for your quick reply! I meant the tip of master, I apologize for the confusion. I did set the timezone property, the error remained. I thought, the problem is that I cannot assign any datatime objects to the nwbFile without them being stored in a cell. Or are they supposed to be stored in a cell and then exported as datetime objects anyway? Thanks a lot for your help! |
I believe them being in cells is intentional but I also don't think that is relevant to the pynwb error. You may also need to set the time to a non-zero value along with a time zone in order for the format to be correct. The Pynwb error is just complaining that the string format is not iso8601. |
thank you very much! I will try this out tomorrow and let you know of the result! |
Hi Lawrence, your advice was indeed correct! If, the times are set to non-zero values, everything works fine. I greatly apprechiate your help! Best, Tim |
Hi @NeuroGuth , Please try not to edit the same post over and over, it makes it difficult to know where the conversation is going. The main issue appears to be the format detection for datetime objects in this snippet: matnwb/+types/+util/correctType.m Lines 76 to 85 in 33b8178
When the special case of time being all zeros occurs, the formatter assumed that the time was invalid and dropped both the time and any relevant time zones. Since this causes errors in pynwb, I have now changed the snippet such that: matnwb/+types/+util/correctType.m Lines 76 to 83 in 9d77234
It only checks for timezone and assumes that the time is always present. In fact, there really was no way to know if hours/minutes/seconds were set, just if TimeZone was never provided. If you can, please pull from master and let me know if this worked for you. |
Hi Lawrence, great! Thank you very much, that fixes the issue! Maybe one could even consider the following code: function Datetime = formatDatetime(Datetime)
formatString = 'yyyy-MM-dd''T''HH:mm:ss.SSSSSSZZZZZ';
if isempty(Datetime.TimeZone)
localTime = datetime('now', 'TimeZone', 'local');
localTime.Format = formatString;
Datetime.TimeZone = localTime.TimeZone;
warning('no time zone specified - set to local time zone');
end
Datetime.Format = formatString;
end This might improve compatibility with pyNWB, if someone forgets to add a time zone. It might also be helpful, if pyNWB wouldn't require a time zone to read in the nwb-file. Thanks a lot Lawrence! |
What happened?
Hello,
I have an issue reading in .nwb files in Python which I have created in Matlab.
I tried to assign datetime objects to the properties 'timestamps_reference_time' and 'session_start_time' but it would always result in the assignment of cells containing the datetime objects.
When I export an .nwb-file in Matlab, I cannot read it in using Python. I get the following error message:
"Could not construct NWBFile object due to: 'timestamps_reference_time' must be a timezone-aware datetime object."
I think that there might be an issue in the assignment of the datetime object to the nwbFile.
Any help will be greatly apprechiated!
Steps to Reproduce
Error Message
Operating System
Windows
Matlab Version
2023a
Code of Conduct
The text was updated successfully, but these errors were encountered: