CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
DateTime conversion for SQL, wrong year??

 



PapaGeek
User

May 11, 2014, 3:49 AM

Post #1 of 3 (8170 views)
DateTime conversion for SQL, wrong year?? Can't Post

 
DateTime conversion for SQL

Iím having a problem converting a DateTime to an ASCII string for use in a SQL statement for MS ACCESS. For some reason the DateTime conversion of the year is wrong.

Iím using DateTime because of its math functions like:
$dt1y = $dt->clone->subtract(years => 1);
Here is an example of the problem Iím having:

Code
use Modern::Perl '2013'; 

use DateTime;

main();

sub main
{
UpdateMarkets('2012-12-31');
}

sub UpdateMarkets
{
my ($newDate) = @_;
say "Updating Markets for $newDate";
my ($year, $month, $day) = split/-/, $newDate;
my $dt = DateTime->new (year => $year,
month => $month,
day => $day,
time_zone => 'local' );

say "dt is now $dt";
my $sqlDate = $dt->strftime('%d-%b-%g');
say "date $newDate converted to SQL date $sqlDate";
say "calling the update process with $sqlDate";
}

The output is

Code
Updating Markets for 2012-12-31 
dt is now 2012-12-31T00:00:00
date 2012-12-31 converted to SQL date 31-Dec-13
calling the update process with 31-Dec-13

As you can see, I call the code with the last day of 2012 and get back the last day of 2013.
Any suggestions?


PapaGeek
User

May 11, 2014, 5:20 AM

Post #2 of 3 (8139 views)
Re: [PapaGeek] DateTime conversion for SQL, wrong year?? [In reply to] Can't Post

I found the answer, but not exactly sure why.


%g is Two digit representation of the year going by ISO-8601:1988 standards

%y is Two digit representation of the year


Not exactly sure what that ISO standard is, but I should have followed my favorite quote by Andrew Tanenbaum ďThe nice thing about standards is that you have so many to choose fromĒ !


BillKSmith
Veteran

May 11, 2014, 6:55 AM

Post #3 of 3 (8104 views)
Re: [PapaGeek] DateTime conversion for SQL, wrong year?? [In reply to] Can't Post

I suspect that their is a difference only on the first/last day of the year and that it has to do with time zone.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives