Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I convert a date (MM/DD/YYYY) to UTC time?

1 view
Skip to first unread message

Bill Nelson

unread,
May 22, 2001, 5:27:14 PM5/22/01
to
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.

Any ideas?

thanks,

bill

william.c.nelson.vcf

Todd Smith

unread,
May 22, 2001, 5:35:11 PM5/22/01
to
Date::Manip

(probably)


John Joseph Trammell

unread,
May 22, 2001, 5:37:06 PM5/22/01
to
On Tue, 22 May 2001 21:27:14 GMT, Bill Nelson <william....@gte.net> wrote:
> 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.

Perl is chock-full of time manipulation utils:

localtime
gmtime
Time::Local

--
Salad: it's what's for dinner, for what's for dinner.

Bill Nelson

unread,
May 22, 2001, 5:41:22 PM5/22/01
to
No way to do it with standard perl functions? I would rather not try to
bring in another module at this time.

bill

Todd Smith wrote:

> Date::Manip
>
> (probably)

william.c.nelson.vcf

Bill Nelson

unread,
May 22, 2001, 5:51:01 PM5/22/01
to
Yes, but none that I can tell to take a date and convert it to UTC, only to go from
UTC.

thanks,

bill

william.c.nelson.vcf

Todd Smith

unread,
May 22, 2001, 5:53:03 PM5/22/01
to
gmtime EXPR
Converts a time as returned by the time function to a
9-element array with the time localized for the standard
Greenwich time zone. Typically used as follows:


is that it?


Bill Nelson

unread,
May 22, 2001, 6:15:51 PM5/22/01
to
No, that goes from UTC to human readable time. I.E. I can go from UTC,
"954991003", and make it "Tue Mar 27 14:01:42 2001" by running the UTC
through the following code:

$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

william.c.nelson.vcf

John Joseph Trammell

unread,
May 22, 2001, 6:21:02 PM5/22/01
to
On Tue, 22 May 2001 21:51:01 GMT, Bill Nelson wrote:
> Yes, but none that I can tell to take a date and convert it to
> UTC, only to go from UTC.

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_

Jürgen Exner

unread,
May 22, 2001, 6:47:45 PM5/22/01
to
"Bill Nelson" <william....@gte.net> wrote in message
news:3B0AD4D0...@gte.net...

> 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.

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


Bart Lateur

unread,
May 22, 2001, 7:16:57 PM5/22/01
to
Bill Nelson wrote:

>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.

Bill Nelson

unread,
May 22, 2001, 10:14:39 PM5/22/01
to
You are correct, I actually misstated the question. Let me try again.

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

william.c.nelson.vcf

Todd Smith

unread,
May 23, 2001, 4:08:43 AM5/23/01
to
> 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.
>

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).


Sgluarb

unread,
May 23, 2001, 7:44:35 AM5/23/01
to

"Bill Nelson" <william....@gte.net> a écrit dans le message news:
3B0AD4D0...@gte.net...

> 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.

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...


Jeff Pinyan

unread,
May 23, 2001, 1:23:47 PM5/23/01
to
[posted & mailed]

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" **


jimbo

unread,
May 24, 2001, 8:52:44 AM5/24/01
to
"Bill Nelson" <william....@gte.net> wrote in message
news:3B0AD81B...@gte.net...

> No way to do it with standard perl functions? I would rather not try
to
> bring in another module at this time.

Why?

Do you prefer to spend your time coding your project or reinventing the
wheel?

jimbo
;-)

Garry Williams

unread,
Jun 9, 2001, 8:36:24 PM6/9/01
to

Exhibit the code.

(I doubt it, since the question seems to want to start with something
other than an epoch-seconds value.)

--
Garry Williams

Colin Keith

unread,
Jun 10, 2001, 3:24:49 PM6/10/01
to
In article <slrn9i5g88...@zfw.zvolve.net>, <ga...@zvolve.com> wrote:
>(I doubt it, since the question seems to want to start with something
>other than an epoch-seconds value.)

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

Joe Smith

unread,
Jun 11, 2001, 10:14:24 PM6/11/01
to
In article <3B0ADA55...@gte.net>, Bill Nelson <is...@flanet.com> wrote:
>> Perl is chock-full of time manipulation utils:
>>
>> localtime
>> gmtime
>> Time::Local
>
>Yes, but none that I can tell to take a date and convert it to UTC,

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.

0 new messages