Re: converting dates to epoch seconds and back
Bill Stephenson wrote: When converting DMYHMS to Epoch Seconds and back I get cheated out of a day. Why? Bill -- #!/usr/bin/perl use strict; use warnings; use Time::Local; my ($time, $month, $day, $year, $seconds, $minutes, $hours, $wday, $yday, $isdst); my $start_date = '11/30/2012'; print "$start_date \n"; ($month, $day, $year) = split(/\//, $start_date); print $time = timegm($seconds, $minutes, $hours, $day, $month-1, $year-1900); ^^ ** print "\n"; ($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday, $isdst) = localtime($time); ^ * $month++; $year = ($year+1900); print "$month/$day/$year \n"; # output: # 11/30/2012 # 1354233600 # 11/29/2012 You are using GMT for one conversion and local time for the other. Best to use GMT for both conversions: $ perl -le' use strict; use warnings; use Time::Local; my ( $time, $month, $day, $year, $seconds, $minutes, $hours ); my $start_date = q[11/30/2012]; print $start_date; ( $month, $day, $year ) = split /\//, $start_date; print $time = timegm( $seconds, $minutes, $hours, $day, $month - 1, $year - 1900 ); ( $seconds, $minutes, $hours, $day, $month, $year ) = gmtime $time; $month++; $year += 1900; print "$month/$day/$year"; ' 11/30/2012 1354233600 11/30/2012 John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: converting dates to epoch seconds and back
The output depends on the timezone that you have set on your machine. You can toy around and try different ones with $ENV{TZ} = 'Europe/Vienna'; or $ENV{TZ} = 'America/Los_Angeles'; If you don't want to depend on the timezone use gmtime instead of localtime. Bill Stephenson hat am 18. Januar 2013 um 21:13 geschrieben: > When converting DMYHMS to Epoch Seconds and back I get cheated out of a day. > Why? > > Bill > > -- > > #!/usr/bin/perl > > use strict; > use warnings; > use Time::Local; > > my ($time, $month, $day, $year, $seconds, $minutes, $hours, $wday, $yday, > $isdst); > > my $start_date = '11/30/2012'; > > print "$start_date \n"; > > ($month, $day, $year) = split(/\//, $start_date); > > print $time = timegm($seconds, $minutes, $hours, $day, $month-1, $year-1900); > > print "\n"; > > ($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday, $isdst) = > localtime($time); > > $month++; > > $year = ($year+1900); > > print "$month/$day/$year \n"; > > # output: > # 11/30/2012 > # 1354233600 > # 11/29/2012 > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > --- Bekanntgabe: Bitte in Zukunft sämtliche E-mailkorrespondenz an diese E-Mailadresse: domi...@foop.at Dominik Danter
RE: converting dates to epoch seconds and back
Bill, it appears to have to do with it pick the time not being populated, and it picking up the date from yesterday. I added some lines to test this theory. Try running the code below without an argument, and then with an argument. Tim #!/usr/bin/perl use strict; use warnings; use Time::Local; my ($time, $month, $day, $year, $seconds, $minutes, $hours, $wday, $yday, $isdst); my $start_date = '11/30/2012'; print "$start_date \n"; ($month, $day, $year) = split(/\//, $start_date); #Added this to populate the time if (defined($ARGV[0])) { print "Populating the time\n"; $seconds = 30; $minutes = 30; $hours = 11; } print $time = timegm($seconds, $minutes, $hours, $day, $month-1, $year-1900); print "\n"; ($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday, $isdst) = localtime($time); $month++; $year = ($year+1900); print "$month/$day/$year \n"; -Original Message- From: "Bill Stephenson" Sent: Friday, January 18, 2013 3:13pm To: "Perl Beginners" Subject: converting dates to epoch seconds and back When converting DMYHMS to Epoch Seconds and back I get cheated out of a day. Why? Bill -- #!/usr/bin/perl use strict; use warnings; use Time::Local; my ($time, $month, $day, $year, $seconds, $minutes, $hours, $wday, $yday, $isdst); my $start_date = '11/30/2012'; print "$start_date \n"; ($month, $day, $year) = split(/\//, $start_date); print $time = timegm($seconds, $minutes, $hours, $day, $month-1, $year-1900); print "\n"; ($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday, $isdst) = localtime($time); $month++; $year = ($year+1900); print "$month/$day/$year \n"; # output: # 11/30/2012 # 1354233600 # 11/29/2012 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/