Let's say I have a variable $numsec which contains a number of seconds e.g. 62345
Now I want to convert this number into a more human readable form with hours,
minutes and seconds e.g.
17h23m24s
How can I do this in a shell script?
Is there really no other way but:
hour=$(($numsec/3600))
minute=$(($numsec/60-60*$hour))
sec=$(($numsec-3600*$hour-60*$minute))
echo $hour'h'$minute'h'$sec's'
I could imagine that there is a conversion function which simulates the "date" output:
echo `converttodate($numsec)` +%Hh%Mm%Ss
Matthew
|
|
0
|
|
|
|
Reply
|
kmlincoln100
|
6/2/2008 6:22:48 PM |
|
Matthew Lincoln schreef:
> Let's say I have a variable $numsec which contains a number of seconds e.g. 62345
> Now I want to convert this number into a more human readable form with hours,
> minutes and seconds e.g.
>
> 17h23m24s
>
> How can I do this in a shell script?
>
> Is there really no other way but:
>
> hour=$(($numsec/3600))
> minute=$(($numsec/60-60*$hour))
> sec=$(($numsec-3600*$hour-60*$minute))
> echo $hour'h'$minute'h'$sec's'
>
> I could imagine that there is a conversion function which simulates the "date" output:
>
> echo `converttodate($numsec)` +%Hh%Mm%Ss
>
> Matthew
$ x=62345
$ date -d "1970-01-01 UTC +$((x-3600)) seconds" +"%H:%M%S"
17:19:05
$
--
Luuk
|
|
0
|
|
|
|
Reply
|
luuk (828)
|
6/2/2008 7:02:21 PM
|
|
In article <48443a77$0$7543$9b4e6d93@newsspool1.arcor-online.net>,
Matthew Lincoln wrote:
> Is there really no other way but:
>
> hour=$(($numsec/3600))
> minute=$(($numsec/60-60*$hour))
> sec=$(($numsec-3600*$hour-60*$minute))
> echo $hour'h'$minute'h'$sec's'
>
> I could imagine that there is a conversion function
> which simulates the "date" output:
date(1) is an external command. I suppose you
could use one for your conversion too:
#include <stdlib.h>
int
main(int ac, char **av)
{ div_t d;
int h,m,s = atoi(av[1]);
d = div(s,60); m = d.quot; s = d.rem;
d = div(m,60); h = d.quot; m = d.rem;
printf("%dh%02dm%02ds\n",h,m,s);
return 0;
}
Compile and install in ~/bin/
Of course, this would be less efficient than the
shell calculations above. Why not create a shell
function?
Regards,
Marcel
--
printf -v email $(echo \ 155 141 162 143 145 154 155 141 162 \
143 145 154 100 157 162 141 156 147 145 56 156 154 | tr \ \\)
# O Herr, lass Hirn vom Himmel fallen! #
|
|
0
|
|
|
|
Reply
|
we-love-all-spam (43)
|
6/2/2008 7:08:06 PM
|
|
On Mon, 02 Jun 2008 18:22:48 +0000, Matthew Lincoln wrote:
> Let's say I have a variable $numsec which contains a number of seconds
> e.g. 62345 Now I want to convert this number into a more human readable
> form with hours, minutes and seconds e.g.
>
> 17h23m24s
>
> How can I do this in a shell script?
>
> Is there really no other way but:
>
> hour=$(($numsec/3600))
> minute=$(($numsec/60-60*$hour))
> sec=$(($numsec-3600*$hour-60*$minute)) echo $hour'h'$minute'h'$sec's'
>
> I could imagine that there is a conversion function which simulates the
> "date" output:
>
> echo `converttodate($numsec)` +%Hh%Mm%Ss
>
> Matthew
http://stromberg.dnsalias.org/~strombrg/modunits.html
seki-strombrg> ~/public_html/modunits -t time -n 111 -d two-highest -c -u
unabbreviated -a
51 seconds, 1 minutes
|
|
0
|
|
|
|
Reply
|
dstromberglists (64)
|
6/2/2008 7:21:01 PM
|
|
On 06/02/2008 03:02 PM, Luuk wrote:
> Matthew Lincoln schreef:
>> Let's say I have a variable $numsec which contains a number of seconds
>> e.g. 62345
>> Now I want to convert this number into a more human readable form with
>> hours,
>> minutes and seconds e.g.
>>
>> 17h23m24s
>>
>> How can I do this in a shell script?
>>
>> Is there really no other way but:
>>
>> hour=$(($numsec/3600))
>> minute=$(($numsec/60-60*$hour))
>> sec=$(($numsec-3600*$hour-60*$minute))
>> echo $hour'h'$minute'h'$sec's'
>>
>> I could imagine that there is a conversion function which simulates
>> the "date" output:
>>
>> echo `converttodate($numsec)` +%Hh%Mm%Ss
>>
>> Matthew
>
> $ x=62345
> $ date -d "1970-01-01 UTC +$((x-3600)) seconds" +"%H:%M%S"
> 17:19:05
> $
$ x=62345
$ date -d "1970-01-01 UTC +$((x-3600)) seconds" +"%H:%M:%S"
11:19:05
Off by six hours (I'm in tz EDT).
Also try x > 86400
|
|
0
|
|
|
|
Reply
|
oneal (207)
|
6/2/2008 7:23:12 PM
|
|
Douglas O'Neal schreef:
> On 06/02/2008 03:02 PM, Luuk wrote:
>> Matthew Lincoln schreef:
>>> Let's say I have a variable $numsec which contains a number of
>>> seconds e.g. 62345
>>> Now I want to convert this number into a more human readable form
>>> with hours,
>>> minutes and seconds e.g.
>>>
>>> 17h23m24s
>>>
>>> How can I do this in a shell script?
>>>
>>> Is there really no other way but:
>>>
>>> hour=$(($numsec/3600))
>>> minute=$(($numsec/60-60*$hour))
>>> sec=$(($numsec-3600*$hour-60*$minute))
>>> echo $hour'h'$minute'h'$sec's'
>>>
>>> I could imagine that there is a conversion function which simulates
>>> the "date" output:
>>>
>>> echo `converttodate($numsec)` +%Hh%Mm%Ss
>>>
>>> Matthew
>>
>> $ x=62345
>> $ date -d "1970-01-01 UTC +$((x-3600)) seconds" +"%H:%M%S"
>> 17:19:05
>> $
>
> $ x=62345
> $ date -d "1970-01-01 UTC +$((x-3600)) seconds" +"%H:%M:%S"
> 11:19:05
> Off by six hours (I'm in tz EDT).
>
> Also try x > 86400
you're right about the time zone...
try: date -d "1970-01-01 +$((x)) seconds" +"%H:%M:%S"
x>86400: you did not give a 'solution' to this problem yourself.
with 'converttodate()' you are saying that the result should be a date?
or do you allow i.e. 26h23m24s as an answer?
--
Luuk
|
|
0
|
|
|
|
Reply
|
luuk (828)
|
6/2/2008 7:33:59 PM
|
|
In article <48443a77$0$7543$9b4e6d93@newsspool1.arcor-online.net>,
Matthew Lincoln <kmlincoln100@hotmail.com> wrote:
>Let's say I have a variable $numsec which contains a number of seconds e.g. 62345
>Now I want to convert this number into a more human readable form with hours,
>minutes and seconds e.g.
>
>17h23m24s
>
>How can I do this in a shell script?
>
>Is there really no other way but:
>
>hour=$(($numsec/3600))
>minute=$(($numsec/60-60*$hour))
>sec=$(($numsec-3600*$hour-60*$minute))
>echo $hour'h'$minute'h'$sec's'
>
>I could imagine that there is a conversion function which simulates the "date" output:
>
>echo `converttodate($numsec)` +%Hh%Mm%Ss
Not built in. Of course you can write one. Or go a bit overboard, and write
one that produces every sort of time-period-conversion You've Ever Needed, like
the ksh93 function you'll find here:
ftp://ftp.armory.com/pub/lib/ksh/timeperiod
.. timeperiod; secToPeriod -a 62345'
17h19m5s
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
|
|
0
|
|
|
|
Reply
|
spcecdt
|
6/2/2008 9:09:49 PM
|
|
Matthew Lincoln wrote:
> Let's say I have a variable $numsec which contains a number of seconds e.g. 62345
> Now I want to convert this number into a more human readable form with hours,
> minutes and seconds e.g.
>
> 17h23m24s
>
> How can I do this in a shell script?
Is using GNU awk an option...?
awk -v t=$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
Janis
>
> Is there really no other way but:
>
> hour=$(($numsec/3600))
> minute=$(($numsec/60-60*$hour))
> sec=$(($numsec-3600*$hour-60*$minute))
> echo $hour'h'$minute'h'$sec's'
>
> I could imagine that there is a conversion function which simulates the "date" output:
>
> echo `converttodate($numsec)` +%Hh%Mm%Ss
>
> Matthew
|
|
0
|
|
|
|
Reply
|
Janis
|
6/2/2008 9:44:42 PM
|
|
On Mon, 02 Jun 2008 15:22:48 -0300, Matthew Lincoln <kmlincoln100@hotmai=
l.com> wrote:
> Let's say I have a variable $numsec which contains a number of seconds=
e.g. 62345
> Now I want to convert this number into a more human readable form with=
hours,
> minutes and seconds e.g.
>
> 17h23m24s
>
> How can I do this in a shell script?
>
> Is there really no other way but:
>
> hour=3D$(($numsec/3600))
> minute=3D$(($numsec/60-60*$hour))
> sec=3D$(($numsec-3600*$hour-60*$minute))
> echo $hour'h'$minute'h'$sec's'
>
> I could imagine that there is a conversion function which simulates th=
e "date" output:
>
> echo `converttodate($numsec)` +%Hh%Mm%Ss
>
> Matthew
>
Possibly nothing new here:
$ cat s
s=3D$1
h=3D$((s/3600))
s=3D$((s-h*3600))
m=3D$((s/60))
s=3D$((s-m*60))
echo $h\h$m\m$s\s
echo ---
# Using command date:
date --utc -d "1970-01-01 UTC $1 sec" +"%Hh%Mm%Ss"
date --version|head -n1
$ ./s 62345
17h19m5s
---
17h19m05s
date (GNU coreutils) 6.9
$
|
|
0
|
|
|
|
Reply
|
invalid1660 (4)
|
6/2/2008 10:30:40 PM
|
|
Matthew Lincoln wrote:
> Let's say I have a variable $numsec which contains a number of seconds e.g. 62345
> Now I want to convert this number into a more human readable form with hours,
> minutes and seconds e.g.
>
> 17h23m24s
$ perl -MPOSIX -le'print strftime q[%-Hh%-Mm%-Ss], gmtime 62345'
17h19m5s
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|
|
0
|
|
|
|
Reply
|
someone4 (105)
|
6/2/2008 10:51:58 PM
|
|
On Jun 2, 11:44=A0pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
>
> Is using GNU awk an option...?
>
> =A0 =A0awk -v t=3D$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
>
One might have to set TZ=3DUTC in order to avoid potential time zone
issues.
$ awk 'BEGIN{print strftime("%Hh%Mm%Ss",62345)}'
18h19m05s
$ TZ=3DUTC awk 'BEGIN{print strftime("%Hh%Mm%Ss",62345)}'
17h19m05s
Hermann
|
|
0
|
|
|
|
Reply
|
Hermann
|
6/3/2008 6:51:56 AM
|
|
On Jun 3, 8:51=A0am, Hermann Peifer <pei...@gmx.net> wrote:
> On Jun 2, 11:44=A0pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
> > Is using GNU awk an option...?
>
> > =A0 =A0awk -v t=3D$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
I think it is not a possible option to do the trick.
(numsec are the seconds from 1970, with "%Hh%Mm%Ss" you are saying the
hour of the day '(numsecond/(3600*24) from 1st January 1970)')
|
|
0
|
|
|
|
Reply
|
Javi
|
6/3/2008 8:13:03 AM
|
|
On Monday 2 June 2008 23:44, Janis Papanagnou wrote:
> Matthew Lincoln wrote:
>> Let's say I have a variable $numsec which contains a number of seconds
>> e.g. 62345 Now I want to convert this number into a more human readable
>> form with hours, minutes and seconds e.g.
>>
>> 17h23m24s
>>
>> How can I do this in a shell script?
>
> Is using GNU awk an option...?
>
> awk -v t=$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
But what if $numsec > 86400?
TZ=UTC awk -v t=86500 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
00h01m40s
--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
|
|
0
|
|
|
|
Reply
|
pk
|
6/3/2008 8:21:23 AM
|
|
On Jun 3, 10:21 am, pk <p...@pk.invalid> wrote:
> On Monday 2 June 2008 23:44, Janis Papanagnou wrote:
>
> > Matthew Lincoln wrote:
> >> Let's say I have a variable $numsec which contains a number of seconds
> >> e.g. 62345 Now I want to convert this number into a more human readable
> >> form with hours, minutes and seconds e.g.
>
> >> 17h23m24s
>
> >> How can I do this in a shell script?
>
> > Is using GNU awk an option...?
>
> > awk -v t=$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
>
> But what if $numsec > 86400?
>
> TZ=UTC awk -v t=86500 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
> 00h01m40s
>
You are right: it only produces correct results for numsec values in
the range 0..86399
Hermann
|
|
0
|
|
|
|
Reply
|
peifer (24)
|
6/3/2008 8:38:55 AM
|
|
Javi wrote:
> On Jun 3, 8:51 am, Hermann Peifer <pei...@gmx.net> wrote:
>
>>On Jun 2, 11:44 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
>>wrote:
>>
>>>Is using GNU awk an option...?
>>
>>> awk -v t=$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
>
> I think it is not a possible option to do the trick.
>
> (numsec are the seconds from 1970, with "%Hh%Mm%Ss" you are saying the
> hour of the day '(numsecond/(3600*24) from 1st January 1970)')
No.
$ numsec=$( date +%s ) # seconds now since 1970-01-01
$ awk -v t=$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
10h50m47s
What exactly is your problem with the proposed solution?
Janis
|
|
0
|
|
|
|
Reply
|
Janis
|
6/3/2008 8:52:49 AM
|
|
pk wrote:
> On Monday 2 June 2008 23:44, Janis Papanagnou wrote:
>
>
>>Matthew Lincoln wrote:
>>
>>>Let's say I have a variable $numsec which contains a number of seconds
>>>e.g. 62345 Now I want to convert this number into a more human readable
>>>form with hours, minutes and seconds e.g.
>>>
>>>17h23m24s
>>>
>>>How can I do this in a shell script?
>>
>>Is using GNU awk an option...?
>>
>> awk -v t=$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
>
>
> But what if $numsec > 86400?
I suppose a "more human readable form" would be to add day/month/year
("%Y%m%d"). If that is inappropriate ignore the proposed solution.
Janis
>
> TZ=UTC awk -v t=86500 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
> 00h01m40s
>
|
|
0
|
|
|
|
Reply
|
janis_papanagnou (1038)
|
6/3/2008 8:57:15 AM
|
|
On Jun 2, 8:22=A0pm, kmlincoln...@hotmail.com (Matthew Lincoln) wrote:
> Let's say I have a variable $numsec which contains a number of seconds e.g=
.. 62345
> Now I want to convert this number into a more human readable form with hou=
rs,
> minutes and seconds e.g.
>
> 17h23m24s
>
> How can I do this in a shell script?
>
> Is there really no other way but:
>
> hour=3D$(($numsec/3600))
> minute=3D$(($numsec/60-60*$hour))
> sec=3D$(($numsec-3600*$hour-60*$minute))
> echo $hour'h'$minute'h'$sec's'
>
> I could imagine that there is a conversion function which simulates the "d=
ate" output:
>
> echo `converttodate($numsec)` +%Hh%Mm%Ss
>
> Matthew
With GNU date
date -d "Jan 1, 1970 +${nonsecs} sec" '+%Hh%Mm%Ss'
|
|
0
|
|
|
|
Reply
|
petrus.dreyer (11)
|
6/3/2008 11:15:40 AM
|
|
On Jun 2, 8:22=A0pm, kmlincoln...@hotmail.com (Matthew Lincoln) wrote:
> Let's say I have a variable $numsec which contains a number of seconds e.g=
.. 62345
> Now I want to convert this number into a more human readable form with hou=
rs,
> minutes and seconds e.g.
>
> 17h23m24s
>
> How can I do this in a shell script?
>
> Is there really no other way but:
>
> hour=3D$(($numsec/3600))
> minute=3D$(($numsec/60-60*$hour))
> sec=3D$(($numsec-3600*$hour-60*$minute))
> echo $hour'h'$minute'h'$sec's'
>
> I could imagine that there is a conversion function which simulates the "d=
ate" output:
>
> echo `converttodate($numsec)` +%Hh%Mm%Ss
>
> Matthew
With gnu date
date -d "Jan 1, 1970 +${nonsecs} sec" '+%Hh%Mm%Ss'
|
|
0
|
|
|
|
Reply
|
petrus.dreyer (11)
|
6/3/2008 11:19:47 AM
|
|
On 06/02/2008 06:51 PM, John W. Krahn wrote:
> Matthew Lincoln wrote:
>> Let's say I have a variable $numsec which contains a number of seconds
>> e.g. 62345
>> Now I want to convert this number into a more human readable form with
>> hours,
>> minutes and seconds e.g.
>>
>> 17h23m24s
>
> $ perl -MPOSIX -le'print strftime q[%-Hh%-Mm%-Ss], gmtime 62345'
> 17h19m5s
perl -MPOSIX -le'print strftime q[%-Hh%-Mm%-Ss], gmtime 86500'
0h1m40s
Doug
|
|
0
|
|
|
|
Reply
|
oneal (207)
|
6/3/2008 12:35:15 PM
|
|
On 06/02/2008 03:33 PM, Luuk wrote:
> Douglas O'Neal schreef:
>> On 06/02/2008 03:02 PM, Luuk wrote:
>>> Matthew Lincoln schreef:
>>>> Let's say I have a variable $numsec which contains a number of
>>>> seconds e.g. 62345
>>>> Now I want to convert this number into a more human readable form
>>>> with hours,
>>>> minutes and seconds e.g.
>>>>
>>>> 17h23m24s
>>>>
>>>> How can I do this in a shell script?
>>>>
>>>> Is there really no other way but:
>>>>
>>>> hour=$(($numsec/3600))
>>>> minute=$(($numsec/60-60*$hour))
>>>> sec=$(($numsec-3600*$hour-60*$minute))
>>>> echo $hour'h'$minute'h'$sec's'
>>>>
>>>> I could imagine that there is a conversion function which simulates
>>>> the "date" output:
>>>>
>>>> echo `converttodate($numsec)` +%Hh%Mm%Ss
>>>>
>>>> Matthew
>>>
>>> $ x=62345
>>> $ date -d "1970-01-01 UTC +$((x-3600)) seconds" +"%H:%M%S"
>>> 17:19:05
>>> $
>>
>> $ x=62345
>> $ date -d "1970-01-01 UTC +$((x-3600)) seconds" +"%H:%M:%S"
>> 11:19:05
>> Off by six hours (I'm in tz EDT).
>>
>> Also try x > 86400
>
> you're right about the time zone...
> try: date -d "1970-01-01 +$((x)) seconds" +"%H:%M:%S"
>
> x>86400: you did not give a 'solution' to this problem yourself.
> with 'converttodate()' you are saying that the result should be a date?
> or do you allow i.e. 26h23m24s as an answer?
I read the OP's post as wanting 26h23m24s as an answer. Unfortunately,
the date command renders this as 2h23m24s.
|
|
0
|
|
|
|
Reply
|
oneal (207)
|
6/3/2008 12:49:32 PM
|
|
On Jun 2, 8:22=A0pm, kmlincoln...@hotmail.com (Matthew Lincoln) wrote:
> Let's say I have a variable $numsec which contains a number of seconds e.g=
.. 62345
> Now I want to convert this number into a more human readable form with hou=
rs,
> minutes and seconds e.g.
>
> 17h23m24s
>
> How can I do this in a shell script?
>
> Is there really no other way but:
>
> hour=3D$(($numsec/3600))
> minute=3D$(($numsec/60-60*$hour))
> sec=3D$(($numsec-3600*$hour-60*$minute))
> echo $hour'h'$minute'h'$sec's'
>
> I could imagine that there is a conversion function which simulates the "d=
ate" output:
>
> echo `converttodate($numsec)` +%Hh%Mm%Ss
>
> Matthew
With gnu date
date -d "Jan 1, 1970 +${nonsecs} sec" '+%Hh%Mm%Ss'
|
|
0
|
|
|
|
Reply
|
petrus.dreyer (11)
|
6/3/2008 12:56:16 PM
|
|
Hermann Peifer <peifer@gmx.net> writes:
>On Jun 3, 10:21 am, pk <p...@pk.invalid> wrote:
>> On Monday 2 June 2008 23:44, Janis Papanagnou wrote:
>>
>> > Matthew Lincoln wrote:
>> >> Let's say I have a variable $numsec which contains a number of seconds
>> >> e.g. 62345 Now I want to convert this number into a more human readable
>> >> form with hours, minutes and seconds e.g.
>>
>> >> 17h23m24s
>>
>> >> How can I do this in a shell script?
>>
>> > Is using GNU awk an option...?
>>
>> > awk -v t=$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
>>
>> But what if $numsec > 86400?
>>
>> TZ=UTC awk -v t=86500 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
>> 00h01m40s
>>
>You are right: it only produces correct results for numsec values in
>the range 0..86399
Look, this "date" thing is a total kludge, trying to use "date" for a
prupose it was NOT designed for. This started because the OP did not want
to explicitly put into a script the the procedure for reducing seconds to
hours, minutes, seconds. I have no idea why not. His typing on this post
already took far far more time and effort than putting those lines into the
script. He seems enamoured of the idea that there should be a single simple
tool gfor any job his whims might come up with. Rather the Unix philosophy
is to make tools out of small general purpose things, which may not be
simple ( in terms of the number of lines used) but is clear and works.
Do NOT kludge "date" for this purpose. It will always cause you trouble
down the line.
|
|
0
|
|
|
|
Reply
|
unruh-spam (2581)
|
6/3/2008 5:57:37 PM
|
|
Janis Papanagnou <Janis_Papanagnou@hotmail.com> writes:
>Javi wrote:
>> On Jun 3, 8:51 am, Hermann Peifer <pei...@gmx.net> wrote:
>>
>>>On Jun 2, 11:44 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
>>>wrote:
>>>
>>>>Is using GNU awk an option...?
>>>
>>>> awk -v t=$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
>>
>> I think it is not a possible option to do the trick.
>>
>> (numsec are the seconds from 1970, with "%Hh%Mm%Ss" you are saying the
>> hour of the day '(numsecond/(3600*24) from 1st January 1970)')
>No.
>$ numsec=$( date +%s ) # seconds now since 1970-01-01
>$ awk -v t=$numsec 'BEGIN{print strftime("%Hh%Mm%Ss",t)}'
>10h50m47s
>What exactly is your problem with the proposed solution?
Perhaps that there are more than 10 hours, 50 min 47 seconds since Jan 1
1970?
The OP wanted to format an elapsed time into hours min sec. Your elapsed
time is just wrong if that is what he wanted to do.
>Janis
|
|
0
|
|
|
|
Reply
|
Unruh
|
6/3/2008 6:01:19 PM
|
|
On 2008-06-02, Matthew Lincoln <kmlincoln100@hotmail.com> wrote:
>
>
> Let's say I have a variable $numsec which contains a number of seconds
> e.g. 62345 Now I want to convert this number into a more human
> readable form with hours, minutes and seconds e.g.
>
> 17h23m24s
>
> How can I do this in a shell script?
>
> Is there really no other way but:
>
> hour=$(($numsec/3600))
> minute=$(($numsec/60-60*$hour))
> sec=$(($numsec-3600*$hour-60*$minute))
> echo $hour'h'$minute'h'$sec's'
>
> I could imagine that there is a conversion function which simulates
> the "date" output:
>
> echo `converttodate($numsec)` +%Hh%Mm%Ss
>
> Matthew
With the GNU date command, if the number of seconds is less than 86400,
you could do:
date -d "00:00:00 $numsec seconds" +%HhMm%Ss
|
|
0
|
|
|
|
Reply
|
Bill
|
6/3/2008 6:50:41 PM
|
|
The carbonbased lifeform Matthew Lincoln inspired comp.unix.shell with:
> Let's say I have a variable $numsec which contains a number of seconds e.g. 62345
> Now I want to convert this number into a more human readable form with hours,
> minutes and seconds e.g.
>
> 17h23m24s
>
> How can I do this in a shell script?
Python can at least calculate days and rest seconds from a lapsed time in
second-ticks.
If you don't mind calling Python in your script:
#v+
#!/usr/bin/python
from datetime import *
from os import *
d = timedelta(seconds=int(getenv("numsec")))
print "%sd%sh%sm%ss" % (d.days, d.seconds // 3600,
(d.seconds % 3600) // 60, (d.seconds % 3600) % 60)
#v-
$export numsec=1000000
$./timelaps.py
11d13h46m40s
Theo
--
theo at van-werkhoven.nl ICQ:277217131 SuSE Linux
linuxcounter.org: 99872 Jabber:muadib at jabber.xs4all.nl AMD XP3000+ 1024MB
"ik _heb_ niets tegen Microsoft, ik heb iets tegen
de uitwassen *van* Microsoft"
|
|
0
|
|
|
|
Reply
|
Theo
|
6/3/2008 10:04:46 PM
|
|
|
24 Replies
218 Views
(page loaded in 0.042 seconds)
Similiar Articles: DMS to DD - comp.sys.hp48How can I convert a Degree, Minute Second angle to a decimal degree on ... HMS\-> converts hour-minute-second format (HH ... Fast way to display number in exponential ... Excel time format conversion - comp.soft-sys.math.scilab ...Convert Seconds to a Time Format in Excel | Excel Semi-Pro Convert a large number of seconds to a hours:minute:seconds format using the TIME, INT and MOD functions. Convert getFullYear to 2-digit format - comp.lang.javascript ...3 Replies 175 Views (0.126 seconds) Tweet ... recognize the numeric and not string format ... Am using an A2k app in which I want to convert a list of numbers (1,2,3 ... How compare two dates or convert date to number - comp.lang.perl ...6 Replies 304 Views (0.166 seconds) Tweet ... Compare number to format - comp.databases.filemaker How ... Convert DATE to NUMBER - comp.lang.awk convert absolute time ... how to convert 1 to 000001? - comp.soft-sys.sashi guys, I need to convert number"1" into code"0000001" or ... but just display with excess zeros, just use FORMAT. ... 3 Replies 259 Views (0.102 seconds) Tweet How Convert TXT data format to MAT data format - comp.soft-sys ...(0.035 seconds) Tweet ... Netware. how to convert dat files into csv format. ... can export data through ODBC to CSV (or any number ... How Convert TXT data format to MAT data ... How to Change WORDDATE. format character date to Numeric DDMMYY10 ...1 Replies 439 Views (0.164 seconds) Tweet ... SAS - Convert YYYYMMDD to Julian7 format - comp.soft-sys.sas ..... for converting YYYYMMDD to ... Convert UTC seconds to actual time. - comp.lang.cSo you have 10 lots of four years, plus a number of seconds remaining ... Convert UTC seconds to actual time. - comp.lang.c... seconds format into decimal ... Convert ... seconds to YYYYMMDDHHMMSS - comp.unix.programmerHi folks, i am in need to convert the number of seconds since 01/01/1970 UTC to a human ... If you need exactly the format in $subject use strftime(3) instead of ... How to convert characters to number - comp.lang.fortranSince it isn't wide enough to reach the second is 0.0. ... What does your compiler do with 2F15.6 format, two ... 000001? - comp.soft-sys.sas hi guys, I need to convert number"1 ... Degrees, Minutes, Seconds on the 49G+ - comp.sys.hp48To multiply deg Min sec by a real number, convert the measurement to decimal degrees ... be able to add, subtract and multiply > angles in Degrees,Minutes, Seconds format ... two's complement 8 bit data to decimal - comp.soft-sys.matlab ...Hi guys, does anyone knows how to convert a 8-bit data in the two's complement format back to signed decimal number? ... 2 Replies 673 Views (0.551 seconds) Tweet How to convert bytearray into integer? - comp.lang.python ...... is that struct may need to parse the format ... 4 Replies 220 Views (0.108 seconds) Tweet ... Convert string list to number list - comp.soft-sys.math ... How to get a number with 2 decimal, whatever this number is ...Run a script to convert the number to text, format it the way you want ... whole lot simpler to just format the number, unless it is, in fact, text you want. I'll second ... SAS - Convert YYYYMMDD to Julian7 format - comp.soft-sys.sas ...1 Replies 566 Views (0.08 seconds) Tweet ... Convert YYYYMMDD to Julian7 format - comp.soft-sys.sas ..... need to convert a date format ... Know Dot Net - Convert Number of Seconds to 00:00:00 FormatThere may be a time in your career where you are given a number of seconds and need to convert it to Hour:Minute:Second format. This is a pretty common task, and ... How to Convert Seconds to Hours & Minutes | eHow.comDivide the number of seconds by 3,600 to convert them in the hour decimal form. ... the time is presented in the "hour:minute:second" format.... How to Convert Hours to ... 7/26/2012 4:31:48 AM
|