Re: How do I find difference between two timestamps?
by davorg (Chancellor) on Nov 16, 2004 at 17:19 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use DateTime::Format::Strptime;
my $fmt = '%Y-%m-%dT%H:%M:%S.%3NZ';
my ($start, $end) = qw(2004-11-15T18:59:52.863Z
2004-11-15T19:09:52.972Z);
my $parser = DateTime::Format::Strptime->new(pattern => $fmt);
my $dt1 = $parser->parse_datetime($start) or die;
my $dt2 = $parser->parse_datetime($end) or die;
my $diff = $dt2 - $dt1;
print $diff->hours, " hours\n";
print $diff->minutes, " minutes\n";
print $diff->seconds, " seconds\n";
print $diff->nanoseconds / 1e6, " milliseconds\n";
--
< http://www.dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] [Watch: Dir/Any] [d/l] |
Re: How do I find difference between two timestamps?
by ikegami (Patriarch) on Nov 16, 2004 at 17:20 UTC
|
use Time::Local;
$start = '2004-11-15T18:59:52.863Z';
$end = '2004-11-15T19:09:52.972Z';
@parts = $start =~ /^(.{4})-(.{2})-(.{2})T(.{2}):(.{2}):(.{2})\.(.{3})
+Z$/;
$start_secs = pop(@parts) / 1000;
$start_secs += timegm reverse @parts;
@parts = $end =~ /^(.{4})-(.{2})-(.{2})T(.{2}):(.{2}):(.{2})\.(.{3})Z$
+/;
$end_secs = pop(@parts) / 1000;
$end_secs += timegm reverse @parts;
$diff = $end_secs - $start_secs;
printf("%.3f$/", $diff); # 600.109
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
Re: How do I find difference between two timestamps?
by tomhukins (Curate) on Nov 16, 2004 at 17:18 UTC
|
| [reply] [Watch: Dir/Any] |
Re: How do I find difference between two timestamps?
by tall_man (Parson) on Nov 16, 2004 at 17:13 UTC
|
The timegm function in Time::Local can convert a Zulu (GMT) time into a timestamp. Since your date formats are nonstandard, it would be simplest to break out the year, month, day, hours, minutes, and seconds with a regular expression or substrings, then do:
$time = timegm($sec,$min,$hour,$mday,$mon,$year);
These times can be subtracted to give differences in seconds. You'll have to subtract the milliseconds separately and add that to the difference. | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
That may be so according to the most recent 8601 standard, but as far as Perl is concerned it is not. For example, Date::Manip claims to be able to handle ISO 8601 date formats, but the following will fail:
use strict;
use Date::Manip;
my $string = "2004-11-15T18:59:52.863Z";
my $date = ParseDate($string);
print $date,"\n";
| [reply] [Watch: Dir/Any] [d/l] |
|
Re: How do I find difference between two timestamps?
by castaway (Parson) on Nov 17, 2004 at 08:49 UTC
|
You could also skip all this 'figuring out what format that is, and passing it to something', and use a module that parses exactly what you have there, to wit: Time::ParseDate.
perl -MTime::ParseDate -le'print scalar localtime(parsedate("2004-11-1
+5T18:59:52.863Z"))'
Gives me:
Mon Nov 15 18:59:52 2004
Which looks good to me. So just run parsedate() on both, subtract the results, then all you'll need to do is convert the resulting number of seconds to something you understand ,)
C.
| [reply] [Watch: Dir/Any] [d/l] |
Re: How do I find difference between two timestamps?
by Diakoneo (Beadle) on Nov 16, 2004 at 18:42 UTC
|
I'm new to programming, but can you convert them to epoch time, subtract them, then convert them back? Or is that inefficient? | [reply] [Watch: Dir/Any] |
|
I'm not completly sure, but I think the epoch counts seconds, so the OP's timestamps would lose their information on milliseconds during the conversion.
| [reply] [Watch: Dir/Any] |
|
That's what all the previously mentioned modules do. It's not a question of efficiency, it's a question of necessity.
| [reply] [Watch: Dir/Any] |