473,699 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RE: how do you convert to epoch time

31 New Member
can anybody tell me how to conver to this date (07-08-17 13:00:00) into epoch time?
Aug 23 '07 #1
10 11935
numberwhun
3,509 Recognized Expert Moderator Specialist
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
gsaray101
31 New Member
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 Recognized Expert Moderator Specialist
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
gsaray101
31 New Member
I am still getting 1177869600 after coverting year to 2007-1900.
Aug 23 '07 #5
numberwhun
3,509 Recognized Expert Moderator Specialist
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
gsaray101
31 New Member
http://www.esqsoft.com/javascript_exam ples/date-to-epoch.htm
Aug 23 '07 #7
numberwhun
3,509 Recognized Expert Moderator Specialist
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 Recognized Expert Specialist
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 Recognized Expert Moderator Specialist
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

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

Similar topics

8
18837
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( Substr(Char(CURRENT TIMESTAMP),12,2) ||
2
5855
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
8419
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 time to the query. I thanku all in advance.
2
5058
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 to get my local time. Is there a way to do that? Thanks
5
2903
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
2604
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 string format previously. Can anyone help this one too? Thanks! Astan
3
6282
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 whatever as long as it's consistent. So I picked the epoch time UTC getting the value I wanted, "timestamp", this way: #include "boost/date_time/local_time/local_time.hpp" using namespace boost::gregorian; using namespace boost::local_time;
11
3701
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 time() in Montreal AT THIS VERY INSTANT, will they return the same values, or will A's program return a value that is more than B's value by 36000 (since Melboune is 10 hours ahead of Montreal). Thanks, Anil
1
19961
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 taken on different days of the week at different time. E.g. Math02 on Wednesday at 8:30 AM to 11:30AM. So I want to show Time of Day, (eg 6:30 AM - 9:00 AM) Day (eg Monday) ad group the courses taken by day and time. Please assist Thanks O
0
8628
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9054
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5884
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.