chansen / p5-time-moment

Time::Moment represents an exact moment in time.
32 stars 8 forks source link

strftime('%f') does not zero-pad when nanoseconds is zero #14

Closed d5ve closed 9 years ago

d5ve commented 9 years ago

Hi,

I'm trying to use strftime() to output ISO 8601 <date>T<time>Z values. It's quite an edge-case, but when the nanoseconds value is zero, strftime() doesn't zero-pad the fractional seconds.

The two following one-liners show the difference in output beween 0 and 1 nanoseconds when strftime() is asked to pad fractional seconds to 9 digits.

$ perl -MTime::Moment -lwe "print Time::Moment->from_epoch(1439337975, 0)->strftime('%FT%X%9fZ')"
2015-08-12T00:06:15Z

$ perl -MTime::Moment -lwe "print Time::Moment->from_epoch(1439337975, 1)->strftime('%FT%X%9fZ')"
2015-08-12T00:06:15.000000001Z

Is this considered a bug?

Cheers,

Dave

chansen commented 9 years ago

Hi Dave,

The strftime() implementation has two conversion specifications for formatting fractional seconds, %f and %N. If you want to unconditionally output fractional seconds as nanoseconds, use the '.%9N' specification.

Example:

$ perl -MTime::Moment -lwe "print Time::Moment->from_epoch(1439337975, 0)->strftime('%FT%X.%9NZ')"
2015-08-12T00:06:15.000000000Z

$ perl -MTime::Moment -lwe "print Time::Moment->from_epoch(1439337975, 1)->strftime('%FT%X.%9NZ')"
2015-08-12T00:06:15.000000001Z

chansen

d5ve commented 9 years ago

Doh, I completely missed the "Replaced by the fractional second including the preceding decimal point or by an empty string if no fractional seconds are present." bit in the docs for %f.

%N is indeed exactly what I need here, thanks.