cae7291

day increment program

Jan 8th, 2021 (edited)
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # code to increment day
  3.  
  4. master(@ARGV);
  5. exit(0);
  6.  
  7. sub master
  8. {
  9.  
  10. my(@days_in_month) = (31,28,31,30,31,30,31,31,31,30,30,31);
  11.  
  12. # input format:
  13. # 15,07/31/2020 0000,778,0,82
  14. while ($buf = <STDIN>) {
  15. chomp($buf);
  16.  
  17. # split off date and time
  18. my($date,$time) = split(" ",$buf);
  19.  
  20. # split up date
  21. my($pre,$mon,$day,$year) = split(m%[,/]%,$date);
  22.  
  23. # decide if current year is leap year
  24. my($isleap) = 0;
  25. {
  26. # year must be divisble by 4
  27. last if (($year % 4) != 0);
  28.  
  29. # if year is divisible by 100, it is _not_ a leap year unless ...
  30. if (($year % 100) == 0) {
  31. # ... it is also divisible by 400
  32. last if (($year % 400) != 0);
  33. }
  34.  
  35. $isleap = 1;
  36. }
  37.  
  38. # get number of days in february
  39. if ($isleap) {
  40. $days_in_month[1] = 29;
  41. }
  42. else {
  43. $days_in_month[1] = 28;
  44. }
  45.  
  46. # change only if time is 0000
  47. if ($time =~ /^0000,/) {
  48. # increment day
  49. $day += 1;
  50.  
  51. # advance month if necessary
  52. if ($day > $days_in_month[$mon - 1]) {
  53. $day = 1;
  54. $mon += 1;
  55. }
  56.  
  57. # advance year if necessary
  58. if ($mon > 12) {
  59. $mon = 1;
  60. $year += 1;
  61. }
  62. }
  63.  
  64. $date = sprintf("%2.2d,%2.2d/%2.2d/%4.4d",$pre,$mon,$day,$year);
  65.  
  66. printf("%s %s\n",$date,$time);
  67. }
  68. }
  69.  
Add Comment
Please, Sign In to add comment