Click here for important information about

conversion to seconds

August 28, 2009 at 01:24:09
Specs: Windows XP
Hi All,
In a file i get the date as:
2009-08-29 23:10:05 CST

My current date is:
Fri Aug 28 08:18:33 UTC 2009

Now i need to convert the file date and time to number of seconds and put a sleep command based on my current date.
Can anyone please let me know how to do this in korn shell script.

Thanks in Advance

See More: conversion to seconds

August 28, 2009 at 19:45:15
I am assuming you are asking for the number of seconds since the epoch date, Jan 1, 1970. This topic has been disucssed before:

The NON-Gunu (i.e. Non Linux) tools do not handle date arithmetic well. (The Linux date command's %s option returns the number of seconds since 1,1,1970).

I encorporated a small perl program into a shell function that returns the seconds from 1,1,1970 for a given date and time:


# This function takes a date time string of the format:
# 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] - 1, $F[0]);
print "$epochseconds\n"; '

filedate="2009-08-29 23:10:05 CST"

# parse filedate to get the date & time strings
set -- $(echo $filedate)
datestr=$1  # date
timestr=$2  # time

# parse the date string
set - $(IFS='-'; echo $datestr)
yr=$1 # year
mo=$2 # month
dd=$3 # day

# parse the time string
set - $(IFS=':'; echo $timestr)
hr=$1  # hour
mm=$2  # min
ss=$3  # seconds

# get the file date seconds
sfd=$(seconds_from_epoch "$yr $mo $dd $hr $mm $ss")
echo $sfd

Report •

January 7, 2010 at 08:34:27
When i tried the script i got the below error message :

syntax error at line 4 : `echo' unexpected

Report •

January 7, 2010 at 22:12:16
This program works fine on my Solaris 9 system. I don't know what to tell you. What OS are you using?

Report •
Related Solutions

Ask Question