Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Need help on this.

I would like to create a script that will receive 2 parameters (time) with following format AA:BB (AA goes from 00 to 200 and BB from 0 to 59).

And it should add those to time and give the results, if the result is more than 24 h, it make it 1 day, if it is more than 7 days a week.

For example, 23:03 11:05 will give 1 day and 10:08, and 35:05 162:01 will give 1 week, 1 day and 5:06.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
219 views
Welcome To Ask or Share your Answers For Others

1 Answer

Before we start, I want to remind you that not every day is 24 hours long. The length of a day can be 22, 23, 24, 25 or 26 hours. This means that what you want to do will inherently add errors to your data.

I'd normally be the first to instruct you to use a module, because a good date/time module is designed to prevent you from doing exactly what you ask for. That means it's easier to solve your problem from scratch rather that using a module. In fact, it's not even possible to do what you want with the module I normally use, DateTime.

my ($hours1, $minutes1) = split /:/, $arg1;
my ($hours2, $minutes2) = split /:/, $arg2;

my $hours   = $hours1   + $hours2;
my $minutes = $minutes1 + $minutes2;

$hours   += ($minutes - ($minutes % 60)) / 60;  $minutes %= 60;
my $days  = ($hours   - ($hours   % 24)) / 24;  $hours   %= 24;
my $weeks = ($days    - ($days    %  7)) /  7;  $days    %=  7;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...