sdispater / pendulum

Python datetimes made easy
https://pendulum.eustace.io
MIT License
6.18k stars 381 forks source link

How does fractional seconds work? #576

Open setiat opened 2 years ago

setiat commented 2 years ago

Issue

Consider the example below:

>>> import pendulum as p
>>> test = p.from_format("13:00:00.010", "HH:mm:ss.SS")
>>> test
DateTime(2021, 11, 11, 13, 0, 0, 100000, tzinfo=Timezone('UTC'))
>>> test.format("SS")
'10'
>>> 

shouldn't the result be test.format("SS") return 01?

Renkoru commented 2 years ago

Hi @setiat, you have 3 fractional seconds in time you want to parse: "010" but in 'format string' you are using only 2: "SS"

I think that's why it behaves incorrectly, you get wrong DateTime object

setiat commented 2 years ago

Thanks @Renkoru

Do you know if there is any way parse fractional seconds on any length in pendulum?

Renkoru commented 2 years ago

@setiat, I think in your case you could try to use parse

>>> import pendulum as p
>>> dt = p.parse("13:00:00.010")
>>> dt
DateTime(2021, 11, 12, 13, 0, 0, 10000, tzinfo=Timezone('UTC'))

>>> dt.format("SS")
'01'

>>> dt = p.parse("13:00:00.001")
>>> dt
DateTime(2021, 11, 12, 13, 0, 0, 1000, tzinfo=Timezone('UTC'))