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
Time::Local(3pm)					 Perl Programmers Reference Guide					  Time::Local(3pm)

NAME
Time::Local - efficiently compute time from local and GMT time SYNOPSIS
$time = timelocal($sec,$min,$hour,$mday,$mon,$year); $time = timegm($sec,$min,$hour,$mday,$mon,$year); DESCRIPTION
These routines are the inverse of built-in perl functions localtime() and gmtime(). They accept a date as a six-element array, and return the corresponding time(2) value in seconds since the Epoch (Midnight, January 1, 1970). This value can be positive or negative. It is worth drawing particular attention to the expected ranges for the values provided. The value for the day of the month is the actual day (ie 1..31), while the month is the number of months since January (0..11). This is consistent with the values returned from local- time() and gmtime(). The timelocal() and timegm() functions perform range checking on the input $sec, $min, $hour, $mday, and $mon values by default. If you'd rather they didn't, you can explicitly import the timelocal_nocheck() and timegm_nocheck() functions. use Time::Local 'timelocal_nocheck'; { # The 365th day of 1999 print scalar localtime timelocal_nocheck 0,0,0,365,0,99; # The twenty thousandth day since 1970 print scalar localtime timelocal_nocheck 0,0,0,20000,0,70; # And even the 10,000,000th second since 1999! print scalar localtime timelocal_nocheck 10000000,0,0,1,0,99; } Your mileage may vary when trying these with minutes and hours, and it doesn't work at all for months. Strictly speaking, the year should also be specified in a form consistent with localtime(), i.e. the offset from 1900. In order to make the interpretation of the year easier for humans, however, who are more accustomed to seeing years as two-digit or four-digit values, the following conventions are followed: o Years greater than 999 are interpreted as being the actual year, rather than the offset from 1900. Thus, 1963 would indicate the year Martin Luther King won the Nobel prize, not the year 2863. o Years in the range 100..999 are interpreted as offset from 1900, so that 112 indicates 2012. This rule also applies to years less than zero (but see note below regarding date range). o Years in the range 0..99 are interpreted as shorthand for years in the rolling "current century," defined as 50 years on either side of the current year. Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045, but 55 would refer to 1955. Twenty years from now, 55 would instead refer to 2055. This is messy, but matches the way people currently think about two digit dates. Whenever possible, use an absolute four digit year instead. The scheme above allows interpretation of a wide range of dates, particularly if 4-digit years are used. Please note, however, that the range of dates that can be actually be handled depends on the size of an integer (time_t) on a given plat- form. Currently, this is 32 bits for most systems, yielding an approximate range from Dec 1901 to Jan 2038. Both timelocal() and timegm() croak if given dates outside the supported range. IMPLEMENTATION
These routines are quite efficient and yet are always guaranteed to agree with localtime() and gmtime(). We manage this by caching the start times of any months we've seen before. If we know the start time of the month, we can always calculate any time within the month. The start times are calculated using a mathematical formula. Unlike other algorithms that do multiple calls to gmtime(). timelocal() is implemented using the same cache. We just assume that we're translating a GMT time, and then fudge it when we're done for the timezone and daylight savings arguments. Note that the timezone is evaluated for each date because countries occasionally change their official timezones. Assuming that localtime() corrects for these changes, this routine will also be correct. BUGS
The whole scheme for interpreting two-digit years can be considered a bug. The proclivity to croak() is probably a bug. perl v5.8.0 2002-06-01 Time::Local(3pm)
All times are GMT -4. The time now is 06:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy