Shell Script for Epoch Time Conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script for Epoch Time Conversion
# 1  
Old 03-22-2012
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 directory in /var/log. Example as below:

Code:
export HISTFILE=/var/log/user_history/JP1_history

export HISTFILESIZE=20000

export HISTSIZE=5000

export HISTTIMEFORMAT="%F %T "

This is the error message from the script:

Code:
[root@H99A100 user_history]# ./test
Month '23' out of range 0..11 at -e line 2

This is the script I am using:

Code:
#!/bin/bash
# YYYY MM DD HH MM SS
# perl autosplits the string and uses timelocal to return
# the number of seconds from the Epoch
# No error checking!
function seconds_from_epoch {
echo $*| perl -MTime::Local -ane '
my $epochseconds = timelocal($F[5], $F[4], $F[3], $F[2], $F[1], $F[0]);
print "$epochseconds\n"; '
}

# parse file s32adm_bash_history date and time
set - $(ls -l s32adm_bash_history)
fdate=$6
ftime=$7

# parse the year, month, and day
set - $(IFS="-"; echo $fdate)
fyear=$1
fmonth=$2
fday=$3

# parse the hours and minutes
set - $(IFS=":"; echo $ftime)
fhour=$1
fmin=$2

totsecs=$(seconds_from_epoch "$fyear $fmonth $fday $fhour $fmin 0")
echo $totsecs

Can someone please point out what I am doing wrong?
# 2  
Old 03-22-2012
Code:
echo $*

you are not passing any input arguments for your program.
# 3  
Old 03-22-2012
Hi itkamaraj

I added echo $epochseconds| perl -MTime::Local -ane '

But i am getting this error now

Code:
[root@H99A100 user_history]# ./test
Day '' out of range 1..31 at -e line 2

# 4  
Old 03-23-2012
Quote:
Originally Posted by itkamaraj
Code:
echo $*

you are not passing any input arguments for your program.
@itkamaraj: $* is used in a function and it receives from the 2nd last line of the original script in post #1.

@hedkandi: Your original script in post #1 works for me without errors. Though, I didn't validate if the time in seconds from epoch is correct or not.
# 5  
Old 03-23-2012
Couple of things that come to mind
  1. The parameters required for the perl timelocal function seem to be reversed (Time::Local - perldoc.perl.org)
  2. The timelocal function uses months 0-11 instead of 1-12
  3. Using ls -l like that is unreliable unless you control the format. Also, some ls switch to another format after 6 months and/or a year
  4. You need the echo $* because only one parameter gets past to the function instead of 6 (lose the double quotes around the parameters to make it 6)
  5. use set -- instead of set -
# 6  
Old 03-26-2012
Hi Scrutinizer, thanks for your advise. I amended the script but its still not working

Code:
#!/bin/bash
# YYYY MM DD HH MM SS
# perl autosplits the string and uses timelocal to return
# the number of seconds from the Epoch
# No error checking!
function seconds_from_epoch {
echo $*| perl -MTime::Local -ane '
my $epochseconds = timegm($F[5], $F[4], $F[3], $F[2], $F[1], $F[0]);
print "$epochseconds\n"; '
}

# parse file s32adm_bash_history date and time
set -- $(ls -l s12adm_history)
fdate=$6
ftime=$7

# parse the year, month, and day
set -- $(IFS="-"; echo $fdate)
fyear=$0
fmonth=$1
fday=$2

# parse the hours and minutes
set -- $(IFS=":"; echo $ftime)
fhour=$3
fmin=$4
fsecs=$5

totsecs=$($fsecs $fmin $fhour $fday $fmonth $fyear)
echo $totsecs

I am getting this error now

Code:
[root@H99A100 user_history]# ./test
./test: line 29: Mar: command not found

Sorry, I'm not really good with scripts Smilie
# 7  
Old 03-27-2012
What is the output of ls -l s12adm_history? It seems to me that "Mar" stands for "March", and the is hardly the input that you should feed into timegm or timelocal.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python conversion to epoch time

Hi. I have timestamps that I am trying to convert to epoch time. An example: I am trying to convert this to an epoch timestamp but have one little glitch. I have this: import time date_time = '' pattern = '' epoch = int(time.mktime(time.strptime(date_time, pattern))) print epoch... (1 Reply)
Discussion started by: treesloth
1 Replies

2. Shell Programming and Scripting

Different epoch conversion result for bash and csh users

Hi there I'm using this script to convert command line history with Epoch time stamp to human readable. While it works fine with users with /bin/csh shell, it fails to convert for users with /bin/bash shell. Why is this happening? I even changed and added * and after the # but it still didnt... (2 Replies)
Discussion started by: hedkandi
2 Replies

3. Shell Programming and Scripting

Epoch & Unix Timestamp Conversion Tools

Hi All, Please read the below data carefully. I need an unix command for converting unix timestamp to Epoch timestamp. I need to daily convert this today's unix(UTC) time to epoch time, so i am thinking to make a shellscript for this. Please help me for this by providing... (3 Replies)
Discussion started by: aish11
3 Replies

4. Shell Programming and Scripting

Date conversion from Standard/given format to seconds/epoch

I am trying get time difference of two dates in secs. Initially I want to convert a standard date format to epoch for two dates and then subtract the two epoch dates. Example : date -d "2007-09-01 17:30:40" '+%s' But this gives me below error date: illegal option -- d Usage: date OS: AIX... (6 Replies)
Discussion started by: bpaac
6 Replies

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

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

7. Shell Programming and Scripting

epoch conversion

I need to convert an epoch time from a file into a standard UTC time and output it in the same format but I'm not sure what's the best approach here's the input file and the bold part is what I need to convert. 1,1,"sys1",60,300000 2,"E:",286511144960 3,1251194521,"E:",0,0... (2 Replies)
Discussion started by: satchy321
2 Replies

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

9. Shell Programming and Scripting

conversion from EPOCH timestamp to local time zone

hello gurus, i want a perl/shell script which once invoked should convert a set of EPOCH timestamps to local time ( IST..i want) . how does it work ,i have an idea on that..but writing a perl/shell script for it is not possible for me...so i need help for the same. my exact requirement is... (2 Replies)
Discussion started by: abhijeetkul
2 Replies

10. Shell Programming and Scripting

Convert from standard epoch time from a shell script?

Is there an easy method to do an on the fly conversion of a standard epoch time (seconds from 1970) to more readable date format? Does Unix have anything built in to do this? (4 Replies)
Discussion started by: LordJezo
4 Replies
Login or Register to Ask a Question