Computing.Net > Forums > Unix > conversion to seconds

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

conversion to seconds

Reply to Message Icon

Name: HemaV
Date: August 28, 2009 at 01:24:09 Pacific
OS: Windows XP
Subcategory: General
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: August 28, 2009 at 19:45:15 Pacific
Reply:

I am assuming you are asking for the number of seconds since the epoch date, Jan 1, 1970. This topic has been disucssed before:

http://www.computing.net/answers/un...

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:


#!/bin/ksh

# This function takes a date time string of the format:
# 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] - 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


0
Reply to Message Icon

Related Posts

See More


loss connection Want to run a shell scrip...


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: conversion to seconds

Date To Second Convert Vice Versa www.computing.net/answers/unix/date-to-second-convert-vice-versa/5262.html

Installing Linux to second hard drive www.computing.net/answers/unix/installing-linux-to-second-hard-drive/701.html

How to format difftime? www.computing.net/answers/unix/how-to-format-difftime/5503.html