Working with time data

Working with time data#

Give your file, group or dataset a timestamp by calling .write_iso_timestamp()

import h5rdmtoolbox as h5tbx

with h5tbx.File() as h5:
    h5.attrs.write_iso_timestamp()
    h5.dump()
    • timestamp: 2026-01-18T09:05:23.267048

HDF5 cannot store datetime objects. The solution is to store them as string-datasets. Therefore, datetime is written to the HDF5 datasets in ISO-format. When data is requested, it is converted back to numpy.datetime64 format and fed into the xarray object. Note, that you may use the method create_time_dataset instead of constructing the string dataset yourself. In fact, this is recommended, because some attributes must be set in order to identify a dataset as a “time-data-dataset”:

import datetime
with h5tbx.File() as h5:
    h5.create_time_dataset('time',
                           data=[datetime.datetime.now(),
                                 datetime.datetime.now()+datetime.timedelta(hours=1),
                                 datetime.datetime.now()+datetime.timedelta(hours=3)],
                           time_format='iso', make_scale=True)
    h5.create_dataset('vel', data=[1,2,-3], attach_scale='time')
    v = h5.vel[()]
    t = h5.time[()]
    h5.dump()
      : [|S26]
      • time_format: %Y-%m-%dT%H:%M:%S.%f
      (time: 3) [int64]
v.plot()
[<matplotlib.lines.Line2D at 0x786fea2fb550>]
../../_images/d2aa2e69aca0eb948b4e78aa3dee645ce6f9ac3953d7b856b017fbce8b7bc922.png