Sponsored Content
Top Forums Shell Programming and Scripting need help in time manipulation Post 302174237 by bakunin on Monday 10th of March 2008 12:45:19 PM
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
 

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

9. 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
HOZ(1)								   User Commands							    HOZ(1)

NAME
hoz - file splitter that uses the hacha file format SYNOPSIS
hoz [-pvf] [-c size[K|M]] [-o outpath] inputfname ghoz DESCRIPTION
HOZ is a file splitter, which uses the same file format as the popular 'Hacha' program. OPTIONS
-c --cut cut (file->pieces) -p --paste paste (pieces->file) -v --verbose verbose output -f --force force overwrite of file when pasting -o --outpath specify an output directory -h --help print this help, then exit --version print hoz program version number, then exit USAGE
There are two basic operations: cut and paste. Cut will 'split' a file in pieces. The size of each piece is passed as an option. Each piece will have a numeric extension, starting with 0. So for instance if you 'cut' a file called 'foo.iso', the pieces will be named 'foo.iso.0', 'foo.iso.1' and so on. Paste will 'merge' these pieces and generate and exact copy of the original file. ghoz is the graphical version of hoz, which uses a GTK GUI interface. SEE ALSO
You can find more information at http://hoz.sourceforge.net/ hoz 1.65 August 2008 HOZ(1)
All times are GMT -4. The time now is 04:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy