Discussion:
How to reverse the time localtime/gmtime function's process?
(too old to reply)
L. D. James
2005-04-13 19:18:27 UTC
Permalink
Hi Gang. I have two times in the format of:



20050413185947

20050413175146



It is broken down as, year, month, day, hour, minute and second.



2005-0413-1751-46



I would like to subtract the smaller number from the larger number. I
guess if I had an easy way to set both numbers in to seconds, that then
subtract, then convert them back to year, month, day, etc. such would work.



I'm familiar with the localtime(time) function. Is there a way to
enter the split time into a function that will give you the big number of
the time function? If I could get both numbers into a single number, then
subtract the numbers, this would be ideal.



Thanks in advance for any comments on this matter.



-- L. James
--
--------------------------------
L. D. James
***@apollo3.com
www.apollo3.com/~ljames
Fabian Pilkowski
2005-04-13 20:53:37 UTC
Permalink
Post by L. D. James
20050413185947
20050413175146
It is broken down as, year, month, day, hour, minute and second.
2005-0413-1751-46
I would like to subtract the smaller number from the larger number. I
guess if I had an easy way to set both numbers in to seconds, that then
subtract, then convert them back to year, month, day, etc. such would work.
I'm familiar with the localtime(time) function. Is there a way to
enter the split time into a function that will give you the big number of
the time function? If I could get both numbers into a single number, then
subtract the numbers, this would be ideal.
Have a look at Time::Local. The method timelocal() should be the reverse
of localtime(), and -- not surprising that -- timegm() is reversing what
gmtime() does.


#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;

sub get_time {
my @date = reverse unpack 'A4(A2)*', shift;
$date[4]--; # adjust month
timelocal( @date );
}

my $time = get_time( '20050413185947' );
print $time, "\n", scalar localtime $time;
__END__
1113411587
Wed Apr 13 18:59:47 2005


Perhaps someone would check if $date is formatted correctly to do a
reasonable error handling.

regards,
fabian
L. D. James
2005-04-14 02:02:58 UTC
Permalink
Post by Fabian Pilkowski
Have a look at Time::Local. The method timelocal() should be the reverse
of localtime(), and -- not surprising that -- timegm() is reversing what
gmtime() does.
Thanks, Fabian. Interesting nomenclature for the method. It works
like a charm.

I also appreciate all the other useful comments and contributions. I
had looked at the perldoc -q date, but couldn't fully follow the flow. I'll
revisit it.

-- L. James
--
--------------------------------
L. D. James
***@apollo3.com
www.apollo3.com/~ljames
A. Sinan Unur
2005-04-13 20:54:35 UTC
Permalink
Post by L. D. James
20050413185947
20050413175146
...
Post by L. D. James
I'm familiar with the localtime(time) function. Is there a way to
enter the split time into a function that will give you the big number
of the time function? If I could get both numbers into a single
number, then subtract the numbers, this would be ideal.
How about searching CPAN:

http://search.cpan.org/~drolsky/Time-Local-1.11/
Post by L. D. James
-- L.
James
Your signature is not formatted properly.

Sinan
--
A. Sinan Unur <***@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
Tad McClellan
2005-04-13 23:08:09 UTC
Permalink
Post by A. Sinan Unur
-- L.
James
Your signature is not formatted properly.
s/is/is still/;
--
Tad McClellan SGML consulting
***@augustmail.com Perl programming
Fort Worth, Texas
a***@white-eagle.invalid.uk
2005-04-14 03:31:05 UTC
Permalink
Post by A. Sinan Unur
Your signature is not formatted properly.
Isn't it? How? It seemed ok to me.

Axel
A. Sinan Unur
2005-04-14 03:55:57 UTC
Permalink
Post by a***@white-eagle.invalid.uk
Post by A. Sinan Unur
Your signature is not formatted properly.
Isn't it? How? It seemed ok to me.
Well, I was confused by the fact that his (properly formatted) sig block
Post by a***@white-eagle.invalid.uk
Post by A. Sinan Unur
-- L.
James
I mistook this for an improperly formatted sig block given the two dashes.

Sinan
--
A. Sinan Unur <***@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
Mothra
2005-04-13 20:56:58 UTC
Permalink
Post by L. D. James
I would like to subtract the smaller number from the larger number.
I
Post by L. D. James
guess if I had an easy way to set both numbers in to seconds, that then
subtract, then convert them back to year, month, day, etc. such would work.
Something like this should get you started

use strict;
use warnings;
use DateTime;
use DateTime::Format::Strptime;

my $Strp = new DateTime::Format::Strptime(
pattern => '%Y%m%d%H%M%S',
time_zone => 'floating',
);
my @data;
while (<DATA>) {
push(@data, $Strp->parse_datetime($_) );

}

print map { $_->epoch(), "\n" } @data;

__DATA__
20050413185947
20050413175146
Tad McClellan
2005-04-13 23:03:58 UTC
Permalink
Post by L. D. James
20050413185947
20050413175146
I would like to subtract the smaller number from the larger number.
Thanks in advance for any comments on this matter.
Your Question is Asked Frequently.

perldoc -q date

How can I compare two dates and find the difference?
--
Tad McClellan SGML consulting
***@augustmail.com Perl programming
Fort Worth, Texas
Loading...