need help in time manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help in time manipulation
# 1  
Old 03-10-2008
need help in time manipulation

i have a script that gives the last time the data is inserted in a file. i want to check the time difference b/w the sytem time and the time in file.

for e,g.

if script runs at 3.30.So $a=3.30
and the time in file is 3.00.So $time =3.00

then the time difference should be 30 min......


-------------------------------------------------

#!/bin/ksh

a=`date +"%H:%M"`
cd /ednadtu3/u01/pipe/logs

Time=`ls -ltr File1.log | tr -s " " | cut -d " " -f8`

count=`expr $a - $Time`
if [ $count -gt 30 ]
then
echo "it is not processing data"
else
echo "it is processing data"
fi
--------------------------------------------------------------

so here if the delay b/w the time is More than 30 minutes,so it means it is not processing data
plz help me in this

Last edited by ali560045; 03-10-2008 at 06:25 AM..
# 2  
Old 03-10-2008
Quote:
Originally Posted by ali560045
i have a script that gives the last time the data is inserted in a file. i want to check the time difference b/w the sytem time and the time in file.

for e,g.

if script runs at 3.30
and the time in file is 3.00

then the time difference should be 30 min......


-------------------------------------------------

#!/bin/ksh

a=`date +"%H:%M"`
cd /ednadtu3/u01/pipe/logs

Time=`ls -ltr File1.log | tr -s " " | cut -d " " -f8`

if [ $time < 30 ]
then
echo "it is not processing data"
else
echo "it is processing data"
fi
--------------------------------------------------------------

plz help me in this
Please clarify above DarkRed variables....
# 3  
Old 03-10-2008
Sorry....... i have made the chnages in the script.....

now can u tell me how to get the time diff b/w the $a and $Time.
# 4  
Old 03-10-2008
Use a perl function:
Code:
#!/bin/ksh
# filetimes compared with now 
filetimediff()
{
    perl  -e '
          use POSIX qw(strftime);
          
          $mtime = (stat $ARGV[0])[9];             
          $seconds1 = strftime "%s\n", localtime($mtime);
          $seconds = time;
          $seconds -= $seconds1;
          print "$seconds: ";         # total seconds comment out if not needed
          $hours = int $seconds / 3600 ;
          $minutes = int ($seconds - (3600 * $hours)) / 60;
          $seconds = $seconds % 60;
          # print HOURS:Minutes:Seconds,  comment out if not needed
          printf "%d:%02d:%02d\n", $hours, $minutes, $seconds; 
         ' $1
}
#usage example
dt=$(filetimediff  "$1")
echo "$dt"

# 5  
Old 03-10-2008
Here is a ksh function which converts a time to seconds passed since midnight. From there on it is simply a matter of integer arithmetic.

Code:
# ------------------------------------------------------------------------------
# f_ConvertTime                        converts timestrings to seconds since MN
# ------------------------------------------------------------------------------
# Author.....: Wolf Machowitsch
# last update: 2005 05 15    by: Wolf Machowitsch
# ------------------------------------------------------------------------------
# Revision Log:
# - 0.99   2005 05 15   Original Creation
#                       base functionality, correction of incomplete
#                       timestrings like ":15" or "14"
#
# - 1.00   2007 11 20   Production release
#                       it was possible to pass nonsense data without
#                       f_ConvertTime() returning ERRLVL 1. This is
#                       now fixed. "f_ConvertTime aksj:00" will give
#                       back 1 as return value.
#
# ------------------------------------------------------------------------------
# Usage:
#     f_ConvertTime <char> timestring -> <int> seconds
#
#     Example: iSecondsSinceMidnight=$(f_ConvertTime "10:00:00")
#              print - $iSecondsSinceMidnight       # yields "36000"
#
# Prerequisites:
#
# ------------------------------------------------------------------------------
# Documentation:
#     f_ConvertTime() gets a timestring in $1 and prints the number of
#     seconds elapsed since midnight up to this time to <stdout>.
#     "Timestring" is any string of up to 3 digit groups delimited by
#     a non-digit separator. Digit groups amounting to "0" can be omitted
#     as can trailing separators, therefore "10:00:00", "10:00" and "10"
#     are treated the same, as well as "10-00.00", etc..
#
#     "00:10:01" can also be written ":00:01" and "10::10" and "10:00:10"
#     will both result in 36010.
#
#     Parameters: char Timestring
#     returns:    0 = no error
#                 1 = parameter error, minutes > 60, etc.
#                >1 = miscellaneous error
#                 Result is printed to <stdout>
#
# ------------------------------------------------------------------------------
# known bugs:
#
#     none
# ------------------------------------------------------------------------------
# ......................(C) 2005 Wolf Machowitsch ..............................
# ------------------------------------------------------------------------------

