Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

getting a unix timestamp from a date

Posted on 1999-07-15
9
Medium Priority
?
394 Views
Last Modified: 2010-03-04
Hi there,

If I have a date formatted similarly to this:
 Nov 15 1998 11:30:11:496AM
 or
 Nov 15 1998 11:30AM
and I want to get a UNIX timestamp of approximatly that time (all I really need is a timestamp within that day), what's the easiest way to do it?  I have not been able to find a bulit in perl function.  Please let me know if you have any ideas

Yinon

0
Comment
Question by:yinon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 

Accepted Solution

by:
prophead earned 1600 total points
ID: 1214177
Use the DATE::Parse module from CPAN. Using the second format you specified it should work like this.

use Date::Parse;

$date = "Nov 15 1998 11:30AM";
$time = str2time($date);

Let us know if you need any help installing the module.
0
 

Author Comment

by:yinon
ID: 1214178
Didn't think it would be this difficult, but i'm having trouble with the install.  A pointer to the correct file to download from CPAN would be greatly appreciated.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1214179
perl -MCPAN -e shell;
cpan> install Date::Parse

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:yinon
ID: 1214181
Well, I got a little further, but i'm still not getting it to work.  As a simple test, I used:


use Date::Parse;
$date = "Nov 15 1998 11:30AM";
$datestmp =  str2time($date);
print $datestmp;

And got:
Substitution loop at (eval 1) line 27.

But no timestamp.  This is the same thing I get when I try to incorporate the suggestion into a bigger script I wrote (same line number).

Thanks,
Yinon


0
 

Expert Comment

by:prophead
ID: 1214182
Does it work if you put in a file of it's own? i.e. test.pl

#!/usr/bin/perl

use Date::Parse;
$date = "Nov 15 1998 11:30AM";
$datestmp =  str2time($date);
print $datestmp;
0
 

Author Comment

by:yinon
ID: 1214183
No, it doesn't.  That's exactly what i did when i tried it (even named the file test.pl -- hehe).

Yinon
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1214184
Did you follow the install procedure? You should have af file called Parse.pm in your Date directory and a file called Zone.pm in your Time directory, under your site-lib area, such as /usr/local/lib/per5/site/... for Unix.

I ran this code and got a value of 911093400. Not sure if this is the number you are looking for.
0
 

Author Comment

by:yinon
ID: 1214185
Sorry it took me so long to reply.  Apparently, this was an issue with the standard RH5.2 rpm of perl (perhaps some sort of corruption).  After testing it on my personal RH 6.0 box and on another 5.2 box, I narrowed now the problem and installed another version of perl.  It's now working great, and I can now worry about more important things with this script.  Thanks to all who helped me with this.  

For future reference, the formats I supplied are the ones that are generated by the DBD::Sybase module when fetching from a MSSQL database.  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question