how to convert date time to epoch time in solaris


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to convert date time to epoch time in solaris
# 1  
Old 07-27-2009
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?
# 2  
Old 07-27-2009
Hi.

There's lots of examples of this kind of thing on the forum if you search.

This may be useful Epoch Converter - Unix Timestamp Converter

%s shows seconds since the epoch. It doesn't take a variable and format it to seconds since the epoch.

There (as I know) is no way to do this in normal ksh, etc, but ksh93 has some features. But ksh93 (at least on my Solaris) doesn't exist.
# 3  
Old 07-27-2009
I have a shell script that will do it, but this might be easier if you can use it:

Code:
perl -e 'print time."\n"'

# 4  
Old 07-28-2009
see also - This thread.

---------- Post updated at 11:57 AM ---------- Previous update was at 01:41 AM ----------

for solaris (without perl , %s, and -d)

I found this:

Code:
truss /usr/bin/date 2>&1 | grep ^time | awk -F"= " '{print $2}'

Note: use nawk if awk doesn't work.
# 5  
Old 07-28-2009
convert date to epoch time in solaris

I have some start time like : 2009-07-08 06:52:05
and end time : 2009-07-08 06:52:44

now here I want to subtract start time from end time and get the result in seconds

which I was doing in linux like :

Code:
epoch_time_start=`date +%s -d"$1"`
epoch_time_end=`date +%s -d"$2"`
total_time=`expr $epoch_time_end - $epoch_time_start`

but the same is not working in solaris....I can not use perl because it is not available in the system where I have to deploy this script.....
# 6  
Old 07-28-2009
To convert a date to seconds for easier math, see pseudo code:

Code:
Get date values of the date to be converted for second, minute, hour, day, month, year.

if month > 2 then
  month=month+1
else
  month=month+13
  year=year-1
fi

day=(year*365)+(year/4)-(year/100)+(year/400)+(month*306001/10000)+day
days_since_epoch=day-719591 (which is Jan 1 1970)
seconds_since_epoch=(days_since_epoch*86400)+(hour*3600)+(minute*60)+seconds

See more detail in Chapter 3 of Expert Shell Scripting - Apress.
# 7  
Old 08-22-2009
The +%s format for date and date functions like strftime() is not defined by POSIX standards, oddly enough. So a lot of systems - Solaris being one - require the use of perl or ruby or python to do that. Or a complex shell function.

I think this is on the list of changes for the new C standards.

Date/time has always been a problem child because of locales, different calendars and so on.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Second Column Date Into EPOCH Time And Print Complete Row

Hello Team, I am stuck in getting the required output in the following case. Please help. My input file is aa|08/01/2016 bb|08/15/2016 I wish to convert the file into aa|epoch time bb|epoch time I am using following code: (3 Replies)
Discussion started by: angshuman
3 Replies

2. 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

3. Shell Programming and Scripting

Convert epoch time to Julian date

Need assistance in converting an epoch time to Julian date To get epoch perl -e 'use Time::Local; print timelocal(1,5,2,12,10,2008), "\n"' (3 Replies)
Discussion started by: ajayram_arya
3 Replies

4. 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

5. Shell Programming and Scripting

Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do. What am I trying to do? I need to take the time value in epoch format returned from the... (5 Replies)
Discussion started by: minigts
5 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

Convert epoch to human readable date & time format

Hello I have log file from solaris system which has date field converted by Java application using System.currentTimeMillis() function, example is 1280943608380 which equivalent to GMT: Wed, 04 Aug 2010 17:40:08 GMT. Now I need a function in shell script which will convert 1280943608380... (3 Replies)
Discussion started by: Yaminib
3 Replies

8. 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

9. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

10. Shell Programming and Scripting

how to convert epoch time to readible format?

Hi, I would like to convert epoch time from the logs to readible fromat. How do I do it within shell? Thanks! (11 Replies)
Discussion started by: cin2000
11 Replies
Login or Register to Ask a Question