Skip to content

Commit

Permalink
Add datime <-> Python datetime conversion functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski committed Apr 9, 2021
1 parent 95bd056 commit fea04ad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/uproot/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import absolute_import

import datetime
import glob
import numbers
import os
Expand Down Expand Up @@ -729,3 +730,31 @@ def damerau_levenshtein(a, b, ratio=False):
return M[len(a)][len(b)]
else:
return (len(a) + len(b)) - M[len(a)][len(b)] / (len(a) + len(b))


def code_to_datetime(code):
"""
Converts a ROOT datime code into a Python datetime.
"""
return datetime.datetime(
((code & 0b11111100000000000000000000000000) >> 26) + 1995,
((code & 0b00000011110000000000000000000000) >> 22),
((code & 0b00000000001111100000000000000000) >> 17),
((code & 0b00000000000000011111000000000000) >> 12),
((code & 0b00000000000000000000111111000000) >> 6),
((code & 0b00000000000000000000000000111111)),
)


def datetime_to_code(dt):
"""
Converts a Python datetime into a ROOT datime code.
"""
return (
((dt.year - 1995) << 26)
| (dt.month << 22)
| (dt.day << 17)
| (dt.hour << 12)
| (dt.minute << 6)
| (dt.second)
)
8 changes: 8 additions & 0 deletions tests/test_0322-writablefile-infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import skhep_testdata

import uproot
import uproot._util
import uproot.writing

ROOT = pytest.importorskip("ROOT")
Expand Down Expand Up @@ -103,3 +104,10 @@ def test_subsubdir(tmp_path):
"wowzers/yikes/you": "TObjString",
"wowzers/yikes/one_more": "TObjString",
}


def test_little_datime_functions():
assert (
uproot._util.datetime_to_code(uproot._util.code_to_datetime(1762860281))
== 1762860281
)

0 comments on commit fea04ad

Please sign in to comment.