Listing 1 GetEpochTime
#!/bin/ksh
# Function name: GetEpochTime
# [ Insert get_astro_JD function here ]
GetEpochTime ()
{
# Initialize variables
BeginTime=1970
TotalDays=0
# get the number of days from JD to Jan. 1, 1970
DayOneAstrJulian=$(get_astro_JD 1 1 1970)
# ... and decrease by 1
DayOneAstrJulian=$(($DayOneAstrJulian-1))
#Calculate this year (YYYY - 1)
ThisYearMinusOne=$(( $(date -u +"%Y") - 1))
# Loop, counting up from 1970 to (current year) - 1)) to add up total days
# in each year.
while [[ $BeginTime -le $ThisYearMinusOne ]]
do
# Get the astro_JD of the current year
LastDayOfYearAstrJulian=$(get_astro_JD 31 12 $BeginTime)
# Subtract the days since Jan. 1, 1970)
TotalDaysTmp=$(($LastDayOfYearAstrJulian-$DayOneAstrJulian))
# bump the running total of total days
TotalDays=$(( $TotalDays + $TotalDaysTmp))
# Assign the get_astro_JD integer value for "last day of current year" to
# "the first day of (current year) + 1)".
DayOneAstrJulian=$LastDayOfYearAstrJulian
# Increment the current loop year by 1, and continue loop
BeginTime=$(( $BeginTime + 1))
done
# Add "total days elapsed from Jan 1, 1970 thru last year" to current
# Julian day (1-365), then subtract 1 (today) from the value and assign the
# value to a variable that represents total elapsed days from the epoch
# to yesterday at midnight.
TotalDays=$(( $TotalDays + $(date -u +"%j") - 1 ))
# Calculate the number of seconds from the total days
TotalSeconds=$(( $TotalDays * 86400))
# get the current system time in "hh:mm:ss" format.
TodaysHourMinSec=`date -u +"%T"`
# get rid of the colons
TodaysHourMinSec=$(echo $TodaysHourMinSec|tr : ' ')
# set the values to variables $1, $2, and $3.
set - $(echo $TodaysHourMinSec)
# get the total number of seconds for today
TotalSecondsToday=$(( ($1 * 3600) + ($2 * 60) + $3))
# Add second from today, to total elapsed seconds from the epoch
# thru yesterday
TheEpochTime=$(( $TotalSeconds + $TotalSecondsToday))
# return the total number of seconds from the epoch
echo $TheEpochTime
}
|