-
Notifications
You must be signed in to change notification settings - Fork 140
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
Encoding/Decoding vector of Datetimes #1082
Comments
Could you provide example code of how you are writing the vector to a HDF5 file? |
Here's an example recreating your issue, and perhaps also solving it via julia> using HDF5, Dates
julia> dts = DateTime.([
"2022-05-04T11:38:49",
"2022-05-04T14:28:09",
"2022-05-05T10:43:37"
])
3-element Vector{DateTime}:
2022-05-04T11:38:49
2022-05-04T14:28:09
2022-05-05T10:43:37
julia> h5open("test.h5", "w") do h5f
ds = write_dataset(h5f, "dts", dts)
end
julia> dts = h5open("test.h5", "r") do h5f
h5f["dts"][:]
end
3-element Vector{NamedTuple{(:instant,), Tuple{NamedTuple{(:periods,), Tuple{NamedTuple{(:value,), Tuple{Int64}}}}}}}:
(instant = (periods = (value = 63787347529000,),),)
(instant = (periods = (value = 63787357689000,),),)
(instant = (periods = (value = 63787430617000,),),)
julia> reinterpret(DateTime, dts)
3-element reinterpret(DateTime, ::Vector{NamedTuple{(:instant,), Tuple{NamedTuple{(:periods,), Tuple{NamedTuple{(:value,), Tuple{Int64}}}}}}}):
2022-05-04T11:38:49
2022-05-04T14:28:09
2022-05-05T10:43:37
julia> dts = reinterpret(DateTime, dts)
3-element reinterpret(DateTime, ::Vector{NamedTuple{(:instant,), Tuple{NamedTuple{(:periods,), Tuple{NamedTuple{(:value,), Tuple{Int64}}}}}}}):
2022-05-04T11:38:49
2022-05-04T14:28:09
2022-05-05T10:43:37
julia> using HDF5, Dates
julia> h5open("test.h5", "w") do h5f
ds = write_dataset(h5f, "dts", dts)
end
julia> retrieved_dts = h5open("test.h5", "r") do h5f
h5f["dts"][:]
end
3-element Vector{NamedTuple{(:instant,), Tuple{NamedTuple{(:periods,), Tuple{NamedTuple{(:value,), Tuple{Int64}}}}}}}:
(instant = (periods = (value = 63787347529000,),),)
(instant = (periods = (value = 63787357689000,),),)
(instant = (periods = (value = 63787430617000,),),)
julia> retrieved_dts = reinterpret(DateTime, retrieved_dts)
3-element reinterpret(DateTime, ::Vector{NamedTuple{(:instant,), Tuple{NamedTuple{(:periods,), Tuple{NamedTuple{(:value,), Tuple{Int64}}}}}}}):
2022-05-04T11:38:49
2022-05-04T14:28:09
2022-05-05T10:43:37
julia> retrieved_dts[1]
2022-05-04T11:38:49 |
You could also retrieve the
|
@mkitti you beat me to the punch in including a workflow, thanks! Thank you also for introducing me to But what if the end user of the h5 file is not going to necessarily use Julia to read it? Maybe encoding the datetimes to something more generic could be useful? |
For a broadly interpretable date time, I would consider the ISO8601 date format strings. https://en.wikipedia.org/wiki/ISO_8601?wprov=sfla1 If you would like to understand how Julia's Datetime object works perhaps the Julia Discourse forum would be a good place to figure out. It seems to be milliseconds in UT time. |
I am writing a vector of Datetimes to an h5 file. Then I read it back and I get the following.
How is one supposed to interpret these values to recover the human readable Datetime? I thought they were unix times but they are exceedingly large for contemporary times!
Maybe this is something one is not supposed to do? I guess I could just do the conversion to unix time before writing to h5, but it'd be nice not have to do this every time!
The text was updated successfully, but these errors were encountered: