Epoch finder

From: Bill Verzal (BVerzal_at_KOMATSUNA.COM)
Date: 04/29/04

  • Next message: Stamper, Steve: "Re: Restoring file from MKSYSB (summary)"
    Date:         Thu, 29 Apr 2004 11:15:13 -0500
    To: aix-l@Princeton.EDU
    
    

    OK - I have an old C program print out the epoch. I also have an old C
    program that converts the epoch to a 'date' command. I know Perl can do
    the same. Doe anyone have a program that takes the date in YYYY/MM/DD and
    HH:MM:SS format and can print out the epoch ?

    I created a script as below to do it and it is functional. Feel free to
    keep it and to offer suggestions on it.

    Thanks, Bill.

    #!/usr/bin/ksh
    #
    # Enter a date/time and I'll find the epoch.
    #
    # Input #1: yyyy/mm/dd
    # Input #2: HH:MM:SS
    #
    # I'll find the epoch.
    #
    perl="/usr/komatsu/bin/perl -we"
    p1="$perl 'print time'"
    p2="$perl 'print scalar localtime'"

    # ### P1=`eval $p1`
    # ### P2=`eval $p2 $P1`
    # ### The commands must be 'eval'-ed to work. See above.
    #

    echo "Enter date (format YYYY/MM/DD)"
    read yyyyxmmxdd
    echo "Enter time (format HH:MM:SS)"
    read hhxmmxss
    now=`eval $p1`

    d1=`echo $yyyyxmmxdd|wc -c|awk {'print $1'}`
    t1=`echo $hhxmmxss|wc -c|awk {'print $1'}`

    # The character counts are off by one because of the \n

    if [ "$d1" -ne "11" ] ; then
       echo "Invalid date format - YYYY/MM/DD only"
       exit 1
    fi
    if [ "$t1" -ne "9" ] ; then
       echo "Invalid time format - HH:MM:SS only"
       exit 1
    fi

    # Now, break the fields up...

    yyyy=`echo "$yyyyxmmxdd"|cut -c1-4`
    mm=`echo "$yyyyxmmxdd"|cut -c6-7`
    dd=`echo "$yyyyxmmxdd"|cut -c9-10`
    HH=`echo "$hhxmmxss"|cut -c1-2`
    MM=`echo "$hhxmmxss"|cut -c4-5`
    SS=`echo "$hhxmmxss"|cut -c7-8`

    secday="86400"
    secweek="604800"
    secyear="31449600"
    secmo="2620800"
    sechour="3600"
    secmin="60"

    # Work on the year first...
    q=`date +%Y`
    if [ "$q" -eq "$yyyy" ] ; then
       e="$now"
    else
       if [ "$q" -gt "$yyyy" ] ; then
          w=`echo "$q-$yyyy"|bc -l`
          r=-1
       else
          w=`echo "$yyyy-$q"|bc -l`
          r=1
       fi
       e=`echo "($secyear*$w)*$r" |bc -l`
       y=`echo "$now+$e"|bc -l`
       e=$y
    fi
    # ### echo "epoch is now: $e"
    # ### perl -we "print scalar localtime $e"
    # ### echo " "

    # Now the month
    q=`date +%m`
    if [ "$q" -eq "$mm" ] ; then
       :
    else
       if [ "$q" -gt "$mm" ] ; then
          w=`echo "$q-$mm"|bc -l`
          r=-1
       else
          w=`echo "$mm-$q"|bc -l`
          r=1
       fi
       e1=`echo "($secmo*$w)*$r" |bc -l`
       e=`echo "$e+$e1"|bc -l`
    fi
    # ### echo "epoch is now: $e"
    # ### perl -we "print scalar localtime $e"
    # ### echo " "

    # Now the day
    q=`date +%d`
    if [ "$q" -eq "$dd" ] ; then
       :
    else
       if [ "$q" -gt "$dd" ] ; then
          w=`echo "$q-$dd"|bc -l`
          r=-1
       else
          w=`echo "$dd-$q"|bc -l`
          r=1
       fi
       e1=`echo "($secday*$w)*$r" |bc -l`
       e=`echo "$e+$e1"|bc -l`
    fi
    # ### echo "epoch is now: $e"
    # ### perl -we "print scalar localtime $e"
    # ### echo " "

    # In case the date is off due to leap years or other
    # things, check with a tweak.
    D=`perl -we "print scalar localtime $e"|awk {'print $3'}`

    # Now the day #2
    if [ "$D" -eq "$dd" ] ; then
       :
    else
       if [ "$D" -gt "$dd" ] ; then
          w=`echo "$D-$dd"|bc -l`
          r=-1
       else
          w=`echo "$dd-$D"|bc -l`
          r=1
       fi
       e1=`echo "($secday*$w)*$r" |bc -l`
       e=`echo "$e+$e1"|bc -l`
    fi

    # ### echo "\n\nepoch is now: $e"
    # ### perl -we "print scalar localtime $e"
    # ### echo "\n\n"

    #
    # At this stage of the game, testing indicates that we
    # have the year, month and day correct. Now we need
    # to tweak the time.
    #

    savE=$e # Save the epoch as we have it in case we need it.

    eTime=`perl -we "print scalar localtime $e" |awk {'print $4'}`

    eTimeHH=`echo "$eTime"|cut -f1 -d":"`
    eTimeMM=`echo "$eTime"|cut -f2 -d":"`
    eTimeSS=`echo "$eTime"|cut -f3 -d":"`

    # Now the hour
    q="$HH"
    if [ "$q" -eq "$eTimeHH" ] ; then
       :
    else
       if [ "$q" -gt "$eTimeHH" ] ; then
          w=`echo "$q-$eTimeHH"|bc -l`
          r=1
       else
          w=`echo "$eTimeHH-$q"|bc -l`
          r=-1
       fi
       e1=`echo "($sechour*$w)*$r" |bc -l`
       e=`echo "$e+$e1"|bc -l`
    fi
    # ### echo "epoch is now: $e"
    # ### perl -we "print scalar localtime $e"
    # ### echo " "

    # Now the minute
    q="$MM"
    if [ "$q" -eq "$eTimeMM" ] ; then
       :
    else
       if [ "$q" -gt "$eTimeMM" ] ; then
          w=`echo "$q-$eTimeMM"|bc -l`
          r=1
       else
          w=`echo "$eTimeMM-$q"|bc -l`
          r=-1
       fi
       e1=`echo "($secmin*$w)*$r" |bc -l`
       e=`echo "$e+$e1"|bc -l`
    fi
    # ### echo "epoch is now: $e"
    # ### perl -we "print scalar localtime $e"
    # ### echo " "

    # Now the seconds
    q="$SS"
    if [ "$q" -eq "$eTimeSS" ] ; then
       :
    else
       if [ "$q" -gt "$eTimeSS" ] ; then
          w=`echo "$q-$eTimeSS"|bc -l`
          r=1
       else
          w=`echo "$eTimeSS-$q"|bc -l`
          r=-1
       fi
       e1=`echo "($w)*$r" |bc -l`
       e=`echo "$e+$e1"|bc -l`
    fi
    #
    # At this point we should have it.
    #
    echo "$e"

    --------------------------------------------------------

    "If everything is coming your way, then you are in the wrong lane"

    Bill Verzal
    AIX Administrator, Komatsu America
    (847) 970-3726 - direct
    (847) 970-4184 - fax


  • Next message: Stamper, Steve: "Re: Restoring file from MKSYSB (summary)"

    Relevant Pages

    • [HPADM] RE: Getting the date from the number of seconds since epoch
      ... print scalar localtime $ARGV; ... Just give the epoch as input. ... OR doing single command line: ... [HPADM] Getting the date from the number of seconds since epoch ...
      (HP-UX-Admin)
    • Re: uptime
      ... # Enter a date/time and I'll find the epoch. ... # things, check with a tweak. ... echo "$e" ... Subject: uptime ...
      (AIX-L)
    • [HPADM] Getting the date from the number of seconds since epoch
      ... Anyone know a correlated command to the perl command given below that ... Subject: [HPADM] SUMMARY: Getting the data in seconds since epoch ... To subscribe/unsubscribe to this list, ...
      (HP-UX-Admin)
    • Re: Use of set
      ... "getting time since epoch using shell commands? ... date _may_ modify its output for some values of those environment ... the seconds since the epoch, then exit, I doubt this is necessary, as the ... and then runs the specified command. ...
      (comp.unix.shell)
    • Re: RPM naming question.
      ... Aaron Konstam wrote: ... This is what I get when I run the above command: ... Try breaking the strings up manually (and adding an epoch of 0): ... Tact is just a mutual agreement to be full of shit. ...
      (Fedora)