1nickt has asked for the wisdom of the Perl Monks concerning the following question:

Learned brethren,

I have a module that uses Time::Moment to produce timestamps for logging. The timestamps are supposed to be to microsecond accuracy. The code:

time => Time::Moment->now_utc->strftime('%FT%T%6f%Z')
produces timestamps with six decimal places wherever I use it on Linux and Darwin. This I expect because the doc shows:

%f
Replaced by the fractional second including the preceding decimal point or by an empty string if no fractional seconds are present.

%3f is replaced by decimal point and exactly three fractional digits (zero-padded on the right or truncated if needed) if fractional seconds are present.

I've released a module to the CPAN using this code, and it failed on a Windows tester:
# at t/013-context.t line 28. # Using Regexp on $data->{"time"} # got : '2021-05-01T18:58:28Z' # expect : (?^:^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{0,6}Z$) # Looks like you failed 1 test of 1.

So my question is whether this is something that is known to occur in Strawberry Perl on Windows, or did I make a mistake in reading the doc, or ... ?

Thanks for any suggestions!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re: Time::Moment precision on Windows?
by 1nickt (Canon) on May 01, 2021 at 21:25 UTC

    Answering my own question: I misinterpreted the doc, which is not very clear on this point IMHO.

    $ perl -MTime::Moment -E 'while (1) { $x = Time::Moment->now_utc->strf +time("%FT%T%6f%Z"); say $x if $x !~ /\.\d{6}Z$/ }'
    ... prints out a value like "2021-05-01T20:58:09Z" on every complete second it hits.

    So I will manually pad with RHS zeroes.


    The way forward always starts with a minimal test.
Re: Time::Moment precision on Windows?
by johnaj (Sexton) on May 01, 2021 at 21:22 UTC
    DB<5> x Time::Moment->now_utc->strftime('%FT%T%6f%Z'); 0 '2021-05-01T21:20:17.131965Z'

    Works fine for me on Windows 7, Strawberry Perl (v5.32.0, MSWin32-x64-multi-thread).