-1

My example was: How to compare dates using perl?

...
# $ARG1 is specified by the user for example "10" min

my $difftime=$ARG1; 

# -- Get date ---------------------------------------------------------------------
#Stores current date and time - $time min
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time() - (60*$difftime));
$year += 1900;

#parse date format to '22.10.2021 00:00:00' -----------------------------------------------------
my $act_date="$mday.$mon.$year $hour:$min:$sec";
...

My first problem is $hour and $mon are one to low why?

My second problem is the time is with one digit "8:4:34" and not with two digits "08:04:34". Is that a problem for a date comparison?

...
#compare date

# date example for $sql_value is 06.10.2021 09:38:27
my $date_to_compare=$sql_value;

if ($act_date <= $date_to_compare) 
{
 print "the current date is smaller";
} 
else 
{
 print "the current date is greater";
}
...

I get only "the current date is smaller" Why?

Additionally how can i get the time different in minutes?

thanks

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • 1
    Please, ask only one question per post. You mention that you use the question [How to compare dates using Perl](https://stackoverflow.com/q/17040842/4990392) as an example. The accepted answer suggest to use `Time::Piece` to do this, and yet you do not use this module; why is that? It would have make your life easier, in particular since the documentation of [`Time::Piece`](https://perldoc.perl.org/Time::Piece) explains how to solve most of your issues. – Dada Oct 28 '21 at 07:58
  • Look into [`Time::Piece`](https://perldoc.perl.org/Time::Piece) and [`Time::Seconds`](https://perldoc.perl.org/Time::Seconds). – Shawn Oct 28 '21 at 08:01
  • Or [`DateTime`](https://metacpan.org/pod/DateTime) from CPAN. – Shawn Oct 28 '21 at 08:03
  • @Apfelkuchen - Date strings can be sensibly compared only if ordered _year-mon-mday_. – Armali Oct 28 '21 at 08:30
  • A very related [post](https://stackoverflow.com/a/69069864/4653379), together with difference in hours/minutes. With both modules, too – zdim Oct 28 '21 at 09:00

1 Answers1

3

Dealing with dates and times at a low level like you're trying to do is really making your life far harder than it needs to be. You should follow the advice in the answers to the example that you link to and use Time::Piece.

My first problem is $hour and $mon are one to low why?

If $hour is wrong then that's likely to be down to timezone differences. But $mon is defined to be between 0 and 11 in the documentation for localtime(). This is part of what I mean by making your life too hard by working at this level.

You really haven't made it clear what you're trying to do. But you can make Time::Piece objects from a datetime string using strptime().

my $date_str = '22.10.2021 00:00:00';
my $date = Time::Piece->strptime($date_str, '%d.%m.%y %H:%M:%S');

Two Time::Piece objects can be compared directly:

if ($dt1 > $dt2) {
  # do something
} else {
  # do something else
}

If you subtract two Time::Piece objects, you get the number of seconds between the two timestamps.

my $secs = $dt1 - $dt2;

And you can use the strftime() method to get your timestamp in whatever format you want.

say $dt1->strftime('%d.%m.%y %H:%M:%S');
Dave Cross
  • 68,119
  • 3
  • 51
  • 97