-2

I have two strings which contain time stamps only and look like this - my $t = "2014-11-28 00:00:00.000"; How do I get the time difference between then in readable time (days, hours, minutes, seconds) and not seconds. I am new and I am facing great difficulty in doing this. Each blog or article is telling me to do different things. Some things can't be done because I don't have the modules. I cannot even install the modules into perl because I have many pearl versions on the computer I am working on. So, cpan > install some::modlue does not help.

Please teach me how to do this. Please try to use core perl. Otherwise I will have to spend more time in installing perl modules into the right perl

I made some code to begin with, but its wrong and useless -

use strict;
use warnings;
use Time::Local;

sub secondsToTime{

    my $inSeconds = shift;
    my $days = int($inSeconds/(24*60*60));
    my $hours = ($inSeconds/(60*60))%24;
    my $mins = ($inSeconds/60)%60;
    my $sec = $inSeconds%60;
    my $time = "$days days, $hours hours, $mins mins, $sec seconds";
    return $time;
}

# EXAMPLE timelocal( $sec, $min, $hour, $mday, $mon, $year );
my @today = localtime();
print "@today\n";
my $currentTime = timelocal(@today);

#my @t = (0, 00, 13, 28, 11, 2014);
#$currentTime = timelocal(@t);
my @birthday = (0, 00, 12, 28, 11, 2014); 
my $birthTime = timelocal(@birthday);
my $sec = ($currentTime - $birthTime);
my $time = secondsToTime($sec);

