Change Date Format with Perl - mysql

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

Related

Handling dates with different precision and a wide time span in Ruby on Rails

I would like to know how to handle and store dates like birthdates in Ruby on Rails when the exact day or month might be unknown and some birthdates are BC.
MySQL seems to support dates like "1990-00-00", which could be used to store the year without month and day, but BC dates don't seem to be possible. Since MySQL supports dates until the year 9999 and I need only birthdates up to today, the higher dates could be used to represent BC dates (9999 = -1, 9998 = -2, ...), but this seems complicated and messy.
Switch to Postgresql? LOL, just kidding. There are some solutions out there that suggest adding a constant to ALL dates in the background then subtracting it before display. Seems messy. Rails doesn't care how long ago your date is though. So you could store those dates as strings like '2023-01-29' or '-5000-01-29' and then parse them in rails:
Date.parse('29 Jan 2023')
=> Sun, 29 Jan 2023
Date.parse('-5000-01-29')
=> Fri, 29 Jan -5000
Date.parse('29 Sep 1000 BC')
=> Sun, 29 Sep -0999
As you can see Rails handles dates quite well and can parse a number of different string formats into dates. You could override the setter/getter for the column in the model:
class Foo < ActiveRecord::Base
# "Uses a string to hold the date for the column 'bar'"
def bar=(date)
super(date.to_s)
end
def bar
Date.parse(super)
end
end
Now when you have an instance of Foo and call .bar it will get converted from the string in the DB to a date, and when given a date in the setter method it will convert it to a string for you.

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.

timestamp format in mysql and perl

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

MySQL - How to Cast a datetime from an rss feed

I'm importing an rss feed into MySQL 5.1 via wget and LOAD DATA INFILE.
This is all working well, but, I'm having problems converting the date & time in the rss feed to a datetime col in mysql.
An example date from the feed is:
Sat, 19 Jan 2013 11:10:19 GMT
Any ideas how I can cast or convert this?
Thanks
J.
How about this? using str_to_Date:
STR_TO_DATE('rss_date', '%y-%m-%d')
use STR_TO_DATE
SELECT STR_TO_DATE(DATE_STRING,'%a, %d %b %Y %h:%i:%s')
SQLFiddle Demo
OTHER SOURCE
STR_TO_DATE
Date Formats