[opensuse] Scripting Question - Converting value to minutes:seconds
For all you scripting gurus out there. Can you help me out? I'm trying to convert a value to an output to the user of minutes:seconds. For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes" How would I do this? -- ---Bryen--- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 19 November 2007 20:06:40 Bryen wrote:
For all you scripting gurus out there. Can you help me out?
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
I'm not sure if there is some utility for converting directly to a time (I know there is in C, but I don't know about the shell) But otherwise, you could do something like echo This is $((A/60)):$((A%60)) minutes Anders -- Madness takes its toll -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Mon, 19 Nov 2007, by suseROCKS@bryen.com:
For all you scripting gurus out there. Can you help me out?
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
$ A=100 $ python -c 'import time,sys; \ print "This is %d:%d minutes" % \ (time.localtime(float(sys.argv[2]))[4:6])' - $A This is 1:40 minutes Theo -- Theo v. Werkhoven Registered Linux user# 99872 http://counter.li.org ICBM 52 13 26N , 4 29 47E. + ICQ: 277217131 SUSE 10.2 + Jabber: muadib@jabber.xs4all.nl Kernel 2.6.20 + See headers for PGP/GPG info. Claimer: any email I receive will become my property. Disclaimers do not apply. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tue, 2007-11-20 at 00:03 +0100, Theo v. Werkhoven wrote:
Mon, 19 Nov 2007, by suseROCKS@bryen.com:
For all you scripting gurus out there. Can you help me out?
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
$ A=100 $ python -c 'import time,sys; \ print "This is %d:%d minutes" % \ (time.localtime(float(sys.argv[2]))[4:6])' - $A This is 1:40 minutes
I have no idea what I am seeing, but it was just sweet enough to work! THANKS! -- ---Bryen--- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Mon, 19 Nov 2007, by suseROCKS@bryen.com:
On Tue, 2007-11-20 at 00:03 +0100, Theo v. Werkhoven wrote:
Mon, 19 Nov 2007, by suseROCKS@bryen.com:
For all you scripting gurus out there. Can you help me out?
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
$ A=100 $ python -c 'import time,sys; \ print "This is %d:%d minutes" % \ (time.localtime(float(sys.argv[2]))[4:6])' - $A This is 1:40 minutes
I have no idea what I am seeing, but it was just sweet enough to work! THANKS!
Not so difficult. -c tells the python interpreter to run the command or script as supplied. import time,sys tells python to use these two modules There is a commend, pydoc, which can be used to see what a python module does, so: $ pydoc time Help on module time: NAME time - This module provides various functions to manipulate time values. FILE /usr/lib/python2.5/lib-dynload/time.so MODULE DOCS http://www.python.org/doc/current/lib/module-time.html DESCRIPTION [..] Functions: [..] localtime() -- convert seconds since Epoch to local time tuple [..] FUNCTIONS localtime(...) localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,\ tm_min,tm_sec,tm_wday,tm_yday,tm_isdst) $ pydoc sys Help on built-in module sys: NAME sys FILE (built-in) MODULE DOCS http://www.python.org/doc/current/lib/module-sys.html DESCRIPTION This module provides access to some objects used or maintained by the interpreter and to functions that interact strongly with the interpreter. Dynamic objects: argv -- command line arguments; argv[0] is the script pathname if known Thus the module time, called with function localtime(100), returns a list of values, of which tm_min (the 5th) and tm_sec (the 6th) are of interest. Because python counts from 0, and the intersection returns [from:to-1] the values in the list that you want are numbered [4:6]. localtime() wants a floating point number, thus the number from stdin is converted to float first. According to python's man-page, when python is called with -c and there are arguments after the commands, argv[0] holds '-c', argv[1] holds the first argument, argv[2] the 2nd etc. (My bad, the dash before $A was not needed afterall). This list (1,40) is fed to the printf style command, which prints the list as two seperate numbers. Python is a very usefull and powerfull language, with which you can do anything from these small oneliners to multi million Euro projects. The beauty of it is, that it's almost impossible to write unreadable code in Python. Theo -- Theo v. Werkhoven Registered Linux user# 99872 http://counter.li.org ICBM 52 13 26N , 4 29 47E. + ICQ: 277217131 SUSE 10.2 + Jabber: muadib@jabber.xs4all.nl Kernel 2.6.20 + See headers for PGP/GPG info. Claimer: any email I receive will become my property. Disclaimers do not apply. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
$ A=100 $ python -c 'import time,sys; \ print "This is %d:%d minutes" % \ (time.localtime(float(sys.argv[2]))[4:6])' - $A This is 1:40 minutes
# A=100; perl -e 'use POSIX;print "This is ", strftime "%M:%S", \ gmtime $ARGV[0] ,. " minutes\n";' $A This is 01:40 minutes extended: # A=3661; perl -e 'use POSIX;print "This is ", strftime "%H:%M:%S", \ gmtime $ARGV[0] ,. " minutes\n";' $A This is 01:01:01 minutes regards, Georg -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Bryen wrote:
For all you scripting gurus out there. Can you help me out?
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds)
A=100 notice this: $ echo $A $ $A=100 bash: =100: command not found $ A=DUMMY $ echo $A DUMMY $ $A=100 bash: DUMMY=100: command not found $
Then echo "This is 1:40 minutes"
How would I do this?
learn to read manual pages. man bash NUM1=((arithmetic_expression_here)) NUM2=((another_arithmetic_expression_here)) -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Bryen wrote:
For all you scripting gurus out there. Can you help me out?
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
If you want a pure shell scripting solution: -------------------------------------------- #!/bin/sh mysec=100 min=`expr $mysec / 60` sec=`expr 60 \* $min` sec=`expr $mysec - $sec` echo $min:$sec exit 0 -------------------------------------------- If you need more complicated math you can also try to employ bc (e.g. echo "100/60" | bc ; echo "100%60"| bc). See "man bc". Rgds, Stephan. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 20 November 2007 02:00:32 Stephan Hegel wrote:
sec=`expr 60 \* $min` sec=`expr $mysec - $sec`
As I already posted, these two lines are better done through modulus - mysec%60, but I guess Bryen was more interested in python and perl Anders -- Madness takes its toll -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tue, 2007-11-20 at 20:05 +0100, Anders Johansson wrote:
On Tuesday 20 November 2007 02:00:32 Stephan Hegel wrote:
sec=`expr 60 \* $min` sec=`expr $mysec - $sec`
As I already posted, these two lines are better done through modulus - mysec%60, but I guess Bryen was more interested in python and perl
Anders
Hey now... I appreciated everyone's suggestions, including yours, and I expresed my gratitude by inviting you all over for drinks. :-) ---Bryen--- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders Johansson wrote:
On Tuesday 20 November 2007 02:00:32 Stephan Hegel wrote:
sec=`expr 60 \* $min` sec=`expr $mysec - $sec`
As I already posted, these two lines are better done through modulus - mysec%60, Yes, you're right. I was just not aware that the modulus operation is available in the shell.
But you have to write it like that (with spaces before and after the % !): sec=`expr $mysec % 60` Otherwise the modulus operation isn't executed. Thanks for the hint, Stephan. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The Monday 2007-11-19 at 13:06 -0600, Bryen wrote:
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
cer@nimrodel:~> date --date="2007-1-1 100 minutes" Mon Jan 1 01:40:00 CET 2007 Maybe it can be refined, but you can see it does the conversion of 100 minutes. - -- Cheers, Carlos E. R. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.4-svn0 (GNU/Linux) Comment: Made with pgp4pine 1.76 iD8DBQFHQkk4tTMYHG2NR9URAsL1AJ9Zh9R1+hvqUv2RQBjQ8gv8xTLePwCggQbJ B28Yop1VECnErMIya4N+wtk= =wCNV -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tue, 2007-11-20 at 03:40 +0100, Carlos E. R. wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
The Monday 2007-11-19 at 13:06 -0600, Bryen wrote:
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
cer@nimrodel:~> date --date="2007-1-1 100 minutes" Mon Jan 1 01:40:00 CET 2007
Maybe it can be refined, but you can see it does the conversion of 100 minutes.
- -- Cheers, Carlos E. R.
That's an interesting variation of the date command. And I'm certainly going to keep that in mind should the need for that type of variation arise. But in my script case, I was looking to convert a value into minutes/seconds not relative to clock time.
---Bryen---
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 2007-11-19 21:59, Bryen wrote:
On Tue, 2007-11-20 at 03:40 +0100, Carlos E. R. wrote:
The Monday 2007-11-19 at 13:06 -0600, Bryen wrote:
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
cer@nimrodel:~> date --date="2007-1-1 100 minutes" Mon Jan 1 01:40:00 CET 2007
Maybe it can be refined, but you can see it does the conversion of 100 minutes.
That's an interesting variation of the date command. And I'm certainly going to keep that in mind should the need for that type of variation arise. But in my script case, I was looking to convert a value into minutes/seconds not relative to clock time.
My itchy trigger finger deleted several emails in the thread. Did anyone refine the date cccommand??? $ date --date="2007-1-1 100 seconds" +"This is %M:%S minutes" This is 01:40 minutes Thanks, Ken -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The Monday 2007-11-19 at 23:04 -0500, Ken Jennings wrote:
cer@nimrodel:~> date --date="2007-1-1 100 minutes" Mon Jan 1 01:40:00 CET 2007
Maybe it can be refined, but you can see it does the conversion of 100 minutes.
That's an interesting variation of the date command. And I'm certainly going to keep that in mind should the need for that type of variation arise. But in my script case, I was looking to convert a value into minutes/seconds not relative to clock time.
My itchy trigger finger deleted several emails in the thread. Did anyone refine the date cccommand???
Nop :-)
$ date --date="2007-1-1 100 seconds" +"This is %M:%S minutes" This is 01:40 minutes
Right! - -- Cheers, Carlos E. R. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.4-svn0 (GNU/Linux) Comment: Made with pgp4pine 1.76 iD8DBQFHQuYWtTMYHG2NR9URAiQiAJ0ch7QlvEpR0vfAv1iJwIrEiSXt6QCeJybY exzdWZOjCvYXTlE0nYaKCWo= =Hnhz -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Mon, 19 Nov 2007, Bryen wrote:
For all you scripting gurus out there. Can you help me out?
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
POSIX Shell (e.g. bash, ksh and others): ==== for secondsin in 1 59 61 100 101 10000; do temp=$secondsin; h=$(( temp / 3600 )); if test $h -gt 0; then temp=$(( temp - h * 3600 )); fi m=$(( temp / 60 )); s=$(( temp % 60 )); printf "%i second(s) are %i hours, %i minutes and %i seconds\n" \ $secondsin $h $m $s; done ==== Adjust the output-format as you like, you got the neccessary numbers neatly seperated in $h, $m and $s. See "Arithmetic Expansion" or something like that in the manpage of your shell. Tested with bash 2.03 (in bash and sh mode), pdksh 5.2 and zsh 3.0. pdksh and zsh seem to have no 'printf' builtin (/usr/bin/printf is used), but that should not be a problem. Any questions? HTH, -dnh -- "The Unix phenomenon is scary. It doesn't go away." -- Steve Ballmer -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tue, 2007-11-20 at 06:11 +0100, David Haller wrote:
Hello,
On Mon, 19 Nov 2007, Bryen wrote:
For all you scripting gurus out there. Can you help me out?
I'm trying to convert a value to an output to the user of minutes:seconds.
For example: if $A=100 (for seconds) Then echo "This is 1:40 minutes"
How would I do this?
POSIX Shell (e.g. bash, ksh and others):
==== for secondsin in 1 59 61 100 101 10000; do temp=$secondsin; h=$(( temp / 3600 )); if test $h -gt 0; then temp=$(( temp - h * 3600 )); fi m=$(( temp / 60 )); s=$(( temp % 60 )); printf "%i second(s) are %i hours, %i minutes and %i seconds\n" \ $secondsin $h $m $s; done ====
Adjust the output-format as you like, you got the neccessary numbers neatly seperated in $h, $m and $s.
See "Arithmetic Expansion" or something like that in the manpage of your shell.
Tested with bash 2.03 (in bash and sh mode), pdksh 5.2 and zsh 3.0.
pdksh and zsh seem to have no 'printf' builtin (/usr/bin/printf is used), but that should not be a problem.
Any questions?
HTH, -dnh
-- "The Unix phenomenon is scary. It doesn't go away." -- Steve Ballmer
Now that was a fun one! :-) The earlier python one worked superbly as well, but this one I can understand it a little better and able to tweak it to my preferences/needs. Thanks man! Really impressed with how everyone has come up with different and effective variations of time scripting. You're all invited to my place for drinks this weekend! -- ---Bryen--- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Bryen wrote:
Really impressed with how everyone has come up with different and effective variations of time scripting. Unix/Linux is like huge toolbox ...
You're all invited to my place for drinks this weekend! Where ;) ? Just joking ...
Rgds, Stephan. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (9)
-
Aaron Kulkis
-
Anders Johansson
-
Bryen
-
Carlos E. R.
-
David Haller
-
Georg
-
Ken Jennings
-
Stephan Hegel
-
Theo v. Werkhoven