in reply to missing second of time

You could also try "fudging" your way to the next second.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11111868 use warnings; use Time::HiRes qw( time sleep ); my $fudgefactor = 0.01; while(1) { my $nextsecond = int time + 1; sleep $nextsecond - time - $fudgefactor; 1 while time < $nextsecond; printf "%.6f\n", time; }

Outputs:

1579972033.000003 1579972034.000004 1579972035.000004 1579972036.000004 1579972037.000004 1579972038.000005 1579972039.000003 1579972040.000004 1579972041.000004 1579972042.000003 1579972043.000003 1579972044.000004 1579972045.000003 1579972046.000005 1579972047.000003

Looks fairly consistent :)

Replies are listed 'Best First'.
Re^2: missing second of time
by stevieb (Canon) on Jan 25, 2020 at 17:41 UTC

    Nice. Here's a way using select(), so that there's no need for non-core modules (ie. Time::Hires):

    use strict; use warnings; my $fudgefactor = 0.01; while (1) { my $nextsecond = int time + 1; select(undef, undef, undef, $nextsecond - time - $fudgefactor); 1 while time < $nextsecond; printf "%.6f\n", time; }

    Output:

    1579973997.000000 1579973998.000000 1579973999.000000 1579974000.000000 1579974001.000000 ^C

      Time::HiRes is core Perl, as much as strict.

      $ corelist Time::HiRes Data for 2020-01-20 Time::HiRes was first released with perl v5.7.3

      Dave