Korn Shell: Show elapsed time in a specific formatCommand to monitor elapsed time in background?Understanding...
Taking advantage when the HR forgets to communicate the rules
Why do Klingons use cloaking devices?
How can select a specific triangle in my Delaunay mesh?
What instances can be solved today by modern solvers (pure LP)?
Did Stalin kill all Soviet officers involved in the Winter War?
What do you call the angle of the direction of an airplane?
Why no parachutes in the Orion AA2 abort test?
Did William Shakespeare hide things in his writings?
LTSpice: how to setup sinusoidal or exponential voltage source?
In the Seventh Seal why does Death let the chess game happen?
What's the difference between a type and a kind?
Motorcyle Chain needs to be cleaned every time you lube it?
PhD: When to quit and move on?
Can a USB hub be used to access a drive from 2 devices?
How do I iterate equal values with the standard library?
Speeding up thousands of string parses
How did שְׁלֹמֹה (shlomo) become Solomon?
Was the 45.9°C temperature in France in June 2019 the highest ever recorded in France?
What is the highest level of accuracy in motion control a Victorian society could achieve?
What causes a fastener to lock?
Taking my Ph.D. advisor out for dinner after graduation
Are "confidant" and "confident" homophones?
How would a sea turtle end up on its back?
Isn't "Dave's protocol" good if only the database, and not the code, is leaked?
Korn Shell: Show elapsed time in a specific format
Command to monitor elapsed time in background?Understanding ps elapsed time format for long running processesksh :To store the output of a awk command in an arrayNeed to get the difference between two time in secondscomparing user given time for log file timeCalculate date difference between Last modified date of a file and NOW using shell scriptHow to avoid executing a command if a process doesn't existHow to get time in HHMM format few minutes hence the time now?Linux system time temporally jumpsCalculate Difference In Time Over Several Days
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
In a log file, I need to print the Elapsed time, in the following format:
"Process completed %s - Elapsed %s",
<time now in HH:MM:SS format>,
<difference from start date to end date in HH:MM:SS format>
Example:
Process completed 23:57:59 - Elapsed 103:22:59
How could I achieve this?
ksh time
add a comment |
In a log file, I need to print the Elapsed time, in the following format:
"Process completed %s - Elapsed %s",
<time now in HH:MM:SS format>,
<difference from start date to end date in HH:MM:SS format>
Example:
Process completed 23:57:59 - Elapsed 103:22:59
How could I achieve this?
ksh time
2
Where do you get the start and end dates?
– glenn jackman
Feb 12 '14 at 17:54
1
Which ksh? ksh88 or ksh93?
– Mikel
Feb 12 '14 at 18:25
The start date is the system date when the script starts. The end date is the system date when the script ends. I suppose I am using ksh93 (how can I verify that??).
– UltraCommit
Feb 13 '14 at 5:37
add a comment |
In a log file, I need to print the Elapsed time, in the following format:
"Process completed %s - Elapsed %s",
<time now in HH:MM:SS format>,
<difference from start date to end date in HH:MM:SS format>
Example:
Process completed 23:57:59 - Elapsed 103:22:59
How could I achieve this?
ksh time
In a log file, I need to print the Elapsed time, in the following format:
"Process completed %s - Elapsed %s",
<time now in HH:MM:SS format>,
<difference from start date to end date in HH:MM:SS format>
Example:
Process completed 23:57:59 - Elapsed 103:22:59
How could I achieve this?
ksh time
ksh time
edited Feb 12 '14 at 23:13
Gilles
562k134 gold badges1158 silver badges1667 bronze badges
562k134 gold badges1158 silver badges1667 bronze badges
asked Feb 12 '14 at 17:21
UltraCommitUltraCommit
1231 silver badge5 bronze badges
1231 silver badge5 bronze badges
2
Where do you get the start and end dates?
– glenn jackman
Feb 12 '14 at 17:54
1
Which ksh? ksh88 or ksh93?
– Mikel
Feb 12 '14 at 18:25
The start date is the system date when the script starts. The end date is the system date when the script ends. I suppose I am using ksh93 (how can I verify that??).
– UltraCommit
Feb 13 '14 at 5:37
add a comment |
2
Where do you get the start and end dates?
– glenn jackman
Feb 12 '14 at 17:54
1
Which ksh? ksh88 or ksh93?
– Mikel
Feb 12 '14 at 18:25
The start date is the system date when the script starts. The end date is the system date when the script ends. I suppose I am using ksh93 (how can I verify that??).
– UltraCommit
Feb 13 '14 at 5:37
2
2
Where do you get the start and end dates?
– glenn jackman
Feb 12 '14 at 17:54
Where do you get the start and end dates?
– glenn jackman
Feb 12 '14 at 17:54
1
1
Which ksh? ksh88 or ksh93?
– Mikel
Feb 12 '14 at 18:25
Which ksh? ksh88 or ksh93?
– Mikel
Feb 12 '14 at 18:25
The start date is the system date when the script starts. The end date is the system date when the script ends. I suppose I am using ksh93 (how can I verify that??).
– UltraCommit
Feb 13 '14 at 5:37
The start date is the system date when the script starts. The end date is the system date when the script ends. I suppose I am using ksh93 (how can I verify that??).
– UltraCommit
Feb 13 '14 at 5:37
add a comment |
4 Answers
4
active
oldest
votes
Ksh has a special parameter SECONDS which always contains the number of seconds since the epoch. Evaluating $SECONDS at the beginning and at the end of the job gives you the start and end times, and the difference is the elapsed time.
Unix time doesn't take leap seconds into account: a day in Unix time is always exactly 84000 seconds. Therefore time-of-day arithmetic on Unix time is easy.
start=$SECONDS
…
end=$SECONDS
elapsed=$((end - start))
printf 'Process completed %d:%02d:%02d - Elapsed %d:%02d:%02dn'
$((end / 3600)) $((end / 60 % 60)) $((end % 60))
$((elapsed / 3600)) $((elapsed / 60 % 60)) $((elapsed % 60))
Bash has a$SECONDSbuilt-in too, but it behaves differently from Ksh - it is the number of integer seconds elapsed since the current shell instance was invoked. But deltas would be similar, so this answer should work there as well.
– Amit Naidu
yesterday
add a comment |
Like this (Warning: those of a sensitive disposition look away now - this is old school ksh):
t1=`date '+%H:%M:%S'`
sleep 2
t2=`date '+%H:%M:%S'`
t1h=`expr $t1 : '(..):.*'`
t2h=`expr $t2 : '(..):.*'`
t1m=`expr $t1 : '..:(..).*'`
t2m=`expr $t2 : '..:(..).*'`
t1s=`expr $t1 : '..:..:(..)'`
t2s=`expr $t2 : '..:..:(..)'`
hdiff=`expr $t2h - $t1h`
mdiff=`expr $t2m - $t1m`
sdiff=`expr $t2s - $t1s`
if [ $tm1 -gt $tm2 ];then
hdiff=`expr $hdiff - 1`
mdiff=`expr $tm1 - $tm2`
fi
if [ $ts1 -gt $ts2 ];then
mdiff=`expr $mdiff - 1`
sdiff=`expr $sm1 - $sm2`
fi
if [ $hdiff -lt 10 ];then
hdiff="0$hdiff"
fi
if [ $mdiff -lt 10 ];then
mdiff="0$mdiff"
fi
if [ $sdiff -lt 10 ];then
sdiff="0$sdiff"
fi
echo "Elapsed time $hdiff:$mdiff:$sdiff"
add a comment |
Sorry this is for bash, didn't notice you wanted korn shell untill just now, I can't help with that, but it might give you the basic principle.
StartTime=$( date +"%s" )
# do some stuff
sleep 4
EndTime=$( date )
ElapsedSecs=$(( `date -d"$EndTime" +%s` - StartTime ))
secs=$(( ElapsedSecs % 60 ))
mins=$(( ( ElapsedSecs / 60 ) % 60 ))
hrs=$(( ElapsedSecs / 3600 ))
EndTimeFormated=$( date -d"$EndTime" +"%H:%M:%S" )
echo "Process completed $EndTimeFormated - Elapsed " `printf "%02d:%02d%02dn" $hrs $mins $secs`
add a comment |
The simplest and cleanest option is to use date:
date -ud "@$elapsed" "+Elapsed Time: %T"
Here, $elapsed represents the time delta in seconds.
You can avoid forking the external date process by sticking to shell built-ins [1][2]. The printf shell command has a %(...)T date/time format specifier that is often overlooked:
TZ=UTC0 printf "Elapsed Time: %(%T)Tn" $elapsed
The TZ=UTC0 and date -u set the timezone to UTC, which prevents any timezone/DST related ambiguity.
Since this question emphasizes formatting, these methods are much more convenient and far less error-prone than calculating date parts because you have the entire range of date format options available to you - no math needed. They work well for intervals of up to 24 hours, and other than the sub-second precision, should work in Bash v4.2+ as well.
Example:
Suppose you wanted to add nanoseconds as well:
$ start=$SECONDS
$ #wait or run process
$ date -ud "@$((SECONDS-start))" "+Elapsed Time: %H:%M:%S.%N"
Elapsed Time: 00:00:12.003247901
$ #Or, preferably
$ TZ=UTC0 printf "Elapsed Time: %(%H:%M:%S.%N)Tn" $((SECONDS-start))
Elapsed Time: 00:00:12.005056068
Bonus
You can abstract out your time measurement with aliases to make things easier. For example, we can emulate the Oracle TIMING command like this:
alias timingstart='start=$SECONDS'
alias timingstop='TZ=UTC0 printf "Elapsed Time: %(%T)Tn" $((SECONDS-start))'
So, now:
$ timingstart
$ sleep 2
$ timingstop
Elapsed Time: 00:00:02
[1] For the $SECONDS variable, see man ksh | less +/SECONDS
[2] For printf time formats, see man ksh | less +/date-format -j.9
New contributor
Amit Naidu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f114925%2fkorn-shell-show-elapsed-time-in-a-specific-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ksh has a special parameter SECONDS which always contains the number of seconds since the epoch. Evaluating $SECONDS at the beginning and at the end of the job gives you the start and end times, and the difference is the elapsed time.
Unix time doesn't take leap seconds into account: a day in Unix time is always exactly 84000 seconds. Therefore time-of-day arithmetic on Unix time is easy.
start=$SECONDS
…
end=$SECONDS
elapsed=$((end - start))
printf 'Process completed %d:%02d:%02d - Elapsed %d:%02d:%02dn'
$((end / 3600)) $((end / 60 % 60)) $((end % 60))
$((elapsed / 3600)) $((elapsed / 60 % 60)) $((elapsed % 60))
Bash has a$SECONDSbuilt-in too, but it behaves differently from Ksh - it is the number of integer seconds elapsed since the current shell instance was invoked. But deltas would be similar, so this answer should work there as well.
– Amit Naidu
yesterday
add a comment |
Ksh has a special parameter SECONDS which always contains the number of seconds since the epoch. Evaluating $SECONDS at the beginning and at the end of the job gives you the start and end times, and the difference is the elapsed time.
Unix time doesn't take leap seconds into account: a day in Unix time is always exactly 84000 seconds. Therefore time-of-day arithmetic on Unix time is easy.
start=$SECONDS
…
end=$SECONDS
elapsed=$((end - start))
printf 'Process completed %d:%02d:%02d - Elapsed %d:%02d:%02dn'
$((end / 3600)) $((end / 60 % 60)) $((end % 60))
$((elapsed / 3600)) $((elapsed / 60 % 60)) $((elapsed % 60))
Bash has a$SECONDSbuilt-in too, but it behaves differently from Ksh - it is the number of integer seconds elapsed since the current shell instance was invoked. But deltas would be similar, so this answer should work there as well.
– Amit Naidu
yesterday
add a comment |
Ksh has a special parameter SECONDS which always contains the number of seconds since the epoch. Evaluating $SECONDS at the beginning and at the end of the job gives you the start and end times, and the difference is the elapsed time.
Unix time doesn't take leap seconds into account: a day in Unix time is always exactly 84000 seconds. Therefore time-of-day arithmetic on Unix time is easy.
start=$SECONDS
…
end=$SECONDS
elapsed=$((end - start))
printf 'Process completed %d:%02d:%02d - Elapsed %d:%02d:%02dn'
$((end / 3600)) $((end / 60 % 60)) $((end % 60))
$((elapsed / 3600)) $((elapsed / 60 % 60)) $((elapsed % 60))
Ksh has a special parameter SECONDS which always contains the number of seconds since the epoch. Evaluating $SECONDS at the beginning and at the end of the job gives you the start and end times, and the difference is the elapsed time.
Unix time doesn't take leap seconds into account: a day in Unix time is always exactly 84000 seconds. Therefore time-of-day arithmetic on Unix time is easy.
start=$SECONDS
…
end=$SECONDS
elapsed=$((end - start))
printf 'Process completed %d:%02d:%02d - Elapsed %d:%02d:%02dn'
$((end / 3600)) $((end / 60 % 60)) $((end % 60))
$((elapsed / 3600)) $((elapsed / 60 % 60)) $((elapsed % 60))
answered Feb 13 '14 at 8:11
GillesGilles
562k134 gold badges1158 silver badges1667 bronze badges
562k134 gold badges1158 silver badges1667 bronze badges
Bash has a$SECONDSbuilt-in too, but it behaves differently from Ksh - it is the number of integer seconds elapsed since the current shell instance was invoked. But deltas would be similar, so this answer should work there as well.
– Amit Naidu
yesterday
add a comment |
Bash has a$SECONDSbuilt-in too, but it behaves differently from Ksh - it is the number of integer seconds elapsed since the current shell instance was invoked. But deltas would be similar, so this answer should work there as well.
– Amit Naidu
yesterday
Bash has a
$SECONDS built-in too, but it behaves differently from Ksh - it is the number of integer seconds elapsed since the current shell instance was invoked. But deltas would be similar, so this answer should work there as well.– Amit Naidu
yesterday
Bash has a
$SECONDS built-in too, but it behaves differently from Ksh - it is the number of integer seconds elapsed since the current shell instance was invoked. But deltas would be similar, so this answer should work there as well.– Amit Naidu
yesterday
add a comment |
Like this (Warning: those of a sensitive disposition look away now - this is old school ksh):
t1=`date '+%H:%M:%S'`
sleep 2
t2=`date '+%H:%M:%S'`
t1h=`expr $t1 : '(..):.*'`
t2h=`expr $t2 : '(..):.*'`
t1m=`expr $t1 : '..:(..).*'`
t2m=`expr $t2 : '..:(..).*'`
t1s=`expr $t1 : '..:..:(..)'`
t2s=`expr $t2 : '..:..:(..)'`
hdiff=`expr $t2h - $t1h`
mdiff=`expr $t2m - $t1m`
sdiff=`expr $t2s - $t1s`
if [ $tm1 -gt $tm2 ];then
hdiff=`expr $hdiff - 1`
mdiff=`expr $tm1 - $tm2`
fi
if [ $ts1 -gt $ts2 ];then
mdiff=`expr $mdiff - 1`
sdiff=`expr $sm1 - $sm2`
fi
if [ $hdiff -lt 10 ];then
hdiff="0$hdiff"
fi
if [ $mdiff -lt 10 ];then
mdiff="0$mdiff"
fi
if [ $sdiff -lt 10 ];then
sdiff="0$sdiff"
fi
echo "Elapsed time $hdiff:$mdiff:$sdiff"
add a comment |
Like this (Warning: those of a sensitive disposition look away now - this is old school ksh):
t1=`date '+%H:%M:%S'`
sleep 2
t2=`date '+%H:%M:%S'`
t1h=`expr $t1 : '(..):.*'`
t2h=`expr $t2 : '(..):.*'`
t1m=`expr $t1 : '..:(..).*'`
t2m=`expr $t2 : '..:(..).*'`
t1s=`expr $t1 : '..:..:(..)'`
t2s=`expr $t2 : '..:..:(..)'`
hdiff=`expr $t2h - $t1h`
mdiff=`expr $t2m - $t1m`
sdiff=`expr $t2s - $t1s`
if [ $tm1 -gt $tm2 ];then
hdiff=`expr $hdiff - 1`
mdiff=`expr $tm1 - $tm2`
fi
if [ $ts1 -gt $ts2 ];then
mdiff=`expr $mdiff - 1`
sdiff=`expr $sm1 - $sm2`
fi
if [ $hdiff -lt 10 ];then
hdiff="0$hdiff"
fi
if [ $mdiff -lt 10 ];then
mdiff="0$mdiff"
fi
if [ $sdiff -lt 10 ];then
sdiff="0$sdiff"
fi
echo "Elapsed time $hdiff:$mdiff:$sdiff"
add a comment |
Like this (Warning: those of a sensitive disposition look away now - this is old school ksh):
t1=`date '+%H:%M:%S'`
sleep 2
t2=`date '+%H:%M:%S'`
t1h=`expr $t1 : '(..):.*'`
t2h=`expr $t2 : '(..):.*'`
t1m=`expr $t1 : '..:(..).*'`
t2m=`expr $t2 : '..:(..).*'`
t1s=`expr $t1 : '..:..:(..)'`
t2s=`expr $t2 : '..:..:(..)'`
hdiff=`expr $t2h - $t1h`
mdiff=`expr $t2m - $t1m`
sdiff=`expr $t2s - $t1s`
if [ $tm1 -gt $tm2 ];then
hdiff=`expr $hdiff - 1`
mdiff=`expr $tm1 - $tm2`
fi
if [ $ts1 -gt $ts2 ];then
mdiff=`expr $mdiff - 1`
sdiff=`expr $sm1 - $sm2`
fi
if [ $hdiff -lt 10 ];then
hdiff="0$hdiff"
fi
if [ $mdiff -lt 10 ];then
mdiff="0$mdiff"
fi
if [ $sdiff -lt 10 ];then
sdiff="0$sdiff"
fi
echo "Elapsed time $hdiff:$mdiff:$sdiff"
Like this (Warning: those of a sensitive disposition look away now - this is old school ksh):
t1=`date '+%H:%M:%S'`
sleep 2
t2=`date '+%H:%M:%S'`
t1h=`expr $t1 : '(..):.*'`
t2h=`expr $t2 : '(..):.*'`
t1m=`expr $t1 : '..:(..).*'`
t2m=`expr $t2 : '..:(..).*'`
t1s=`expr $t1 : '..:..:(..)'`
t2s=`expr $t2 : '..:..:(..)'`
hdiff=`expr $t2h - $t1h`
mdiff=`expr $t2m - $t1m`
sdiff=`expr $t2s - $t1s`
if [ $tm1 -gt $tm2 ];then
hdiff=`expr $hdiff - 1`
mdiff=`expr $tm1 - $tm2`
fi
if [ $ts1 -gt $ts2 ];then
mdiff=`expr $mdiff - 1`
sdiff=`expr $sm1 - $sm2`
fi
if [ $hdiff -lt 10 ];then
hdiff="0$hdiff"
fi
if [ $mdiff -lt 10 ];then
mdiff="0$mdiff"
fi
if [ $sdiff -lt 10 ];then
sdiff="0$sdiff"
fi
echo "Elapsed time $hdiff:$mdiff:$sdiff"
answered Feb 12 '14 at 19:04
suspectussuspectus
3,0242 gold badges15 silver badges23 bronze badges
3,0242 gold badges15 silver badges23 bronze badges
add a comment |
add a comment |
Sorry this is for bash, didn't notice you wanted korn shell untill just now, I can't help with that, but it might give you the basic principle.
StartTime=$( date +"%s" )
# do some stuff
sleep 4
EndTime=$( date )
ElapsedSecs=$(( `date -d"$EndTime" +%s` - StartTime ))
secs=$(( ElapsedSecs % 60 ))
mins=$(( ( ElapsedSecs / 60 ) % 60 ))
hrs=$(( ElapsedSecs / 3600 ))
EndTimeFormated=$( date -d"$EndTime" +"%H:%M:%S" )
echo "Process completed $EndTimeFormated - Elapsed " `printf "%02d:%02d%02dn" $hrs $mins $secs`
add a comment |
Sorry this is for bash, didn't notice you wanted korn shell untill just now, I can't help with that, but it might give you the basic principle.
StartTime=$( date +"%s" )
# do some stuff
sleep 4
EndTime=$( date )
ElapsedSecs=$(( `date -d"$EndTime" +%s` - StartTime ))
secs=$(( ElapsedSecs % 60 ))
mins=$(( ( ElapsedSecs / 60 ) % 60 ))
hrs=$(( ElapsedSecs / 3600 ))
EndTimeFormated=$( date -d"$EndTime" +"%H:%M:%S" )
echo "Process completed $EndTimeFormated - Elapsed " `printf "%02d:%02d%02dn" $hrs $mins $secs`
add a comment |
Sorry this is for bash, didn't notice you wanted korn shell untill just now, I can't help with that, but it might give you the basic principle.
StartTime=$( date +"%s" )
# do some stuff
sleep 4
EndTime=$( date )
ElapsedSecs=$(( `date -d"$EndTime" +%s` - StartTime ))
secs=$(( ElapsedSecs % 60 ))
mins=$(( ( ElapsedSecs / 60 ) % 60 ))
hrs=$(( ElapsedSecs / 3600 ))
EndTimeFormated=$( date -d"$EndTime" +"%H:%M:%S" )
echo "Process completed $EndTimeFormated - Elapsed " `printf "%02d:%02d%02dn" $hrs $mins $secs`
Sorry this is for bash, didn't notice you wanted korn shell untill just now, I can't help with that, but it might give you the basic principle.
StartTime=$( date +"%s" )
# do some stuff
sleep 4
EndTime=$( date )
ElapsedSecs=$(( `date -d"$EndTime" +%s` - StartTime ))
secs=$(( ElapsedSecs % 60 ))
mins=$(( ( ElapsedSecs / 60 ) % 60 ))
hrs=$(( ElapsedSecs / 3600 ))
EndTimeFormated=$( date -d"$EndTime" +"%H:%M:%S" )
echo "Process completed $EndTimeFormated - Elapsed " `printf "%02d:%02d%02dn" $hrs $mins $secs`
edited Feb 12 '14 at 18:50
answered Feb 12 '14 at 18:06
X TianX Tian
8,0831 gold badge23 silver badges38 bronze badges
8,0831 gold badge23 silver badges38 bronze badges
add a comment |
add a comment |
The simplest and cleanest option is to use date:
date -ud "@$elapsed" "+Elapsed Time: %T"
Here, $elapsed represents the time delta in seconds.
You can avoid forking the external date process by sticking to shell built-ins [1][2]. The printf shell command has a %(...)T date/time format specifier that is often overlooked:
TZ=UTC0 printf "Elapsed Time: %(%T)Tn" $elapsed
The TZ=UTC0 and date -u set the timezone to UTC, which prevents any timezone/DST related ambiguity.
Since this question emphasizes formatting, these methods are much more convenient and far less error-prone than calculating date parts because you have the entire range of date format options available to you - no math needed. They work well for intervals of up to 24 hours, and other than the sub-second precision, should work in Bash v4.2+ as well.
Example:
Suppose you wanted to add nanoseconds as well:
$ start=$SECONDS
$ #wait or run process
$ date -ud "@$((SECONDS-start))" "+Elapsed Time: %H:%M:%S.%N"
Elapsed Time: 00:00:12.003247901
$ #Or, preferably
$ TZ=UTC0 printf "Elapsed Time: %(%H:%M:%S.%N)Tn" $((SECONDS-start))
Elapsed Time: 00:00:12.005056068
Bonus
You can abstract out your time measurement with aliases to make things easier. For example, we can emulate the Oracle TIMING command like this:
alias timingstart='start=$SECONDS'
alias timingstop='TZ=UTC0 printf "Elapsed Time: %(%T)Tn" $((SECONDS-start))'
So, now:
$ timingstart
$ sleep 2
$ timingstop
Elapsed Time: 00:00:02
[1] For the $SECONDS variable, see man ksh | less +/SECONDS
[2] For printf time formats, see man ksh | less +/date-format -j.9
New contributor
Amit Naidu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
The simplest and cleanest option is to use date:
date -ud "@$elapsed" "+Elapsed Time: %T"
Here, $elapsed represents the time delta in seconds.
You can avoid forking the external date process by sticking to shell built-ins [1][2]. The printf shell command has a %(...)T date/time format specifier that is often overlooked:
TZ=UTC0 printf "Elapsed Time: %(%T)Tn" $elapsed
The TZ=UTC0 and date -u set the timezone to UTC, which prevents any timezone/DST related ambiguity.
Since this question emphasizes formatting, these methods are much more convenient and far less error-prone than calculating date parts because you have the entire range of date format options available to you - no math needed. They work well for intervals of up to 24 hours, and other than the sub-second precision, should work in Bash v4.2+ as well.
Example:
Suppose you wanted to add nanoseconds as well:
$ start=$SECONDS
$ #wait or run process
$ date -ud "@$((SECONDS-start))" "+Elapsed Time: %H:%M:%S.%N"
Elapsed Time: 00:00:12.003247901
$ #Or, preferably
$ TZ=UTC0 printf "Elapsed Time: %(%H:%M:%S.%N)Tn" $((SECONDS-start))
Elapsed Time: 00:00:12.005056068
Bonus
You can abstract out your time measurement with aliases to make things easier. For example, we can emulate the Oracle TIMING command like this:
alias timingstart='start=$SECONDS'
alias timingstop='TZ=UTC0 printf "Elapsed Time: %(%T)Tn" $((SECONDS-start))'
So, now:
$ timingstart
$ sleep 2
$ timingstop
Elapsed Time: 00:00:02
[1] For the $SECONDS variable, see man ksh | less +/SECONDS
[2] For printf time formats, see man ksh | less +/date-format -j.9
New contributor
Amit Naidu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
The simplest and cleanest option is to use date:
date -ud "@$elapsed" "+Elapsed Time: %T"
Here, $elapsed represents the time delta in seconds.
You can avoid forking the external date process by sticking to shell built-ins [1][2]. The printf shell command has a %(...)T date/time format specifier that is often overlooked:
TZ=UTC0 printf "Elapsed Time: %(%T)Tn" $elapsed
The TZ=UTC0 and date -u set the timezone to UTC, which prevents any timezone/DST related ambiguity.
Since this question emphasizes formatting, these methods are much more convenient and far less error-prone than calculating date parts because you have the entire range of date format options available to you - no math needed. They work well for intervals of up to 24 hours, and other than the sub-second precision, should work in Bash v4.2+ as well.
Example:
Suppose you wanted to add nanoseconds as well:
$ start=$SECONDS
$ #wait or run process
$ date -ud "@$((SECONDS-start))" "+Elapsed Time: %H:%M:%S.%N"
Elapsed Time: 00:00:12.003247901
$ #Or, preferably
$ TZ=UTC0 printf "Elapsed Time: %(%H:%M:%S.%N)Tn" $((SECONDS-start))
Elapsed Time: 00:00:12.005056068
Bonus
You can abstract out your time measurement with aliases to make things easier. For example, we can emulate the Oracle TIMING command like this:
alias timingstart='start=$SECONDS'
alias timingstop='TZ=UTC0 printf "Elapsed Time: %(%T)Tn" $((SECONDS-start))'
So, now:
$ timingstart
$ sleep 2
$ timingstop
Elapsed Time: 00:00:02
[1] For the $SECONDS variable, see man ksh | less +/SECONDS
[2] For printf time formats, see man ksh | less +/date-format -j.9
New contributor
Amit Naidu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
The simplest and cleanest option is to use date:
date -ud "@$elapsed" "+Elapsed Time: %T"
Here, $elapsed represents the time delta in seconds.
You can avoid forking the external date process by sticking to shell built-ins [1][2]. The printf shell command has a %(...)T date/time format specifier that is often overlooked:
TZ=UTC0 printf "Elapsed Time: %(%T)Tn" $elapsed
The TZ=UTC0 and date -u set the timezone to UTC, which prevents any timezone/DST related ambiguity.
Since this question emphasizes formatting, these methods are much more convenient and far less error-prone than calculating date parts because you have the entire range of date format options available to you - no math needed. They work well for intervals of up to 24 hours, and other than the sub-second precision, should work in Bash v4.2+ as well.
Example:
Suppose you wanted to add nanoseconds as well:
$ start=$SECONDS
$ #wait or run process
$ date -ud "@$((SECONDS-start))" "+Elapsed Time: %H:%M:%S.%N"
Elapsed Time: 00:00:12.003247901
$ #Or, preferably
$ TZ=UTC0 printf "Elapsed Time: %(%H:%M:%S.%N)Tn" $((SECONDS-start))
Elapsed Time: 00:00:12.005056068
Bonus
You can abstract out your time measurement with aliases to make things easier. For example, we can emulate the Oracle TIMING command like this:
alias timingstart='start=$SECONDS'
alias timingstop='TZ=UTC0 printf "Elapsed Time: %(%T)Tn" $((SECONDS-start))'
So, now:
$ timingstart
$ sleep 2
$ timingstop
Elapsed Time: 00:00:02
[1] For the $SECONDS variable, see man ksh | less +/SECONDS
[2] For printf time formats, see man ksh | less +/date-format -j.9
New contributor
Amit Naidu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Amit Naidu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 28 mins ago
Amit NaiduAmit Naidu
1011 bronze badge
1011 bronze badge
New contributor
Amit Naidu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Amit Naidu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f114925%2fkorn-shell-show-elapsed-time-in-a-specific-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Where do you get the start and end dates?
– glenn jackman
Feb 12 '14 at 17:54
1
Which ksh? ksh88 or ksh93?
– Mikel
Feb 12 '14 at 18:25
The start date is the system date when the script starts. The end date is the system date when the script ends. I suppose I am using ksh93 (how can I verify that??).
– UltraCommit
Feb 13 '14 at 5:37