timestamp format in mysql and perl - mysql

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.

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.

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

Related

How to insert date into MySQL DATETIME column from Angular using Node.js?

I have date picker in my application. I want to insert the selected date into a MySQL database column with DATETIME data type.
This is the value of the date picker in Angular using console.log(date.value) :
Tue Nov 12 2019 00:00:00 GMT+0200 (Israel Standard Time)
Which format does the date need to be converted to for MySQL database insertion?
To ensure consistency, it is helpful to store all dates in the UTC timezone.
Step 1: Convert JavaScript Date to ISO (UTC timezone)
const isoDateString: string = datePickerDate.toISOString();
This also enables sending the date via JSON to the server.
Step 2: Ensure that MySQL timezone is UTC
cursor.execute("SET time_zone = '+00:00'")
Step 3: Format date for MySQL insertion
On the Node.js server, parse the ISO date string (from step 1) and format as:
'YYYY-MM-DD HH:MM:SS'
const isoDate = new Date(isoDateString);
const mySQLDateString = isoDate.toJSON().slice(0, 19).replace('T', ' ');
MySQL Documentation
MySQL recognizes DATETIME and TIMESTAMP values in these formats:
As a string in either 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD HH:MM:SS'
format. A “relaxed” syntax is permitted here, too: Any punctuation
character may be used as the delimiter between date parts or time
parts. For example, '2012-12-31 11:30:45', '2012^12^31 11+30+45',
'2012/12/31 11*30*45', and '2012#12#31 11^30^45' are equivalent.
The only delimiter recognized between a date and time part and a
fractional seconds part is the decimal point.
The date and time parts can be separated by T rather than a space. For
example, '2012-12-31 11:30:45' '2012-12-31T11:30:45' are equivalent.
As a string with no delimiters in either 'YYYYMMDDHHMMSS' or
'YYMMDDHHMMSS' format, provided that the string makes sense as a date.
For example, '20070523091528' and '070523091528' are interpreted as
'2007-05-23 09:15:28', but '071122129015' is illegal (it has a
nonsensical minute part) and becomes '0000-00-00 00:00:00'.
As a number in either YYYYMMDDHHMMSS or YYMMDDHHMMSS format, provided
that the number makes sense as a date. For example, 19830905132800 and
830905132800 are interpreted as '1983-09-05 13:28:00'.
you can use this:{{yourDate | date: 'yyy-MM-dd HH:MM:SS'}} then send it to controller and then save into db

converting date and time to mysql datetime format

Currently, we are trying to convert: 'Wed, 13 Jun 2018 09:34:36' format to MySQL datetime format.
What would be a good approach for this? We have tried cast()
I ended up using this:
select STR_TO_DATE('13-Jun-2018 09:34:36', '%d-%M-%Y %h:%i:%s')
In python, I removed the first part and ending that was not needed.

Convert 2016-12-28 14:14:00 UTC date time format to 12/06/2016 11:28 PM format in rails application

I am querying MySQL database and getting date time in 2016-12-28 14:14:00 UTC format.
But I want my date time converted to the format like 12/06/2016 11:28 PM in rails.
You can use strftime to get the format as below:
d = '2016-12-28 14:14:00 UTC'.to_time
d.strftime('%m/%d/%Y %H:%M%p')
For Rails project I usually create special file called config/initializers/date_time_formats.rb
And add to their predefined a time formats.
Time::DATE_FORMATS[:human_with_12hours] = '%m/%d/%Y %I:%M %p'
Reload server and use it like this: YOUR_DATE_VARIABLE.to_s(:human_with_12hours)
You can create lot of time and date formats in this file.

Change Date Format with Perl

I have a text file which I'm converting to a csv file with information that I want to import into a MySQL database.
I currently have the date in this format:
Mar 24
I would like to change that format
YYYY-MM-DD
I would like to do this in perl as I have done all the other bit in the csv file in perl.
This is a tentative solution to your problem using Time::Piece, which is a core module and shouldn't need installing.
I'm very unhappy about your input date format not containing a year. The code below assumes the current year, but I can imagine that a date like Jan 3 should be upgraded to the following year if the code is run on 31 December.
The year has to be included in the string to be parsed because Time::Piece will alter Feb 29 to Mar 1 for non-leap years.
use strict;
use warnings;
use Time::Piece;
print bd_to_ymd('Mar 24');
sub bd_to_ymd {
my ($bd) = #_;
my $year = localtime->year;
my $tp = Time::Piece->strptime("$year $bd", '%Y %b %d');
$tp->strftime('%Y-%m-%d');
}
output
2014-03-24

my str_to_date in mysql is returning null

I ran this statement in mysql and it returns null on every row:
SELECT STR_TO_DATE('Fri, 22 Jun 2012 03:35:24 +0000', '%a, %e %b %Y %H:%i:%s %%%%%%%%%%') FROm t2;
I don't know the right specifier for the +0000. What should it be?
The +0000 is a time zone offset.
mySQL doesn't understand the concept of time zones, and apparently neither does STR_TO_DATE: the list of date/time format placeholders doesn't mention time zones at all.
If you expect dates from varying time zones, this doesn't seem solvable with the help of mySQL only. You would have to preprocess the date elsewhere to turn it into one that is always UTC (or your local time zone, whatever applies), or if it already is always UTC, ManseUK's suggestion (adding +0000 to your format string so mySQL ignores it) should work.