Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Convert time CDATE in absolute minutes

7 REPLIES 7
Reply
Message 1 of 8
Gustavo_Bernardi
909 Views, 7 Replies

Convert time CDATE in absolute minutes

How to convert the format obtained in "CDate" in absolute minutes?

 

ex:

Command: (rtos (getvar "CDate") 2 4)

 

The result is:

 

"20130326.1301"

 

What I need is:

+01 minuts
+13 hours X 60 = 780 minuts
+26  x 1440 = 37440 minuts

 

the current time this month is: 38221 minutes

 

TIA

7 REPLIES 7
Message 2 of 8


@Gustavo_Bernardi wrote:

How to convert the format obtained in "CDate" in absolute minutes?

.... 

"20130326.1301"

What I need is:

+01 minuts
+13 hours X 60 = 780 minuts
+26  x 1440 = 37440 minuts

....


The (substr) function is the key function for something like this.  You can get the day/hour/minute parts from there [i.e. remove the year and month parts] with it:

 

(setq DHM (substr (rtos (getvar 'cdate) 2 4) 7))

 

Then you can multiply the pieces [converted to integers] by the appropriate numbers, and add them up:

 

(setq TotalMinutesSoFarThisMonth

  (+

    (* (atoi (substr DHM 1 2)) 1440); days

    (* (atoi (substr DHM 4 2)) 24); hours

    (atoi (substr DHM 6)); minutes

  ); +

); setq

Kent Cooper, AIA
Message 3 of 8
Kent1Cooper
in reply to: Kent1Cooper


@Kent1Cooper wrote:
....

The (substr) function is the key function for something like this.  You can get the day/hour/minute parts from there [i.e. remove the year and month parts] with it:

 

(setq DHM (substr (rtos (getvar 'cdate) 2 4) 7))

 

Then you can multiply the pieces [converted to integers] by the appropriate numbers, and add them up:

 

(setq TotalMinutesSoFarThisMonth

  (+

    (* (atoi (substr DHM 1 2)) 1440); days

    (* (atoi (substr DHM 4 2)) 24); hours

    (atoi (substr DHM 6)); minutes

  ); +

); setq


Come to think of it, you could skip the middleman of truncating CDATE to just day/hour/minute parts, and simply change the starting-character-position numbers in the substring functions in the add-it-up variable:

 

(setq

  datestring (rtos (getvar 'cdate) 2 4)

  TotalMinutesSoFarThisMonth

    (+

      (* (atoi (substr datestring 7 2)) 1440); days

      (* (atoi (substr datestring 10 2)) 24); hours

      (atoi (substr datestring 12)); minutes

    ); + & TotalMinutesSoFarThisMonth

); setq

Kent Cooper, AIA
Message 4 of 8

Thanks Kent, This issue is resolved with your help

 

I want to create a lisp to save my project periodically, associate to some commands. In the case with the command move.

 

Now the problem (one of) is when the file not yet saved, and the command qsave create a file in My Documents.

 

	
	
	(defun c:copysave(/ select frase DHM timetosavestring frase saveorno)
	(if (= timetosave nil) (setq timetosave 1));;I set the interval to save
	(if 
	(= oldtime nil)
	(progn
	(setq DHM (substr (rtos (getvar 'cdate) 2 4) 7))
	(setq oldtime ;; get the initial time
	(+
	(* (atoi (substr DHM 1 2)) 1440); days
	(* (atoi (substr DHM 4 2)) 60); hours
	(atoi (substr DHM 6)); minutes
	)
	)
	)
	)
	(setq DHM (substr (rtos (getvar 'cdate) 2 4) 7));;Get the actual time
	(setq atualtime
	(+
	(* (atoi (substr DHM 1 2)) 1440); days
	(* (atoi (substr DHM 4 2)) 60); hours
	(atoi (substr DHM 6)); minutes
	)
	)
	(if 
	(> oldtime atualtime);; solves the problem of the last day of the month and the first of the following month
	(progn
	(setq DHM (substr (rtos (getvar 'cdate) 2 4) 7))
	(setq oldtime
	(+
	(* (atoi (substr DHM 1 2)) 1440); days
	(* (atoi (substr DHM 4 2)) 60); hours
	(atoi (substr DHM 6)); minutes
	)
	)
	)
	)
	(setq deltime (- atualtime oldtime));;; verify the time variation
	(cond
	(
	(< deltime timetosave);;; if the time is less than thr timesave only use the copy
	(setq select (ssget))
	(command "._copy" select "")
	)
	(
	(>= deltime timetosave);;; if the time is greater than the interval to save
	(initget "S N Y I" 0)
	(setq timetosavestring(rtos timetosave))
	(setq frase (strcat "\nHora de salvar o projeto? [Sim/Não/Intervalo= "timetosavestring" min]<Sim>"));;; save the project or no, and define the interval
	(setq saveorno(getkword frase))
		(cond
		(
		(or (= saveorno "S")(= saveorno "Y")(= saveorno nil));;If the save is Yes
		((setq oldtime atualtime)(command "._qsave")(setq select (ssget))(command "._copy" select ""));;reset the oldtime anda save the project
		)
			(
			(= saveorno "N");;; Not save
			(setq oldtime atualtime);;reset the time
			(setq select (ssget))
			(command "._copy" select "")
			)
		(
		(= saveorno "I");;set the interval
		(initget 7)
		(setq timetosave(getreal "\nIntervalo para salvamento: "));;;Get the interval
		(c:copysave);;restart the command
		)
		);;end of conditions in save
	);;end of save
	);;end of save or no
	);;end of lisp

 How to solve this?

 

Sorry my english

Message 5 of 8
pbejse
in reply to: Gustavo_Bernardi

look into DWGTITLED system variable

 

If value is 0 , its either you are prompted for a drawing name and path or terminate the command and flash an alert box.

 

Message 6 of 8

Now that I see what you're doing with it, I have a suggestion for a simpler way to do the same thing, which can shrink your routine considerably.

 

The DATE System Variable is stored in decimal days, which means it is always numerically increasing, and no portion of it shifts from 28 or 29 or 30 or 31 back to 1 at the month turnover, or from 59 back to 00 at the hour turnover, etc.  So if you use DATE values, you can get a direct comparison without having to convert to total minutes so far this month.

 

(if (not timetosave) (setq timetosave 1.0));; set the interval to save [see comments below about the 1.0]

(if (not oldtime) (setq oldtime (getvar 'date)))

(setq atualtime (getvar 'date))

 

There would be no need to compare oldtime to atualtime and possibly reset oldtime because of a month turnover, because atualtime will always be greater than oldtime, no matter what.

 

Then, with timtosave being a real number in minutes, you can calculate the decimal fraction of a day that it represents very simply:

....

(setq deltime (- atualtime oldtime))
(cond
  (
    (< deltime (/ timetosave 24 60));;; if the time is less than the timesave only use the copy
....

  ); end less-than condition
  (
    (>= deltime (/ timetosave 24 60));;; if the time is greater than the interval to save

....

 

You already have timetosave being a real number where it's asked for with a prompt, because it's in a (getreal) function, and to correspond with that, I changed the default at the beginning from 1 to 1.0.  It has to be a real [decimal] number, not an integer, or the division with 24 and 60 as integers will always result in an integer, which won't give you the comparison you need.  [You could also use (getint) to ask for timetosave, keep the default at 1 as an integer, and change either the 24 to 24.0 or the 60 to 60.0 (or both) in the division.  But leaving it as a real number means someone could choose a timetosave value of 2.5 minutes, which wouldn't be possible if it needed to be an integer.]

Kent Cooper, AIA
Message 7 of 8

So much better ... What I was doing is what we named here in Brazil as "gambiarra" (check on google about)

 

Thank you guys...

 

Now I need only elect to associate commands. Initially I will use with the commands: LAYISO and LAYON

 

I chose to use real and not integer.

Below is the routine I think everything is OK:

 

 

(defun c:layisosave(/ select frase DHM timetosavestring frase saveorno)
(if (not timetosave) (setq timetosave 15.0))
(if (not oldtime) (setq oldtime (getvar 'date)))
(setq atualtime (getvar 'date))
(setq deltime (- atualtime oldtime))
(cond
(
(< deltime (/ timetosave 24.0 60.0))
(command "._layiso")
)
(
(>= deltime (/ timetosave 24.0 60.0))
(setq timetosavestring(rtos timetosave))
(setq select (ssget))
(initget "S N Y I" 0)
(setq frase (strcat "\nHora de salvar o desenho? [Sim/Não/Intervalo= "timetosavestring " min]<Sim>"))
(setq saveorno(getkword frase))
(cond
(
(or (= saveorno "S")(= saveorno "Y")(= saveorno nil))
(setq oldtime atualtime)
(if
(= 0 (getvar "DWGTITLED"))(alert 
     "Você ainda não salvou seu arquivo?
Fique calmo...Você pode salvar com o atalho CTRL+S.
Você pode mudar o intervalo de alerta com o comando TIMETOSAVE
:)
")
(command "_.qsave")
)
(command "._layiso" select "")
)
(
(= saveorno "N")
(setq oldtime atualtime)
(command "._layiso" select "")
)
(
(= saveorno "I")
(initget 7)
(setq timetosave(getreal "\nIntervalo para salvamento: "))
(c:layisosave)
)
)
)
)
)

(defun c:timetosave()
(setq timetosave(getreal "\nIntervalo para salvamento: "))
)

 

Message 8 of 8

That looks good [within the limitation on my part that you know at least one more language than I do], though I have two small comments:

 

You can, if you like, use 24 and 60 as integers in the divisions, as long as the timetosave variable is a real number.  If any number in a division is a real number, the result will be, too -- they do not all need to be real numbers.

 

And you can simplify your first (cond)ition a little bit.  This part:

  ....

  (cond

    (

      (or (= saveorno "S")(= saveorno "Y")(= saveorno nil))
        (setq oldtime atualtime)

  ....

can be done this way:

  ....

  (cond
    (
      (member saveorno '("S" "Y" nil))
        (setq oldtime atualtime)

  ....

 

Any time I see multiple instances of the same thing [in this case, (= saveorno ...) three times over], I look for a different [i.e. shorter] way to do the same thing.

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost