Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!
Trying to get a timestamp format going and I can't understand how to get this to have this format:
1900-01-01 00:00:00.000
Any help about how to get to the format above?
my $tstamp = get_timestamp(); print "\n\n $tstamp \n\n"; # 1900-01-01 00:00:00.000 sub get_timestamp { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $year=$year+1900; return $year . sprintf('%02u%02u %02u%02u%02u', $mon, $mday, $hour, $m +in, $sec); }

Thanks for the help!

Replies are listed 'Best First'.
Re: Timestamp Format Help!
by Corion (Patriarch) on Mar 05, 2010 at 19:23 UTC
    use POSIX qw(strftime); print strftime '%Y-%m-%d %H:%M:%S.000', localtime;

    Your error seems to be that you don't know what sprintf does, and not telling us what you get instead.

Re: Timestamp Format Help!
by almut (Canon) on Mar 05, 2010 at 20:15 UTC

    Using POSIX::strftime() is certainly the way to go... but here's what your get_timestamp() could have looked like — just in case you want to learn from what you did wrong.

    sub get_timestamp { my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time); return sprintf('%u-%02u-%02u %02u:%02u:%02u.000', $year+1900, $mon+1, $mday, $hour, $min, $sec); }
Re: Timestamp Format Help!
by jffry (Hermit) on Mar 05, 2010 at 20:24 UTC
Re: Timestamp Format Help!
by Corion (Patriarch) on Mar 05, 2010 at 19:22 UTC

    Duplicate, sorry

    use POSIX qw(strftime); print strftime '%Y-%m-%d %H:%M:%S.000', localtime;