Jump to content

LISP to convert seconds to hour : min : sec format


Recommended Posts

Posted

I'm using the lisp below to convert time in seconds to the format (hour : min: sec) but sometimes I get negative value error.

 

I'm sure there is a better lisp around. All help is appreciated.

 

 

(defun tempo (seconds)

;;;(setq seconds 3666.)
(setq ore (/ seconds 3600.))
(setq oret (rtos ore 2 6))
(setq oretlen (strlen oret))
;;;(setq orelen (strlen ore))
(setq oret_p (substr oret 1 (- oretlen 7)))          
(setq minuti (substr oret (- oretlen 6) 6))
(setq minuti (* (atof minuti) 60.))
(setq minuti (rtos minuti 2 6))
(setq minlen (strlen minuti))
(setq min_p (substr minuti 1 (- minlen 7)))
;//////////////////
(setq sec (substr minuti (- minlen 6) 6))
(setq sec (* (atof sec) 60.))
(setq sec (rtos sec 2 6))
(setq seclen (strlen sec))
(setq sec_p (substr sec 1 (- seclen 7)))
[color=red](setq tmp_result (strcat oret_p " h " min_p " m " sec_p " s"))[/color]
;;;(princ tmp_result)(princ)  
 
      
      );defun

Posted (edited)

Had to look up a bit about menucmd & edtime in the AutoCAD developers file but maybe something like this could be useful

 

(menucmd "M=$(edtime,$(getvar,date), H:MM:SS)")

 

Regards

 

Jammie

Edited by jammie
Ignore reply, misunderstood original request
Posted

Quick one, not too elegant :(

 

(defun FormatSeconds ( sec )
 (
   (lambda ( s )
     (strcat (itoa (/ s 3600)) "h " 
       (itoa (/ (rem s 3600) 60)) "m "
       (itoa (rem (rem s 3600) 60)) "s "
     )
   )
   (fix sec)
 )
)

 

Assumes integer argument.

Posted (edited)

another quickey... even less elegant

(setq ts (getreal "\nEnter total seconds: ")
     hrs (/ ts 3600.)
     rem1 (rem ts 3600. )
     mins (/ rem1 60.)
     rem2 (rem mins 1.)
     secs (* rem2 60.)
           )
(princ (strcat (rtos hrs 2 0) " hrs : " (rtos mins 2 0) " mins : " (rtos secs 2 2) " secs"))

Edited by lpseifert
fixed math
Posted

Thanks lpseifert

Thanks Lee,

 

Very elegant indeed - and better still - it does my case.

 

I tried both though my seconds input is a real number.

 

 

 

:)

Posted
Thanks lpseifert

Thanks Lee,

 

Very elegant indeed - and better still - it does my case.

 

I tried both though my seconds input is a real number.

 

 

 

:)

 

Mine allows a REAL input, but will convert this straight to an INT, I shall have a rethink on my function...

Posted

Hows this:

 

(defun LM:FormatSeconds ( s l )
 ;; © Lee Mac 2010
 (if l
   (cons (fix (/ s (car l)))
     (LM:FormatSeconds (rem s (car l)) (cdr l))
   )
 )
)

(defun test ( seconds )
 (apply 'strcat
   (mapcar 'strcat
     (mapcar 'itoa
       (LM:FormatSeconds seconds '(3600.0 60.0 1.0 0.001))
     )
     '("h " "m " "s " "ms")
   )
 )
)

Will format to any level, provided that you supply the multipliers :)

Posted

Maybe add a little formating:

 [b][color=BLACK]([/color][/b]setq sec [b][color=FUCHSIA]([/color][/b]getreal [color=#2f4f4f]"\nSeconds:   "[/color][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 [b][color=BLACK]([/color][/b]setq hrs [b][color=FUCHSIA]([/color][/b]fix [b][color=NAVY]([/color][/b]/ sec 3600.[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
       min [b][color=FUCHSIA]([/color][/b]fix [b][color=NAVY]([/color][/b]/ [b][color=MAROON]([/color][/b]rem sec 3600[b][color=MAROON])[/color][/b]  60[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
       sec [b][color=FUCHSIA]([/color][/b]rem sec 60[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 [b][color=BLACK]([/color][/b]princ [b][color=FUCHSIA]([/color][/b]strcat [color=#2f4f4f]"\n"[/color] [b][color=NAVY]([/color][/b]itoa hrs[b][color=NAVY])[/color][/b] [color=#2f4f4f]":"[/color]
                     [b][color=NAVY]([/color][/b]if [b][color=MAROON]([/color][/b]< min 10[b][color=MAROON])[/color][/b] [color=#2f4f4f]"0"[/color] [color=#2f4f4f]""[/color][b][color=NAVY])[/color][/b]
                     [b][color=NAVY]([/color][/b]itoa min[b][color=NAVY])[/color][/b] [color=#2f4f4f]":"[/color]
                     [b][color=NAVY]([/color][/b]if [b][color=MAROON]([/color][/b]< sec 10[b][color=MAROON])[/color][/b] [color=#2f4f4f]"0"[/color] [color=#2f4f4f]""[/color][b][color=NAVY])[/color][/b]
                     [b][color=NAVY]([/color][/b]rtos sec 2 4[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

-David

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...