TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: domenicomaria on January 03, 2022, 11:57:19 AM

Title: date to week day
Post by: domenicomaria on January 03, 2022, 11:57:19 AM
Code - Auto/Visual Lisp: [Select]
  1. (defun :DATE>WEEKDAY ( year month day / century-code month-code tmp x-nth year-code )
  2.  
  3.         (defun :DXF (code elist) (cdr (assoc code elist ) ) )
  4.  
  5.         (setq tmp (atoi (substr (itoa year) 1 2) ) )
  6.  
  7.         (cond
  8.                 ( (= tmp 17) (setq century-code 4) )   
  9.                 ( (= tmp 18) (setq century-code 2) )   
  10.                 ( (= tmp 19) (setq century-code 0) )   
  11.                 ( (= tmp 20) (setq century-code 6) )
  12.                 ( (= tmp 21) (setq century-code 4) )
  13.         )
  14.  
  15.         (setq month-code  (:DXF month '( (1 . 0)(2 . 3)(3 . 3)(4 . 6)(5 . 1)(6 . 4)(7 . 6)(8 . 2)(9 . 5)(10 . 0)(11 . 3)(12 . 5) ) ) )
  16.        
  17.         (setq year-code   (atoi (substr (itoa year) 3 2) ) )
  18.  
  19.         (setq year-code   (rem (+ year-code (/ year-code 4) ) 7) )
  20.        
  21.         (setq x-nth (rem (+ year-code month-code century-code day) 7) )
  22.  
  23.        
  24. ;|      
  25.         LEAP YEAR CODE                                                                                                                                                                                                  -
  26.         The other thing to take into account is whether you are dealing with a leap year
  27.  
  28.         EDIT: If the date is in a January or February of a leap year,
  29.         you have to subtract one from your total before the final step
  30.  
  31.         https://artofmemory.com/blog/how-to-calculate-the-day-of-the-week/
  32.  
  33.         https://www.calculator.net/day-of-the-week-calculator.html?today=02%2F02%2F1940&x=86&y=19
  34. |;
  35.        
  36.         (if (and (= (rem year 4) 0) (member month '(1 2) ) )    (setq x-nth (- x-nth 1) ) )
  37.  
  38.        
  39.         (if (= x-nth -1) (setq x-nth 6) )
  40.  
  41.  
  42.         (nth x-nth '("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday") )
  43. )
  44.  
  45. (defun C:K () (:DATE>WEEKDAY 2022 1 3 ) )

Just to share a nice thing !
Let me know if it works
Title: Re: date to week day
Post by: Stefan on January 03, 2022, 12:32:21 PM
Not that it matters too much, but

Code: [Select]
_$ (:DATE>WEEKDAY 1899 12 31)
"Sunday"
_$ (:DATE>WEEKDAY 1900 01 01)
"Sunday"
_$

Title: Re: date to week day
Post by: domenicomaria on January 03, 2022, 12:53:50 PM
 :embarrassed2: :? :laugh:

you are right ...

the tests I did went well ...

have you an idea
what the error could be? 

I followed the instructions
of the link I attached ...

just to understand if the algorithm is wrong,
or I made some mistake . . .
Title: Re: date to week day
Post by: Stefan on January 03, 2022, 01:02:34 PM
:embarrassed2: :? :laugh:

you are right ...

the tests I did went well ...

have you an idea
what the error could be? 

I followed the instructions
of the link I attached ...

1900 is not a leap year. Hundreds of years are leap only once in 400 years.
1600, 2000, 2400 - leap years. But not 1700,1800,1900,2100 etc
Title: Re: date to week day
Post by: domenicomaria on January 03, 2022, 01:09:56 PM
so, what is the right formula to know if an year is leap  ?

(= (rem year 4) 0) is wrong !
Title: Re: date to week day
Post by: Stefan on January 03, 2022, 01:20:09 PM
so, what is the right formula to know if an year is leap  ?

(= (rem year 4) 0) is wrong !

Code - Auto/Visual Lisp: [Select]
  1.   (and
  2.     (if
  3.       (= (rem year 100) 0)
  4.       (= (rem year 400) 0)
  5.       (= (rem year   4) 0)
  6.     )
  7.     (member month '(1 2) )
  8.   )
  9.   (setq x-nth (- x-nth 1))
  10. )
Title: Re: date to week day
Post by: domenicomaria on January 03, 2022, 01:45:08 PM
Code - Auto/Visual Lisp: [Select]
  1. (defun :LEAP-YEAR? (year)
  2.         (if     (= (rem year 100) 0)    (= (rem year 400) 0)    (= (rem year 4  ) 0)    )
  3. )
  4.  
  5.        
  6. $ (:LEAP-YEAR? 1900)
  7. nil
  8.  
  9. _$ (:LEAP-YEAR? 1896)
  10. T
  11.  
  12. _$ (:LEAP-YEAR? 1700)
  13. nil
  14.  
  15. _$ (:LEAP-YEAR? 1704)
  16. T
  17.  
  18. _$ (:LEAP-YEAR? 1696)
  19. T
  20.  
  21. _$ (:LEAP-YEAR? 2000)
  22. T
  23.  
  24. _$ (:LEAP-YEAR? 2400)
  25. T
  26.  
  27. _$ (:LEAP-YEAR? 1600)
  28. T
  29.  

thanks a lot, Stefan

You are always very clever . . .
Title: Re: date to week day
Post by: Lee Mac on January 03, 2022, 05:43:31 PM
Related thread -
https://www.theswamp.org/index.php?topic=41716.0
Title: Re: date to week day
Post by: domenicomaria on January 03, 2022, 11:14:19 PM
Related thread -
https://www.theswamp.org/index.php?topic=41716.0

thank you

it looks very interesting

but it's not that simple to understand

. . .
I have to study it!
Title: Re: date to week day
Post by: ribarm on June 27, 2022, 11:39:24 PM
There is also my contrib in topic : STUDY LISP...