Date conversion from Standard/given format to seconds/epoch


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date conversion from Standard/given format to seconds/epoch
# 1  
Old 07-15-2011
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 :
Code:
date -d "2007-09-01 17:30:40" '+%s'

But this gives me below error
Code:
date: illegal option -- d
Usage: date [-u] [+Field Descriptors]

OS: AIX 5.3.0.0

Thanks in advance

Last edited by Franklin52; 07-15-2011 at 04:12 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-15-2011
I got this in Bash on linux:

Code:
echo "$(date +'%s' -d 2007-09-02)/86400"-"$(date +'%s' -d 2007-09-01)"/86400 | bc

1

Seconds... I don't know... but I enjoyed the exercise.

more accurately (using bash)

Code:
echo $(date --date='2007-09-01 17:30:00' +%s)/60-$(date --date='2007-09-01 18:30:00' +%s)/60 | bc

more elegant or appropriate solutions may also be offered.

Last edited by Habitual; 07-16-2011 at 10:34 AM..
# 3  
Old 07-18-2011
Sorry for late reply.

When I execute on my bash I get below errors
Code:
echo "$(date +'%s' -d 2007-09-02)/86400"-"$(date +'%s' -d 2007-09-01)"/86400 | bc
Invalid character in date/time specification.
Usage: date [-u] [+Field Descriptors]
Invalid character in date/time specification.
Usage: date [-u] [+Field Descriptors]
syntax error on line 1 stdin

can you help me on this? Is it something to do with my bash?

Last edited by Scott; 07-18-2011 at 12:54 PM.. Reason: Code tags, please...
# 4  
Old 07-18-2011
Not all date command offers you all options.

You may have to use some script ( probably perl script ) to do the conversion ....

Code:
use DateTime;
$dt = DateTime->new( year => 1974, month => 11, day => 30, hour => 13, minute => 30,      second => 0, nanosecond => 500000000, time_zone => 'Asia/Taipei' ); 
$epoch_time  = $dt->epoch;

Refer Perl Epoch Converter Routines
# 5  
Old 07-18-2011
If you have possible to use ksh93, then builtin printf using:
Code:
epoc=$(printf "%(%s)T" "2010-10-24 00:00:00")

Or using bash, ksh, dash, ... something like
Code:
oifs="$IFS"  
JulianDate() 
{  
IFS="-"  
array=($1)  
IFS="$oifs"  
scale=0  
d=${array[2]}  
m=${array[1]}  
y=${array[0]} 
echo $((d-32075+1461*(y+4800+(m-14)/12)/4+367*(m-2-(m-14)/12*12)/12-3*((y+4900+(y-14)/12)/100)/4 )) 
}  

fromdate=$( JulianDate 2011-04-01 ) 
# ISO yyyy-mm-dd 
todate=$( JulianDate 2011-05-04 )  
diff=$((todate - fromdate)) 
echo $diff

# 6  
Old 07-18-2011
# 7  
Old 07-18-2011
ksh93 version in AIX 5.3.0.0 is M-12/28/93 and dosn't support %T with printf

You could use a simple perl script:
Code:
$ perl -e 'use Time::Local; print timelocal(0,0,0,$ARGV[2], $ARGV[1]-1, $ARGV[0]);' 2007 09 01
1188568800


Last edited by Chubler_XL; 07-18-2011 at 10:42 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a future date into epoch seconds on HPUX system

Hi All, I have scenario where i have to compare two dates. I thought of converting them to epoch seconds and do a numeric comparison. This works fine on Linux systems. $ date -d '2015/12/31' +%s 1451538000 $ date +%s 1449159121 But we don't have -d option in HPUX. What would be... (5 Replies)
Discussion started by: veeresh_15
5 Replies

2. Shell Programming and Scripting

Regex match date and seconds format

Hi $ awk '{print $1," ",$4}' access.log | sort | uniq -c| sort -nr | head -n20 62 192.168.10.6 How can get the result like 62, 192.168.10.6, 14:40 62, 192.168.10.32, 47:57 I tried modifying - $ awk '{print $1," ",$4}' access.log | sort | uniq -c| sort -nr | head -n20 | awk... (3 Replies)
Discussion started by: ashokvpp
3 Replies

3. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

4. Shell Programming and Scripting

Date format in micro seconds

Can i get date format in micro seconds in unix example 2012-01-27- 12.22.04.568722 Any help is appreciable (2 Replies)
Discussion started by: srichunduru
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

Date format conversion

Hi All, Can someone please let me know how can i convert the date format in unix as follow: From: 24 Oct 2011 i.e $(date +'%d %b %Y') To: 111024 i.e $(date +%y%m%d) Thanks in advance (3 Replies)
Discussion started by: davidtd
3 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

Need to convert an epoch date to MMDDYYHHmm format

System: HP-UX Kornshell Perl is installed, but not POSIX Hello, I am calculating a future date/time. To do this I take the system date in epoch format and add to it. I now need to take the new epoch date and convert it to MMDDYYHHmm format. Any help with this is greatly appreciated. (4 Replies)
Discussion started by: LetsGoPens
4 Replies

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

10. 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
Login or Register to Ask a Question