COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to convert number of seconds to format "17h23m24s" ?

• Follow

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

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

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

```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"

\$ ./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

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

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

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

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

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

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

24 Replies
218 Views

Similiar Articles:

7/26/2012 4:31:48 AM