Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # code to increment day
- master(@ARGV);
- exit(0);
- sub master
- {
- my(@days_in_month) = (31,28,31,30,31,30,31,31,31,30,30,31);
- # input format:
- # 15,07/31/2020 0000,778,0,82
- while ($buf = <STDIN>) {
- chomp($buf);
- # split off date and time
- my($date,$time) = split(" ",$buf);
- # split up date
- my($pre,$mon,$day,$year) = split(m%[,/]%,$date);
- # decide if current year is leap year
- my($isleap) = 0;
- {
- # year must be divisble by 4
- last if (($year % 4) != 0);
- # if year is divisible by 100, it is _not_ a leap year unless ...
- if (($year % 100) == 0) {
- # ... it is also divisible by 400
- last if (($year % 400) != 0);
- }
- $isleap = 1;
- }
- # get number of days in february
- if ($isleap) {
- $days_in_month[1] = 29;
- }
- else {
- $days_in_month[1] = 28;
- }
- # change only if time is 0000
- if ($time =~ /^0000,/) {
- # increment day
- $day += 1;
- # advance month if necessary
- if ($day > $days_in_month[$mon - 1]) {
- $day = 1;
- $mon += 1;
- }
- # advance year if necessary
- if ($mon > 12) {
- $mon = 1;
- $year += 1;
- }
- }
- $date = sprintf("%2.2d,%2.2d/%2.2d/%4.4d",$pre,$mon,$day,$year);
- printf("%s %s\n",$date,$time);
- }
- }
Add Comment
Please, Sign In to add comment