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

Hi All,

I have a MySql database which contains a time field with the following values i.e 20011023083000 which translates to yyyymmddhhmmss(year,month,day,hour,minutes,seconds). So i want to know if there is a Perl time function that grabs the day and time in the same fashion that MySql does since that's what i know how to use. Or if i can just grab the time from other functions and put it together to look like the MySql time.

Thanks for your help in advance,
Kiko

Replies are listed 'Best First'.
Re: DATE TIME Manipulation
by blakem (Monsignor) on Oct 23, 2001 at 23:04 UTC
    How about this:
    #!/usr/bin/perl -wT use strict; use POSIX qw(strftime); my $now = get_now(); print "Now = $now\n"; sub get_now { return strftime '%Y%m%d%H%M%S', localtime(); } =OUTPUT Now = 20011023150416

    -Blake

Re: DATE TIME Manipulation
by tachyon (Chancellor) on Oct 23, 2001 at 23:32 UTC
    my $field = "20011023083000"; my $d = qr /(\d\d)/; my ($year,$month,$day,$hour,$minutes,$seconds) = $field =~ m/(\d{4})$d +$d$d$d$d/o; show(); ($year,$month,$day,$hour,$minutes,$seconds) = (localtime)[5,4,3,2,1,0] +; $year += 1900; $month += 1; # dec = 12 not 11 show(); $string = sprintf "%4d%02d%02d%02d%02d%02d", $year,$month,$day,$hour,$ +minutes,$seconds; print "\n\$string $string\n"; sub show { print " \$year $year \$month $month \$day $day \$hour $hour \$minutes $minutes \$seconds $seconds "; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: DATE TIME Manipulation
by Fletch (Bishop) on Oct 23, 2001 at 23:32 UTC

    Check the mysql docs for the mysql from_unixtime() function. It'll take a time_t/epoch seconds value like time() produces and turn it into something more to mysql's liking.

Re: DATE TIME Manipulation
by davis (Vicar) on Oct 24, 2001 at 15:30 UTC
    I'm fairly sure MySQL's flexible about the input date format. EG:
    mysql> insert into foo values("20011023083000"); Query OK, 1 row affected (0.01 sec) mysql> insert into foo values("2001-10-24 12:24:00"); Query OK, 1 row affected (0.00 sec) mysql> select * from foo; +----------------+ | dt | +----------------+ | 20011024122400 | | 20011023083000 | +----------------+ 2 rows in set (0.01 sec)
    See MySQL Docs for more info