f_ConvertTime ()
{

typeset -i iRetVal=0
typeset    chTime="$1"
typeset -i iHours=0
typeset -i iMinutes=0
typeset -i iSeconds=0
typeset -i iTimeSecs=0

$chFullDebug
                                                 # correct timestring
chTime="$(print - "$chTime" | sed 's/[^0-9]/:/g')" # 14.15.00 -> 14:15:00
chTime="00${chTime}:00:00"                       # 14 -> 14:00:00
                                                 # :15 -> 00:15:00:00
if [ "$(print - $chTime | cut -d':' -f1)" != "" ] ; then
     iHours=$(print - $chTime | cut -d':' -f1)
     if [ $iHours -lt 0 -o $iHours -gt 23 ] ; then
          iRetVal=1
     fi
else
     iRetVal=1
fi
if [ "$(print - $chTime | cut -d':' -f2)" != "" ] ; then
     iMinutes=$(print - $chTime | cut -d':' -f2)
     if [ $iMinutes -lt 0 -o $iMinutes -gt 59 ] ; then
          iRetVal=1
     fi
else
     iRetVal=1
fi
if [ "$(print - $chTime | cut -d':' -f3)" != "" ] ; then
     iSeconds=$(print - $chTime | cut -d':' -f3)
     if [ $iSeconds -lt 0 -o $iSeconds -gt 59 ] ; then
          iRetVal=1
     fi
else
     iRetVal=1
fi
(( iTimeSecs = iSeconds + iMinutes * 60 + iHours * 3600 ))

print - $iTimeSecs

return $iRetVal
}

# --- EOF f_ConvertTime

I hope this helps.

bakunin
# 6  
Old 03-11-2008
thanks i got it now..............thank you very much for ur help. i really appreciate it
# 7  
Old 03-11-2008
Quick Python Example

Here's a quick-and-dirty Python version. Obviously you wouldn't hard-code the filename, but here it is.

Code:
 $ cat timeDiff.py 
#!/usr/bin/env python


import time #time functions
import os #os-specific functions

system_time = time.time()

file = "temp.txt"
    
if os.path.isfile(file):

    last_modified = os.path.getmtime(file)
        
timeDiff = (system_time - last_modified) / 60
print "System time: %s. Last modified, %s. %0.2f minutes." % (system_time, last_modified, timeDiff)

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate Time diff in milli milliseconds(Time format : HH:MM:SS,NNN)

Hi All, I have one file which contains time for request and response. I want to calculate time difference in milliseconds for each line. This file can contain 10K lines. Sample file with 4 lines. for first line. Request Time: 15:23:45,255 Response Time: 15:23:45,258 Time diff... (6 Replies)
Discussion started by: Raza Ali
6 Replies

2. Programming

Find gaps in time data and replace missing time value and column 2 value by interpolation in awk

Dear all, I am kindly seeking assistance on the following issue. I am working with data that is sampled every 0.05 hours (that is 3 minutes intervals) here is a sample data from the file 5.00000 15.5030 5.05000 15.6680 5.10000 16.0100 5.15000 16.3450 5.20000 16.7120 5.25000... (4 Replies)
Discussion started by: malandisa
4 Replies

3. Shell Programming and Scripting

Convert UTC time into current UNIX sever time zone

Hi guys thanks for the help for my previous posts.Now i have a requirement that i download a XMl file which has UTC time stamp.I need to convert UTC time into Unix server timezone. For ex if the time zone of unix server is CDT then i need to convert into CDT.whatever may be the system time... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

4. Solaris

modifying date and time and time zone on solaris 5.10 with (redundant server) veritas

I have a cluster of two Solaris server (veritas cluster). one working and the other is standby I am going to change the date on them , and am looking for a secure solution as it is giving an important service. my opinion is that the active one doesn't need to be restarted (if I don't change the... (1 Reply)
Discussion started by: barry1946
1 Replies

5. Shell Programming and Scripting

Time Manipulation in shell script

Hi all, I have a script that requires time comparisons and sending out an email alert only if the specified interval has been completed. The script runs in Cron tab every 5 mins. For ex: If the interval is set to 2 hrs (Dynamic & varies ) My script should execute and if it finds any error... (1 Reply)
Discussion started by: praseecg
1 Replies

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

7. Shell Programming and Scripting

Files manipulation with time comparison

Hi guys - I am new to Unix and learning some basics. I have to create a report of files that are in a specific directory and I have to list filenames with specific titles. This report will be created everyday early in the morning, say at 05:00 AM. (see output file format below) The 2 categories... (2 Replies)
Discussion started by: sksahu
2 Replies

8. UNIX for Advanced & Expert Users

How To Provide Time Sync Using Nts-150 Time Server On Unix Network?

can anybody tel lme,how to instal NTS -150 on a unix network,it needs some patch to fetch time frm serve,,?? (2 Replies)
Discussion started by: pesty
2 Replies

9. Programming

Time/date manipulation

hey folks, been awhile (actaully a long while) since i last touched C. And the 3 books i've read don't really have much about using time.h Question: How would i be able to assign a variable the value of the current date minus 2 mths, keeping in mind the yr. IE. would like to see Nov.31/2001... (1 Reply)
Discussion started by: choice
1 Replies
Login or Register to Ask a Question