469,280 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

RE: how do you convert to epoch time

can anybody tell me how to conver to this date (07-08-17 13:00:00) into epoch time?
Aug 23 '07 #1
10 11629
numberwhun
3,503 Expert Mod 2GB
You would have to use the Time::Local module from CPAN.

I am not trying to be mean, but you need to try and do your legwork. As easy as it is to post to the forum and expect a quick answer, you can just as easily search for this stuff on CPAN or even on perldoc. Those are two main resources for information. Also, you may want to pick up a copy of "Learning Perl, 4th Edition" from O'Reilly. It will give you a really good, solid grounding in Perl and answer a bunch of your questions.

One more resource, try plugging your question into google. If you search for "convert datetime to epoch perl" and hit enter, you may get lucky and find yet another module to do it as well. As Perl's motto goes, There Is More Than One Way To Do It (TIMTOWTDI).

All in all, make sure you have exhausted your research options before posting. This will additionally help you to learn how to use the resources to find what you want.

Regards,

Jeff
Aug 23 '07 #2
I always search before I post. I am trying to conver to epoch time but I am not sure I am getting the correct results. This is my script:

Expand|Select|Wrap|Line Numbers
  1. use Time::Local;
  2. use strict;
  3. use warnings;
  4.  
  5. my %month;
  6. @month{ qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/ } = 0..11;
  7.  
  8. #my $date = "Thu Mar  9 23:04:03 2006";
  9. my $date= "07-03-29 14:00:00";
  10. my ($year, $month, $day, $hour, $min, $sec) = split /\W+/, $date;
  11.  
  12. my $time = timelocal($sec,$min,$hour,$day,$month,$year);
  13.  
  14. print $time,$/;
  15. print "$year-$month-$day $hour:$min:$sec";
  16. #print scalar localtime $time;
  17.  
my print statement showing 1177869600 rather than 1175176800. I dont know what I am doing wrong.
Aug 23 '07 #3
numberwhun
3,503 Expert Mod 2GB
The reason your conversion isn't working is because the $year is not set correctly. It should be set to the 4 digit year minus 1900. So, this year is 2007, so your $year should be set to 107. If you do that, it should work. A $year of 07 is 1907 and would be correct, if that were the year.

You can re-write your code to look like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. use strict;
  3. use warnings;
  4. use Time::Local;
  5.  
  6. my $date= "2007-03-29 14:00:00";
  7. my ($year, $month, $day, $hour, $min, $sec) = split /\W+/, $date;
  8.  
  9. $year = $year - 1900;  # This will give you the correct value of 107.
  10.  
  11. my $time = timelocal($sec,$min,$hour,$day,$month,$year);
  12.  
  13. print("$time /n");
  14.  
  15.  

Also, please be sure to place your code between code tags, otherwise our kind, wonderful moderators must clean up behind you and make everything nice and tidy.

++ for having "use strict" and "use warnings".

Regards,

Jeff
Aug 23 '07 #4
I am still getting 1177869600 after coverting year to 2007-1900.
Aug 23 '07 #5
numberwhun
3,503 Expert Mod 2GB
For the date that you provided, that is the correct number of seconds. Where are you getting the value of 1175176800 from? That dat is actually 31.166666666... days older than the date you are using in your code.

Regards,

Jeff
Aug 23 '07 #6
http://www.esqsoft.com/javascript_examples/date-to-epoch.htm
Aug 23 '07 #7
numberwhun
3,503 Expert Mod 2GB
The only explanation that I have is that one of these is off (BIG TIME!!). Considering that this module has been out on CPAN for some time and has been proven to be correct, I am going to have to lean towards the javascript tool's math being off.

When I convert the date: 2007-08-23 14:00:00 which is today's date at 14:00, i get the following using my script:

1190570400

When I set the javascript page to the same date/time, and do their conversion to epoch, I get the following:

1187892000

That is a HUGE error. I have used the Time::Local module and not had any problems and once even verified the number of seconds by hand. To me, the javascript tools author should be notified that it may not (rather is not) putting out the correct values. They need to know.

Regards,

Jeff
Aug 23 '07 #8
KevinADC
4,059 Expert 2GB
the month calculation is off. You need to subtract 1 (one) from the month as noted in the Time::Local module The month 03 (March) is month 02, the same as localtime, which calculates the months as 0-11, not 1-12. See line 12 below:

Expand|Select|Wrap|Line Numbers
  1. use Time::Local;
  2. use strict;
  3. use warnings;
  4.  
  5. my %month;
  6. @month{ qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/ } = 0..11;
  7.  
  8. #my $date = "Thu Mar 9 23:04:03 2006";
  9. my $date= "07-03-29 14:00:00";
  10. my ($year, $month, $day, $hour, $min, $sec) = split /\W+/, $date;
  11.  
  12. my $time = timelocal($sec,$min,$hour,$day,$month-1,$year);
  13.  
  14. print $time,$/;
  15. print "$year-$month-$day $hour:$min:$sec\n";
  16. print scalar localtime $time;
Aug 23 '07 #9
numberwhun
3,503 Expert Mod 2GB
Darn!!! I should have caught that. Coding 3 things at once plus posting here. Oh well. That would explain the difference. He he he.

Sorry about the confusion!

Regards,

Jeff
Aug 23 '07 #10
KevinADC
4,059 Expert 2GB
Note: you asked how to convert "07-08-29 14:00:00" but in the code you posted you have:

my $date= "07-03-29 14:00:00";

that is a 5 month difference between what you asked and what you posted, not sure if that makes a difference in what you thought the output should be.
Aug 23 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Chris | last post: by
2 posts views Thread by Sunil | last post: by
1 post views Thread by XML newbie: Urgent pls help! | last post: by
2 posts views Thread by Salim Afsar | last post: by
1 post views Thread by Astan Chee | last post: by
11 posts views Thread by usenet | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.