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

I spent a lot of time researching the pieces to make the following work and was wondering if it might be useful to anyone else. Your comments are appreciated. Thanks!

Problem: I log customer sessions and orders placed through a store. The store program gave me, in one case, the date in both epoch seconds and in string format. In the other case, just as a string. I wanted to get the dates into the mySQL database as DATE datatypes to make future use of the data more smooth (had just been using the strings). Unfortunately, it didn't appear that mySQL handles epoch seconds. Plus it is picky as to how dates may be enterred. So I had to do all the converting up front, in Perl.

Solution: I found bits and pieces of the information I needed at PerlMonks, on Google, on CPAN, etc. I wonder if it could save anyone else some time in the future?

Create the tables with datatypes:

#create Session table. $sql_line = "create table Session (...date DATETIME NOT NULL, ... )"; #create Order table. $sql_line = "create table Orders (...order_date DATE NOT NULL, ...)";

Convert date formats:

### This is useful for converting epoch seconds use Time::localtime; ### This is useful for converting strings to useable time formats ### (installation did not work as advertised, however!) use Date::Manip; sub SA2_getDate { my ($line); my ($fieldvalue); my ($fieldname); $line = $_; ($fieldname,$fieldvalue) = split(/:\s\s*/, $line); chomp ($fieldvalue); ###string received in format like: 991751732 OR Tue Jun 05 2001 ($fieldvalue) = split(/\sOR\s/, $fieldvalue); $fieldvalue =~ s/(^\d*)\s*/$1/; $fieldvalue = localtime($fieldvalue); #convert from epoch seconds. my $formatteddate = sprintf ("%04d%02d%02d %02d:%02d:%02d ", $fiel +dvalue->year+1900, $fieldvalue->mon+1, $fieldvalue->mday, $fieldvalue +->hour, $fieldvalue->min, $fieldvalue->sec); #make into format requir +ed by mySQL if ($session{$currentsession}[0] eq '') { $session{$currentsession}[0] = $formatteddate; } } sub SA8_getOrderDate { my ($line); my ($fieldvalue); my ($fieldname); $line = $_; ###string received in format like: Tue Jun 5 14:59:46 2001 ($fieldname,$fieldvalue) = split(/:\s\s*/, $line); chomp ($fieldvalue); my $formatteddate = ParseDate ($fieldvalue); #Courtesy Date::Manip $formatteddate =~ s/\d\d:\d\d:\d\d$//;#don't need the seconds if ($session{$currentsession}[6] eq '') { $session{$currentsession}[6] = $formatteddate; } }

Write the SQL:

###stuff from the hash can now be inserted, as is, into mySQL print SQL "insert into Orders (..., order_date,...)"; print SQL "values (...,'$order_date', ...);\n";

Replies are listed 'Best First'.
Re: Feedback - Dates From Data to Database
by Masem (Monsignor) on Jul 12, 2001 at 21:04 UTC
    The mysql functions UNIX_TIMESTAMP and FROM_UNIXTIME allow one to convert from the Mysql Date and Time formats to epoch seconds, and back again.

    Update as ar0n points out below (that's what you get for going OTTOMH). FROM_UNIXTIME is still correct, however.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      UNIX_TIMESTAMP, not UNIXTIME.

      ar0n ]