print "My age = $time\n";
smalls
  • 1
  • 3
  • what timezone are the timestamps in? – ysth Nov 28 '14 at 22:12
  • what perl versions do you need to support? – ysth Nov 28 '14 at 22:13
  • @ysth - Perl 5.20. Timestamps will be in MST or PST. Would be nice to have something that takes timestamps into account. Thanks. – smalls Nov 28 '14 at 22:15
  • @ysth - All these posts are scary. There are either big solutions, solutions that are called wrong by others or solutions that don't use core perl - http://stackoverflow.com/questions/4460610/how-can-i-get-the-difference-of-two-timestamps-using-perl, http://stackoverflow.com/questions/821423/how-can-i-calculate-the-number-of-days-between-two-dates-in-perl – smalls Nov 28 '14 at 22:20
  • Please give some examples of pairs of date/times, including those that specify time zones. A single example date is worthless, especially one that has zeroes in the time fields. Do you really need to take fractions of a second into account? – Borodin Nov 28 '14 at 22:24
  • @Borodin - `$l = "2014-11-28T00:00:00.000Z";` Assume the time pairs to be PST. I don't need to take the milliseconds into account when showing difference. Only years, month, days, hours, and mins is needed. Thanks. – smalls Nov 28 '14 at 22:27
  • 2
    The reason why everybody suggests doing it with a module is that doing it right is hard, and it is far easier to run `cpan` (or `cpanp`) and install the relevant modules than it is to write the code to do the job from first principle. One of the first qualities of a Perl programmer is laziness, in the sense of reusing what others have already done. The simplest module for you to install is probably [POSIX::strptime](http://search.cpan.org/perldoc?POSIX%3A%3Astrptime). The most comprehensive is undoubtedly the (extensive) [DateTime](http://search.cpan.org/perldoc?DateTime) family of modules. – Jonathan Leffler Nov 28 '14 at 22:28
  • @JonathanLeffler - Yes, but I am new and I don't know how to install modules into the correct perl. I do all the commands in online tutorials and the wrong perl gets the module. If there is a really easy way to install modules, then I'll do it now. Thanks. – smalls Nov 28 '14 at 22:29
  • @smalls: That is the same date and time as you show in the question. Please show *several pairs* of date/times for which you want to calculate the difference. Do they all have zero time fields and no times zone? – Borodin Nov 28 '14 at 22:33
  • Hmmm...what does that mean? I simply use the `cpan` or `cpanp` that's installed alongside the Perl I'm using, and everything gets installed in the correct place. I seldom (functionally never) use the O/S installed Perl — I need versions I can update without risk of breaking the rest of the system, so I have 5.8.9, 5.10.1, 5.12.1, 5.14.2, 5.16.2, 5.18.2 and 5.20.1 installed, and I simply run the `cpanp` in the relevant bin directory (`$HOME/perl/v5.20.1/bin/cpanp`, for example). – Jonathan Leffler Nov 28 '14 at 22:33
  • @JonathanLeffler - I have perls that were installed by strawberry installer and some other installers. I guess this has something to do with my problem. I had gone into some perl bin directory to run all those install commands. Let me try yours now. – smalls Nov 28 '14 at 22:35
  • @JonathanLeffler - cpanp is not recognized as a command. I'll try CPAN instead. Output is - Writing Makefile for Time::Piece 'nmake' is not recognized as an internal or external command, operable program or batch file. RJBS/Time-Piece-1.29.tar.gz nmake -- NOT OK Running make test Can't test without successful make Running make install Make had returned bad status, install seems impossible Failed during this command: RJBS/Time-Piece-1.29.tar.gz : make NO – smalls Nov 28 '14 at 22:37
  • `cpanp` is the script for [CPANPLUS](http://search.cpan.org/perldoc?CPANPLUS). You may need to install that first. I think CPAN still comes as standard. – Jonathan Leffler Nov 28 '14 at 22:39
  • If you have multiple installations of Perl then I'm not surprised you're having problems. Remove them all and start again. – Borodin Nov 28 '14 at 22:43
  • @Borodin - cannot. I need some of them to be there. – smalls Nov 28 '14 at 22:43
  • @JonathanLeffler: `CPANPLUS` is hardly essential, but it has been a core module since version 5.10. `CPAN` has been around since 5.00. – Borodin Nov 28 '14 at 22:46
  • @smalls: They are useless to you as it stands if you cannot determine which version of perl will be run. What reason do you have for needing multiple versions of Perl? – Borodin Nov 28 '14 at 22:47
  • @Borodin: and as I understand it, CPANPLUS will become non-core again after 5.20 (presumably 5.22). Yes, it isn't essential; I prefer it to CPAN, but they're both functional. – Jonathan Leffler Nov 28 '14 at 22:47
  • @JonathanLeffler: It never did much for me. But I find `cpanm` (`App:cpanm`) a big advantage. That has never been in core. – Borodin Nov 28 '14 at 22:48
  • 1
    @Borodin: [App::cpanminus](http://search.cpan.org/perldoc?App%3A%3Acpanminus) installs `cpanm`; I think that was what you meant, wasn't it? – Jonathan Leffler Nov 28 '14 at 22:57

1 Answers1

3

You don't seem to want to help us understand what it is you need.

This program will print the difference between 2014-11-28T00:00:00.000Z and 2014-11-28T00:00:00.000Z.

use strict;
use warnings;

use Time::Piece;

my $start = '2014-11-28T00:00:00.000Z';
my $end   = '2014-11-28T00:00:00.000Z';

print difference($start, $end);

sub difference {
  my ($beg, $end) = map Time::Piece->strptime(s/\.\d*Z?\z//r, '%Y-%m-%dT%H:%M:%S'), @_;
  ($end-$beg)->pretty;
}

output

0 seconds
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • Can't locate Time/Piece.pm in @INC (@INC contains: – smalls Nov 28 '14 at 22:45
  • 5
    @smalls: Then you have completely screwed up your Perl installations. `Time::Piece` has been part of core Perl since version 5.010, and if you are running 5.20 and it can't find it then something is seriously amiss. You need to remove *everything* and start from scratch. Take a look at [`App::perlbrew`](http://search.cpan.org/~gugod/App-perlbrew-0.72/lib/App/perlbrew.pm) if you *really* need to keep multiple versions of Perl. – Borodin Nov 28 '14 at 22:51