Using PyYaml
import yaml
yaml.full_load(StringIO('a: 16:00:00'))
# {'a': 57600}
yaml.full_load(StringIO('a: 09:31:00'))
# {'a': '09:31:00'}
Why is there a difference in those behaviors?
Older versions of YAML supported sexagesimal (base 60) numbers, intended for use for things like times. Instead of adding additional digits (like hexadecimal uses 0-9 and A-F), it simply uses decimal numbers 0-59 separated by :s. 16:00:00 is thus equivalent to
16*(60**2) + 0*60 + 0 == 57600.
PyYAML apparently still uses this older YAML specification.
09:30:00, however, does not start with a valid decimal: a leading zero indicates an octal number, but 09 is not a valid octal number. Not being able to parse this as any kind of known number (octal, decimal, or sexagesimal), PyYAML falls back to a string.
YAML can represent timestamps, but only if they consist of a date and an optional timestamp. PyYAML parses such timestamps as datetime.datetime objects, as seems reasonable.
>>> yaml.full_load(StringIO('a: 2022-12-21T09:31:00'))
{a: datetime.datetime(2022, 12, 21, 9, 31)}
I referenced an answer in a comment, https://stackoverflow.com/a/45165433/1126841, provided by the author of another package with does adhere to the YAML 1.2 specification, which will parse the value as a string, not a sexagesimal integer.
>>> from ruamel import yaml
>>> yaml.safe_load('a: 16:00:00')
{'a': '16:00:00'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With