Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi All,
In a file i get the date as:
2009-08-29 23:10:05 CSTMy current date is:
Fri Aug 28 08:18:33 UTC 2009Now 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

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

![]() |
loss connection
|
Want to run a shell scrip...
|
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |