Sponsored Content
Top Forums Shell Programming and Scripting Convert from standard epoch time from a shell script? Post 83643 by Perderabo on Friday 16th of September 2005 03:14:16 PM
Old 09-16-2005
Each version of unix handles timezones a little differently. Ignoring timezones more or less, this comes fairly close



Code:
#! /usr/bin/ksh


#
#  convert unix time to a string
#
#   time="$(unixsecond2timestring $seconds)" is
#      similiar to the c construct:
#               strcpy(time,ctime(&xdate));
#      except that it ignores timezone considerations.
#      This means that it is exactly like:
#          strcpy(time,asctime(gmtime(&xdate)));
#
#      The only way to handle timezones is to figure out
#      your local number of seconds difference from GMT and
#      adjust the value of seconds before passing it.  This
#      means that for small values of "seconds" you may adjust
#      it to a negative number.  That's ok, this routine can
#      handle numbers in the range -86400 to 2147483647.
#
unixsecond2timestring() {
        integer uxsec mjd daysecond hour hoursecond minute second
        typeset -Z2 val
        typeset -R2 val2
        typeset -L3 fdow
        typeset dow time year month day
        typeset months
        set -A months xxx Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
        uxsec=$1

#
#  Calculate
#    mjd=modified julian day number  (range is 40586 - 65442)
#      Dec 31, 1969 has  mjd=40586
#      Jan 19, 2038 has  mjd=65442
#    daysecond=number of second during the day (range is 0 - 86399)
#    hoursecond=number of second during hour   (range is 0 - 3599)
#    hour, minute, second represent current time
#
        ((mjd=(uxsec/86400)+40587))
        ((daysecond=uxsec%86400))
        ((hour=daysecond/3600))
        ((hoursecond=daysecond-(hour*3600)))
        ((minute=hoursecond/60))
        ((second=hoursecond%60))

#
#    Adjust things if we are negative
        if ((uxsec<0)) ; then
                ((mjd=mjd-1))
                ((hour=(hour+24)%24))
        fi

#
#    Convert mjd to year, month day and get dow (day of week)
        datecalc -j $mjd | read year month day
        dow=$(datecalc -D $year $month $day)

#
#    Format the date
        val=$hour
        time="${val}:"
        val=$minute
        time="${time}${val}:"
        val=$second
        time="${time}${val} $year"
        fdow=$dow
        val2=$day
        time="${fdow} ${months[month]} $val2 $time"
        echo "$time"
        return
}


integer unixsecond
typeset -R11 dsecond

while (($#)) ; do
        unixsecond=$1
        shift
        time1=$(unixsecond2timestring $unixsecond)
        dsecond=$unixsecond
        ((unixsecond2=unixsecond-(5*3600)))
        time2=$(unixsecond2timestring $unixsecond2)

        print "arg = $dsecond     ${time1}    ${time2}"
done
exit 0

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies

2. Solaris

epoch time in shell script

how can I get the current standard epoch time (seconds from 1970) in a shell script? I know I could do this with a bit of perl of even c++ but i want to do it in Bourne shell..... (14 Replies)
Discussion started by: robsonde
14 Replies

3. Shell Programming and Scripting

how to convert date time to epoch time in solaris

Hi, Is there any easy way to convert date time(stored in shell variable ) to epoch time in solaris box? As +%s is working on linux but not on solaris, also -d option is not working. Any suggestion please? (6 Replies)
Discussion started by: anshuman0507
6 Replies

4. Shell Programming and Scripting

Convert value stored in a variable to epoch time?

Hello I have a the creation date of a file stored in a variable in the following format: Wed May 06 10:14:58 2009Is there a way I can echo the variable and display it in epoch time? I've done a lot of searching on this topic, but haven't managed to get a solution. I'm on Solaris 10. ... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies

5. Shell Programming and Scripting

need shell or Perl script to get the epoch time automatically

I need shell or Perl script to get the epoch time automatically Example I need to execute mysql command to delete content less then given epoch time If date is 01-07-2010 (dd-mm-yy) epoch should be lees 7 days mean 23-06-2010 for 23-06-2010 I need epoch time Delete BS_table where... (1 Reply)
Discussion started by: sreedhargouda
1 Replies

6. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

7. Shell Programming and Scripting

Shell Script for Epoch Time Conversion

Hi there I came across this script online to convert Epoch time to proper date format, but I am receiving the following error Also, I have HISTTIMEFORMAT set in user's .profile so that their history output shows time stamps. Additionally I have changed their .history location to a dedicated... (9 Replies)
Discussion started by: hedkandi
9 Replies

8. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

9. Shell Programming and Scripting

Convert to epoch time

how can i modify the following command to instead provide the epoch time of the interfaces file? perl -le 'print scalar localtime ((stat "/home/skysmart/interfaces"))' Tue Feb 19 03:44:52 2013 i'm hoping to get the equivalent of this command: stat --format=%Y /home/skysmart/interfaces ... (2 Replies)
Discussion started by: SkySmart
2 Replies

10. Shell Programming and Scripting

Convert a string to epoch time

Team, I am working on a shell script and i am extracting a date string in "SunOS server" with below format. Mon Jan 21 04:13:48 EST 2021 Can you please assist me the best way to convert the extracted string to epoch time like "date +%s" in Linux. Thanks in advance (1 Reply)
Discussion started by: Girish19
1 Replies
GETDATE(1)                                                         User Commands                                                        GETDATE(1)

NAME
getdate - AME SYNOPSIS
[-dv][-n dec][-f format] [ra dec sys] itype2otype [date and/or time] DESCRIPTION
Convert date and time between various formats [-dv][-n dec][-f format] itype2otype @file itype: nfd=ISOFITS fd=FITS, dt=yyyy.mmdd, hr=hh:mm:ss, deg=dd:mm:ss jd=Julian Date, mjd=Modified Julian Date hjd=Heliocentric Julian Date, mhjd=Modified HJD ep=epoch, epj=Julian epoch, epb=Besselian epoch lt=local time, ut=UT, ts=seconds since 1950-01-01 now=current time, ang=fractional degrees otype: fd=FITS, dt=yyyy.mmdd, jd=Julian Date, mjd=Modified Julian Date hjd=Heliocentric Julian Date, mhjd=Modified HJD hr=hh:mm:ss, deg=dd:mm:ss, ang=fractional degrees ep=epoch, epj=Julian epoch, epb=Besselian epoch ts=seconds since 1950-01-01, tsu=Unix sec, tsi=IRAF sec gst=Greenwich Sidereal Time, lst=Local Sidereal Time @file: First one or two columns are in itype format ra dec sys: Need for Heliocentric conversions -a: Append date to input file, if there is one -d: Print date without time -e: Print output as ET/TDT/TT converting from UT -f: Format for output number (C printf) -h hours: Longitude in hours, west positive -l degrees: Longitude in degrees, west positive -n: Number of decimal places in sec, epoch, JD -t: Print time without date -v: Verbose getdate 3.8.4 June 2012 GETDATE(1)
All times are GMT -4. The time now is 04:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy