473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 11920
numberwhun
3,509 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,509 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,509 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,509 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,509 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

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

Similar topics

8
by: Chris | last post by:
Sorry, This should be simple, but brain is hurting... How do I convert a Current Time to a Decimal 6,0 (HMS)? There must be a cleaner way then this: Insert into Table Values Dec(...
2
by: Sunil | last post by:
Can someone suggest me how do i write a code in VB for epoch time. I want if the value is entered it changes into time and if time is enetred i get the value
1
by: XML newbie: Urgent pls help! | last post by:
How to Convert local time(eg EST) on local machine to GMT. I am using VB.Net 2005. Then I need to subtract 1 minute(or 1-100) minute selected by the user in the combobox and pass it as start...
2
by: Salim Afsar | last post by:
Hi, My web server is not my country, so server's time and my country time is different. So code runs on server, for example when I run DateTime.Now() function it returns server time but I want...
5
by: Summu82 | last post by:
HI I have to convert a time in the format i have year month day hour min and seconds I need to convert this into time in seconds since 1 jan 1970 i.e the
1
by: Astan Chee | last post by:
Hi, I have a string in this format "DD/MM/YYY" for example: tdate = "18/01/1990" and Im trying to convert this to epoch time, can anyone help? Im also trying to convert that epoch time to the...
3
by: Mark | last post by:
I want to simply get the unix style epoch time (number of secs to now from Jan 01, 1970 UTC) for use as a timestamp to track the staleness of some objects. So I don't care about time zones or...
11
by: usenet | last post by:
Is the epoch time the number of seconds elapsed since January 1st 1970 in my timezone or as per UTC? I mean, if A's program makes a call to time() in Melboune and B's program makes a call to...
1
by: okaymy1112 | last post by:
I have data in this format 0800, 1730, 0930, etc. I do not know how to convert this time to read something like, 8:00AM, 5:30 PM, 9:30AM etc. Also I want to group things by day. I have courses...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.