How can I get the approximate time elapsed from some timestamp?

Is there any useful module in CPAN that returns the number of largest fractions of a date range, i.e. returns an integer of years / months / days / hours / minutes / seconds between two dates for use in a sentence like "N days ago", or "M months ago"

+3
source share
5 answers

DateTime and DateTime :: Duration are the best modules:

use strict;
use warnings;

use DateTime;
use DateTime::Duration;
use Data::Dumper;

my $dt1 = DateTime->now;
my $dt2 = DateTime->new(
    year => 2001,
    month => 01,
    day => 01
);

my $duration = $dt1 - $dt2;
print Dumper($duration);

gives:

$VAR1 = bless( {
                 'seconds' => 38,
                 'minutes' => 181,
                 'end_of_month' => 'wrap',
                 'nanoseconds' => 0,
                 'days' => 20,
                 'months' => 110
               }, 'DateTime::Duration' );

- DateTime::Format DateTime::Format::Duration.

+4

, .

#

+2

Time:: Duration , . . , , "M months ago", .

+1

Time:: Ago CPAN ( ). Perl time_ago_in_words() Rails.

, . Rails:

0  29 secs
  less than a minute

30 secs  1 min, 29 secs
  1 minute

1 min, 30 secs  44 mins, 29 secs
  [2..44] minutes

44 mins, 30 secs  89 mins, 29 secs
  about 1 hour

89 mins, 30 secs  23 hrs, 59 mins, 29 secs
  about [2..24] hours

23 hrs, 59 mins, 30 secs  41 hrs, 59 mins, 29 secs
  1 day

41 hrs, 59 mins, 30 secs  29 days, 23 hrs, 59 mins, 29 secs
  [2..29] days

29 days, 23 hrs, 59 mins, 30 secs  44 days, 23 hrs, 59 mins, 29 secs
  about 1 month

44 days, 23 hrs, 59 mins, 30 secs  59 days, 23 hrs, 59 mins, 29 secs
  about 2 months

59 days, 23 hrs, 59 mins, 30 secs  1 yr minus 1 sec
  [2..12] months

1 yr  1 yr, 3 months
  about 1 year

1 yr, 3 months  1 yr, 9 months
  over 1 year

1 yr, 9 months  2 yr minus 1 sec
  almost 2 years

2 yrs  max time or date
  (same rules as 1 yr)

CPAN

+1

[Actually this is not the answer, but I think it is relevant.]

If you intend to use date and time manipulations and calculations, you better not use the DateTime family of modules. Not only do they have the best time clock management I found, but they all work with each other. I never had a problem in this domain in which I could not find a module in DateTime to solve it.

0
source

Source: https://habr.com/ru/post/1737855/


All Articles