ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Perl - Get Today's Date, Yesterday's Date, and Current Time

    IT Discussion
    4
    6
    2.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • anthonyhA
      anthonyh
      last edited by

      I'm working on cleaning up some Perl (5.3.3) scripts on a Windows server an old colleague of mine put together. These scripts basically check today's date and compares it to an array of dates (our holidays). If today's date matches a value in the array, it quits (i.e. doesn't run said process).

      The way he's checking the date, to me, is a bit convoluted. He's connecting to a MySQL instance on another server to grab today's date, yesterday's date, and current time (three variables).

      $database = "aMySQLdb";
      my $hostname = "10.10.10.10";
      my $port = '3306';
      $user = "auser";
      $password = "apassword";
      my $query = "SELECT NOW()";
      
      $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
      $dbh = DBI->connect($dsn, $user, $password, {'RaiseError' => 1});
      $dbh->{LongReadLen} = 2000;
      $dbh->{LongTruncOk} = 1;
      
      my $rc;
      my @ts;
      my $dbTime;
      my $dbYesterday;
      my $holiday_flag = 'c:\perl\scripts\logs\holiday_flag';
      
      $sth = $dbh->prepare("select date_format(curdate(), '%m/%d/%Y')");
      $rc = $sth->execute;
      @ts = $sth->fetchrow_array;    
      $sth->finish;
                                      
      $dbToday = $ts[0];  
      
      $sth = $dbh->prepare("select date_format(curdate()-1, '%Y%m%d')");
      $rc = $sth->execute;
      @ts = $sth->fetchrow_array;    
      $sth->finish;
                                      
      $dbYesterday = $ts[0];  
        
      $sth = $dbh->prepare("select date_format(sysdate(), '%Y-%m-%d %H:%i:%s')");
      $rc = $sth->execute;
      @ts = $sth->fetchrow_array;    
      $sth->finish;
      $dbh->disconnect;
                                      
      $dbTime = $ts[0];
      

      I am no Perl expert (this is actually my first experience with Perl), but this seems insane. I must be able to simplify this.

      Any suggestions, oh ML wizards?

      Thanks!

      1 Reply Last reply Reply Quote 1
      • scottalanmillerS
        scottalanmiller
        last edited by

        Um.... WHAT!!! LOL That is certainly insane.

        anthonyhA 1 Reply Last reply Reply Quote 0
        • anthonyhA
          anthonyh @scottalanmiller
          last edited by

          @scottalanmiller said in Perl - Get Today's Date, Yesterday's Date, and Current Time:

          Um.... WHAT!!! LOL That is certainly insane.

          Yeah, that's what I'm saying!! I just need to figure out how to set these three variables...

          $dbToday = "Today's date in MM/DD/YYYY"
          $dbYesterday = "Yesterday's date in MM/DD/YYYY"
          $dbTime = "Current Date/Time in YYYY-MM-DD HH:MM:SS"
          

          I sure it can be done in less than half the current lines of code. Maybe even three one-liners. As soon as I'm off this extremely boring conference call I'll be heading to The Google. 😄

          1 Reply Last reply Reply Quote 1
          • scottalanmillerS
            scottalanmiller
            last edited by

            One liners for sure, I just don't have the Perl memorized and am about to go to dinner now that the protests have moved on and are burning effigies of the president elsewhere. Military has moved on, so safe to go eat now.

            gjacobseG 1 Reply Last reply Reply Quote 0
            • gjacobseG
              gjacobse @scottalanmiller
              last edited by

              @scottalanmiller said in Perl - Get Today's Date, Yesterday's Date, and Current Time:

              One liners for sure, I just don't have the Perl memorized and am about to go to dinner now that the protests have moved on and are burning effigies of the president elsewhere. Military has moved on, so safe to go eat now.

              Uhm - that is a big questionable 'Safe' statement...

              1 Reply Last reply Reply Quote 0
              • dafyreD
                dafyre
                last edited by

                Here's a way to get Yesterday today and tomorrow... I'm not a super proficient Perl programmer.. but htis is what I could cobble together...

                
                #!/usr/bin/perl
                
                my $today=time(); #Todays time in seconds since ~Jan 1, 1970
                
                my $yesterday=localtime($today -  86400); #Subtract 86,400 seconds (one whole day)
                
                my $tomorrow=localtime($today + 86400); #Add 86,400 seconds (one whole day)
                
                $today=localtime($today); #Convert today into a proper date/time format.
                
                print "Yesterday: $yesterday\n\r";
                print "Today: $today\n\r";
                print "Tomorrow: $tomorrow\n\r";
                
                1 Reply Last reply Reply Quote 2
                • 1 / 1
                • First post
                  Last post