1

I am not familiar with timestamp format much.

I have a text for example

'Jul 19, 2013 12:00 pm' 

I want to store it to mysql. What format of this timestamp is in MySQL and how should I format it properly in perl before passing it to mysql.

Thanks.

user2851049
  • 13
  • 1
  • 3

2 Answers2

4

What is the format of timestamps in MySQL?

I highly recommend that you read the MySQL manual. You'll get your answers much faster than by posting a question on StackOverflow. From the docs:

TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

As you can see, the format is YYYY-MM-DD HH:MM:SS.

As ysth points out in the comments, MySQL also has a DATETIME data type:

MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

Several things to note:

  • DATETIME and TIMESTAMP both use the format YYYY-MM-DD HH:MM:SS
  • The range of dates supported by DATETIME is much larger than the range for TIMESTAMP
  • TIMESTAMP converts values to UTC for storage and back to the local time zone on retrieval; DATETIME does no time zone conversion

If you aren't already wedded to the TIMESTAMP data type you might consider using DATETIME instead, depending on what kind of data you're trying to store. See this StackOverflow question for more details on DATETIME vs. TIMESTAMP.

How should I format it in Perl before passing it to MySQL?

To convert date/time strings to different formats in Perl, you can use the core (since v5.9) module Time::Piece:

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Time::Piece;

my $date = 'Jul 19, 2013 12:00 pm';
my $t = Time::Piece->strptime($date, '%b %d, %Y %I:%M %p');
say $t->strftime('%F %T');
# 2013-07-19 12:00:00

It's not clear from your example date string whether the day and hour are zero-padded. The above example assumes a format like

Oct 01, 2013 05:00 am

where days and hours less than ten begin with a zero. If your input format is actually

Oct 1, 2013 5:00 am

then you need to change the format string passed to Time::Piece->strptime. A list of format specifiers can be found in the man page for strftime.

Community
  • 1
  • 1
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
  • mysql also has a datetime type; often that is a better choice. – ysth Oct 06 '13 at 15:02
  • @ysth The OP asked about "timestamp" so I just assumed that they already have a column using the `TIMESTAMP` data type. Probably shouldn't have jumped to conclusions; I'll edit my answer. – ThisSuitIsBlackNot Oct 06 '13 at 15:07
1

Time::Piece has been a Perl core module since 5.9. It provides a convenient way to parse any time format input (with strptime()) and produce a differently formatted output (with strftime().

#cat ./tconv
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $t = Time::Piece->strptime( shift, "%b %d, %Y %I:%M %p" );
print $t->epoch, "\n";
print $t->strftime( $t, "%Y/%m/%d %T %z" ), "\n";

#./tconv "Jul 19, 2013 12:00 pm"
1374235200
Fri Jul 19 12:00:00 2013
JRFerguson
  • 7,426
  • 2
  • 32
  • 36
  • And there's always `strftime` from the [`POSIX`](http://perldoc.perl.org/5.8.8/POSIX.html) module. – fenway Oct 06 '13 at 21:10