Any ideas?
thanks,
bill
(probably)
Perl is chock-full of time manipulation utils:
localtime
gmtime
Time::Local
--
Salad: it's what's for dinner, for what's for dinner.
is that it?
$utcTime = 954991003;
$humanReadable = gmtime($utcTime);
That produces a value of "Tue Mar 27 14:01:42 2001" for the $humanReadable
variable.
I want to go the other way around. I want to take something like
11/10/2000 and turn it into a UTC time so that I can do an easy comparrison
of two timestamps in UTC.
thanks,
bill
Todd Smith wrote:
> man perlfunc / gmtime
I think you're confusing UTC and Epoch.
--
According to the Genesis account, the tower of Babel was man's second
major engineering undertaking, after Noah's ark. Babel was the first
engineering fiasco.
- F. Brooks, _The Mythical Man-Month_
I think you are somewhat confused. Your question doesn't make much sense.
- A date is something like e.g. 2001/05/22 (in whatever standard).
- A time is something like e.g. 18:34 o'clock, (and you are asking for the
timezone being in UTC).
They have pretty much nothing to do with each other and you simply cannot
convert a data into a time (be it UTC or any other time zone).
Maybe your date contains a time segment and you are trying to convert this
segment to UTC? Then you may want to look up locales and timezones.
Or are you asking for something totally different?
jue
>I would like to do some date comparrisons via UTC time and am trying to
>determine how to converte a date format of something like: 12/01/2000
>to UTC without having to do all the math and figure out leap years and
>such.
The standard module (as in: present on every perl installation)
Time::Local can turn combinations of year, month and day back into times
since the epoch.
Don't forget to subtract 1 from your month number (January = 0).
BTW is that December 1st of January 12th?
use Time::Local;
my $time = timegm(0, 0, 0, 1, 12-1, 2001);
print scalar gmtime($time)
-->
Sat Dec 1 00:00:00 2001
--
Bart.
I have an application that provides logfiles with entries containing
timestamps in the number of seconds since the Epoch (00:00:00 UTC, January 1,
1970). In trying to create a human readable log file, I can convert each
timestamp to a human readable time/date format by using the gmtime()
function. For instance,
$secondsSinceEpoch = 954991003;
$humanReadableTimeStamp = gmtime($secondsSinceEpoch);
That produces a value of "Tue Mar 27 14:01:42 2001" for the
$humanReadableTimestamp scalar variable.
Now, I want to be able to allow users to enter in a starting date and ending
date so I can only show entries in this log that fall between the starting
date and ending date. I can control the way that they enter in the date
format so that I can extract the month, day, and year from both dates.
I am assuming that the easiest way would be to simply take the dates that they
enter and convert them into the number of seconds since the Epoch. I could
then compare easily by doing something like:
if ( ($secondsSinceEpoch >= $startingDate) && ($secondsSinceEpoch <=
$endingDate) ) {
# print the log entry
}
I simply need to see if there is a way to take a date (ie 12/31/2001) and
convert it into the number of seconds since the Epoch.
Hopefully this is less confusing.
THANKS AGAIN!
bill
Found in /usr/local/lib/perl5/5.6.0/pod/perlfaq4.pod
How can I take a string and turn it into epoch seconds?
If it's a regular enough string that it always has the
same format, you can split it up and pass the parts to
`timelocal' in the standard Time::Local module.
Otherwise, you should look into the Date::Calc and
Date::Manip modules from CPAN.
(and Date::Manip will probably be able to go backwards too).
use Time::Local; # timelocal($sec, $min, $hours, $mday, $mon, $year);
if (/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2}).txt/o) {
$filedate = timelocal($6, $5, $4 ,$3 ,$2 - 1, $1 - 1900);
etc...
On May 23, Sgluarb said:
>if (/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2}).txt/o) {
The /o modifier there does nothing for the regex -- /o only has a purpose
when there are variables being interpolated inside your regex.
http://www.pobox.com/~japhy/docs/LPRE.html#regex%20compilation
--
Jeff "japhy" Pinyan ja...@pobox.com http://www.pobox.com/~japhy/
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** I need a publisher for my book "Learning Perl's Regular Expressions" **
Why?
Do you prefer to spend your time coding your project or reinventing the
wheel?
jimbo
;-)
Exhibit the code.
(I doubt it, since the question seems to want to start with something
other than an epoch-seconds value.)
--
Garry Williams
Likewise, and if taht's the case try this extremely handy module Time::Local.
Pat of the std Perl distribution. Since you only have MM, DD, YYYY, just fill
in the time part as 0's, so you'll get the epoch time at midnight on that
date, you can then convert it onwards if you want:
perl -w -Mstrict -MTime::Local;
$_ = '04/05/1980';
m#^(\d{2})/(\d{2})/(\d{4})$# &&
print "Epoch time: ", ($_ = timelocal(0, 0, 0, $2, $1-1, $3-1900)),
"\nLocaltime: ". localtime($_), "\n";
Gives:
Epoch time: 323737200
Localtime: Sat Apr 5 00:00:00 1980
(Hands up who knows what they were doing on that date, I don't :-)
Col.
---
Colin Keith
Systems Administrator
Network Operations Team
ClaraNET (UK) Ltd. NOC
You are mistaken. Time::Local has two functions that convert from
(seconds,minutes,hours,day,month,year) to epoch time, which is what
you were looking for.
[Splitting a date string into (sec,min,...) is left as an exercise for
the reader. Or CPAN.]
-Joe
--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.