TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Andrea on May 27, 2010, 11:00:37 PM

Title: Last Monday of each week
Post by: Andrea on May 27, 2010, 11:00:37 PM
Hi all..
I didn't see any think similar on the web...
so i've decided to post this code here.

The code below Show the last Monday Date.
If any of you have some diffrent way ...i'll be curius to see.  :-)

Code: [Select]
(setq uMonth (menucmd "M=$(edtime,$(getvar,date),MO\)")
      uDay   (strcase (menucmd "M=$(edtime,$(getvar,date),DDD\)") t)
      uDate  (atoi (menucmd "M=$(edtime,$(getvar,date),DD\)"))
      uYear  (menucmd "M=$(edtime,$(getvar,date),YYYY\)")
)
(cond
  ((= uMonth "01")(setq uMonthday 31 pMonth "12"))
  ((= uMonth "02")(setq uMonthday 31 pMonth "01"))
  ((= uMonth "03")(setq uMonthday 28 pMonth "02"))
  ((= uMonth "04")(setq uMonthday 31 pMonth "03"))
  ((= uMonth "05")(setq uMonthday 30 pMonth "04"))
  ((= uMonth "06")(setq uMonthday 31 pMonth "05"))
  ((= uMonth "07")(setq uMonthday 30 pMonth "06"))
  ((= uMonth "08")(setq uMonthday 31 pMonth "07"))
  ((= uMonth "09")(setq uMonthday 31 pMonth "08"))
  ((= uMonth "10")(setq uMonthday 30 pMonth "09"))
  ((= uMonth "11")(setq uMonthday 31 pMonth "10"))
  ((= uMonth "12")(setq uMonthday 30 pMonth "11"))
)
(setq uDpos (vl-position uDay '("mon" "tue" "wed" "thu" "fri" "sat" "sun")))
(if (< (setq uDdiff (- uDate uDpos)) 0)
    (setq uDate (+ uMonthday uDdiff)
          uMonth pMonth)
  (setq uDate (- uDate uDpos))
)
(alert (strcat "Monday the " (itoa uDate) " of " uMonth " Month " uYear))
Title: Re: Last Monday of each week
Post by: uncoolperson on May 27, 2010, 11:23:46 PM
how can there be a last monday of each week?

(not intending to be mean)
Title: Re: Last Monday of each week
Post by: Daniel J. Ellis on May 28, 2010, 02:56:50 AM
I think it's meant to go something like: "Today is Friday 28th May, so is in the week commencing Monday 24th May"

dJE
Title: Re: Last Monday of each week
Post by: Lee Mac on May 28, 2010, 06:35:53 AM
Nice idea, I suppose it could be made slightly more concise:

Code: [Select]
(defun c:Mon ( / uMonth uDay uDate uYear uMonthday pMonth uDpos uDdiff )
 
  (setq uMonth (menucmd "M=$(edtime,$(getvar,date),MO\)")
        uDay   (strcase (menucmd "M=$(edtime,$(getvar,date),DDD\)") t)
        uDate  (atoi (menucmd "M=$(edtime,$(getvar,date),DD\)"))
        uYear  (menucmd "M=$(edtime,$(getvar,date),YYYY\)")
  )
  (setq uMonthday (nth (1+ (atoi uMonth)) '(31 31 28 31 30 31 30 31 31 30 31 30))
        pMonth    (itoa (1+ (rem (+ 10 (atoi uMonth)) 12))))

  (setq uDpos (vl-position uDay '("mon" "tue" "wed" "thu" "fri" "sat" "sun"))) 
  (if (< (setq uDdiff (- uDate uDpos)) 0)
    (setq uDate (+ uMonthday uDdiff) uMonth pMonth)
    (setq uDate (- uDate uDpos))
  ) 
  (alert (strcat "Monday the " (itoa uDate) " of " uMonth " Month " uYear))
  (princ)
)
Title: Re: Last Monday of each week
Post by: Lee Mac on May 28, 2010, 06:53:55 AM
How about this  :lol:

Code: [Select]
(alert
  (menucmd
    (strcat "M=$(edtime,"
      (rtos
        (- (getvar 'DATE)
          (vl-position (strcase (menucmd "M=$(edtime,$(getvar,date),DDD\)") t)
            '("mon" "tue" "wed" "thu" "fri" "sat" "sun")
          )
        )
        2 15
      )
      ",DD/MO/YYYY)"
    )
  )
)
Title: Re: Last Monday of each week
Post by: Andrea on May 28, 2010, 09:34:34 AM
wow Lee Nice !   :ugly:

Title: Re: Last Monday of each week
Post by: VovKa on May 28, 2010, 10:09:13 AM
Code: [Select]
(alert
  (menucmd (strcat "M=$(edtime,"
   (rtos (- (getvar 'DATE) (fix (rem (getvar 'DATE) 7))) 2 15)
   ",DD/MO/YYYY)"
   )
  )
)
Title: Re: Last Monday of each week
Post by: Andrea on May 28, 2010, 10:23:14 AM
Code: [Select]
(alert
  (menucmd (strcat "M=$(edtime,"
   (rtos (- (getvar 'DATE) (fix (rem (getvar 'DATE) 7))) 2 15)
   ",DD/MO/YYYY)"
   )
  )
)

OMG !!  i'm out on this one..
nice coding Vovka !
Title: Re: Last Monday of each week
Post by: Lee Mac on May 28, 2010, 12:04:32 PM
Code: [Select]
(rtos (- (getvar 'DATE) (fix (rem (getvar 'DATE) 7))) 2 15)

Ah! I should have thought of that one - nice on VovKa you have trumped me yet again  8-)
Title: Re: Last Monday of each week
Post by: VovKa on May 28, 2010, 12:31:51 PM
Code: [Select]
(rtos (- (getvar 'DATE) (fix (rem (getvar 'DATE) 7))) 2 15)

Ah! I should have thought of that one - nice on VovKa you have trumped me yet again  8-)
(setq MyEgo (1+ MyEgo))  :)
Title: Re: Last Monday of each week
Post by: Krushert on May 28, 2010, 01:48:02 PM
I am home for day so I can test the the above code.  What is the purpose or the final goal? 
I am asking becuase if it is what I think it is, then I might have a use for something like this.
Title: Re: Last Monday of each week
Post by: Lee Mac on May 28, 2010, 01:56:56 PM
I'm guessing it might be useful when filling out a timesheet for week commencing...  ;-)
Title: Re: Last Monday of each week
Post by: Andrea on May 31, 2010, 03:22:09 PM
I am home for day so I can test the the above code.  What is the purpose or the final goal? 
I am asking becuase if it is what I think it is, then I might have a use for something like this.


In Fact,...I have a prog who it write on TXT file all print data with:
date,loginname,papersize,paperstyle,printer name, number of copies,total area,...etc.
who can be imported in to an excel sheet and determine total cost by project, by date etc..
1 file by week, and the name need to be the Monday date. eg: 2010-05-31-Printdata.csv