Re: Converting POSIX format date/times to epoch seconds
by Corion (Patriarch) on Feb 23, 2005 at 07:28 UTC
|
| [reply] |
|
That's exactly what I was after... Thanks!
| [reply] |
Re: Converting POSIX format date/times to epoch seconds
by graff (Chancellor) on Feb 23, 2005 at 06:45 UTC
|
Given that you know how to parse the users' preferred date format -- i.e. you can easily split it up into components (sec, min, hr, date, month year), another thing you can try is Time::Local -- it's part of the core Perl distro, so everybody has it.
It exports a function called "timelocal", which just converts those components to seconds since the epoch (i.e. it's the "opposite" of localtime). | [reply] |
Re: Converting POSIX format date/times to epoch seconds
by sgifford (Prior) on Feb 23, 2005 at 04:41 UTC
|
| [reply] |
|
I'm not sure how that helps me. Say I have a date - 02/01/2005. Is that the 2nd Jan or the 1st Feb? If I know the POSIX format then I know which assumption to make. As far as I can tell, Date::Parse makes assumptions on the Time Zone, but with an internet app, my users are distributed. It looks like DateTime does the trick, but alas, there is no Windows ppm, and I had trouible building it from source. (No the app is not on Windows, but development is...)
| [reply] |
|
...but alas, there is no Windows ppm, and I had trouible building it from source.
Date::Parse is a member of the TimeDate-distribution, as you can see here.
There are ppms for TimeDate available at the ActiveState-repositories.
You can take that as a general rule. If you cannot find a module vie ppm, search at CPAN to see if it is hidden in a distribution, then use ppm to search for that distribution.
| [reply] [d/l] [select] |
|
If the only problem is telling apart day-of-month from month, just parse it, format it with strftime, and see if it matches what you started off with. If it's wrong, swith the day-of-month and month and try again.
Kinda hackish, but seems like it would work. AFAIK, your only other option is to implement the opposite of strftime, which sounds like a lot of work.
| [reply] [d/l] [select] |
Re: Converting POSIX format date/times to epoch seconds
by zentara (Archbishop) on Feb 23, 2005 at 12:48 UTC
|
#!/usr/bin/perl
use Date::Manip;
my $secs = UnixDate('03-DEC-02', '%s');
print "$secs\n";
#see perldoc Date::Manip for details on
#formats for seconds, fractional seconds, and timezones
I'm not really a human, but I play one on earth.
flash japh
| [reply] [d/l] |
|
While there are other date modules, Date::Manip is one of, if not the most versatile.
One thing to be aware of though, is that it is very heavy to use in an environment where performance matters.
I use it when I can, and replace it with small, custom routines in places where it's just too slow.
| [reply] |
Re: Converting POSIX format date/times to epoch seconds
by Aristotle (Chancellor) on Feb 23, 2005 at 14:22 UTC
|
See HTTP::Date. Yes, the name and choice of distribution for this piece of code if horrible, but it has a refreshingly minimal-frills API if your needs are very simple (ie no date math, just conversions).
Makeshifts last the longest.
| [reply] |
|
| [reply] |
|
# WMIC (Windows command line) Date output format from `wmic os get
+ lastbootuptime`
(($yr, $mon, $day, $hr, $min, $sec) =
/^
(\d{4}) # year
(\d\d) # numerical month
(\d\d) # day
(\d\d) # Hour
(\d\d) # Min
(\d\d) # Sec
/x)
||
| [reply] [d/l] |
|