Author Topic: Find latest Date  (Read 2714 times)

0 Members and 1 Guest are viewing this topic.

GDF

  • Water Moccasin
  • Posts: 2081
Find latest Date
« on: September 08, 2007, 04:16:08 PM »
How would I go about using lisp to determine the latest date from a list of dates, like in the following:
12 March 2007
21 April 2007
7 Sept 2007

This is what I use to create the dates:

(setq date (ARCH:C_DATE-ISSUE (getvar "tdupdate")))

Code: [Select]
(defun ARCH:C_DATE-ISSUE  (j / y d m)
  (setq j (fix j)
        j (- j 1721119.0)
        y (fix (/ (1- (* 4 j)) 146097.0))
        j (- (* j 4.0) 1.0 (* 146097.0 y))
        d (fix (/ j 4.0))
        j (fix (/ (+ (* 4.0 d) 3.0) 1461.0))
        d (- (+ (* 4.0 d) 3.0) (* 1461.0 j))
        d (fix (/ (+ d 4.0) 4.0))
        m (fix (/ (- (* 5.0 d) 3) 153.0))
        d (- (* 5.0 d) 3.0 (* 153.0 m))
        d (fix (/ (+ d 5.0) 5.0))
        y (+ (* 100.0 y) j))
  (if (< m 10.0)
    (setq m (+ m 3))
    (setq m (- m 9)
          y (1+ y)))
  (strcat (if (< D 10)
            "0"
            "")
          (itoa (fix D))
          " "
          (nth (1- (fix m))
               (list "Jan" "Feb" "March" "April" "May" "June" "July" "Aug" "Sept" "Oct" "Nov"
                     "Dec"))
          " "
          (substr (itoa (fix Y)) 1 4)
          ;;3 2)
          ))

Thanks

Gary
« Last Edit: September 08, 2007, 04:19:25 PM by Gary Fowler »
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Find latest Date
« Reply #1 on: September 08, 2007, 07:29:03 PM »
Hi,

Here's a routine to sort the dates

(last (sort-date '("7 Sept 2007" "9 Sept 2007" "21 March 2007" "12 April 2000" "21 March 2004" "27 April 2006"))) returns "9 Sept 2007", the latest
(car (sort-date '("7 Sept 2007" "9 Sept 2007" "21 March 2007" "12 April 2000" "21 March 2004" "27 April 2006"))) returns "12 April 2000", the oldest

Code: [Select]
(defun sort-date (date-lst / year month day)

  (defun year (str)
    (atoi (substr str (+ 2 (vl-string-position 32 str 0 T))))
  )

  (defun month (str)
    (vl-position
      (substr str
      (+ 2 (vl-string-position 32 str))
      (- (vl-string-position 32 str 0 T) (vl-string-position 32 str) 1)
      )
      (list "Jan"    "Feb"    "March"  "April" "May" "June"
    "July"   "Aug"    "Sept"   "Oct" "Nov" "Dec"
   )
    )
  )
 
  (defun day (str)
    (atoi (substr str 1 (vl-string-position 32 str)))
  )

  (vl-sort date-lst
   '(lambda (x1 x2)
      (if (= (year x1) (year x2))
(if (= (month x1) (month x2))
(< (day x1) (day x2))
  (< (month x1) (month x2))
)
(< (year x1) (year x2))
      )
    )
  )
)
« Last Edit: September 08, 2007, 07:35:35 PM by gile »
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Find latest Date
« Reply #2 on: September 08, 2007, 07:41:01 PM »
Another way, a little bit different

Code: [Select]
(defun sort-date (date-lst / pref date2num)

  (defun pref (str)
    (if (= 1 (strlen str))
      (strcat "0" str)
      str
    )
  )

  (defun date2num (str / sp1 sp2)
    (setq sp1 (vl-string-position 32 str)
  sp2 (vl-string-position 32 str 0 T)
    )
    (strcat
(substr str (+ 2 sp2))
(pref (itoa (1+ (vl-position
  (substr str (+ 2 sp1) (- sp2 sp1 1))
  (list "Jan" "Feb"   "March"  "April"
"May" "June"   "July"   "Aug"
"Sept" "Oct"   "Nov"    "Dec"
       )
)
    )
      )
)
(pref (substr str 1 sp1))
      )
  )
 
  (vl-sort date-lst '(lambda (a b) (< (date2num a) (date2num b))))
)
Speaking English as a French Frog

GDF

  • Water Moccasin
  • Posts: 2081
Re: Find latest Date
« Reply #3 on: September 08, 2007, 08:10:02 PM »
Thank you...that is just what I needed.

Gary
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Find latest Date
« Reply #4 on: September 08, 2007, 11:23:07 PM »
Another way:
Code: [Select]
;;  CAB 09.08.07
(defun getdate (date$ / dlst day year month)
  (defun sparser (str delim / ptr lst)
    (while (setq ptr (vl-string-search delim str))
      (setq lst (cons (substr str 1 ptr) lst))
      (setq str (substr str (+ ptr 2)))
    )
    (reverse (cons str lst))
  )
  (defun c2j (yr m d / y a b)
    (setq y yr)
    (and (<= m 2) (setq y (1- y) m (+ m 12)))
    (if (or (< yr 1582) (and (= yr 1582) (or (< m 10) (and (= m 10) (< d 5)))))
      (setq b 0) ; Julian calendar
      (setq a (fix (/ y 100)) ; Gregorian calendar
            b (+ (- 2 a) (fix (/ a 4))))
    )
    (+ (fix (+ (* 365.25 (+ y 4716)) (fix (* 30.6001 (+ m 1))))) d b -1524.0)
  )

  (setq dlst (sparser date$ " "))
  (setq day   (atoi (car dlst))
        year  (atoi (last dlst))
        month (vl-position
                (strcase(substr (cadr dlst) 1 3))
                '("JAN"    "FEB"    "MAR"    "APR"    "MAY"    "JUN"
                  "JUL"    "AUG"    "SEP"    "OCT"    "NOV"    "DEC") )
  )
  (c2j year month day)
)

;;=======================================
;;  get the latest date from  list of dates
(defun c:test (/ datelist result)
  (setq datelist (mapcar
                   '(lambda (x)
                      (cons x (getdate x))
                    )
                   '("7 Sept 2007" "9 Sept 2007" "21 March 2007" "12 April 2000" "21 March 2004" "27 April 2006")
                 )
  )
  (mapcar '(lambda(x) (cond ((null result) (setq result x))
                            ((> (cdr x) (cdr result)) (setq result x))))
          datelist)
  result
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Find latest Date
« Reply #5 on: September 09, 2007, 11:07:18 AM »
Thanks Alan and gile

This is for a routine in the link: http://www.theswamp.org/index.php?topic=8661.0
You and Jeff Mishler helped me with most of the setup of this routine. I am trying to dust it off and improve on it....but my lisp
skills are limited. Tim Willey is giving me a helping hand.

I know the Sheet Index routine (see link) is setup for our office, but when Tim and I get it working better, I will post it here.

Gary
« Last Edit: September 09, 2007, 01:18:42 PM by Gary Fowler »
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

GDF

  • Water Moccasin
  • Posts: 2081
Re: Find latest Date
« Reply #6 on: September 13, 2007, 11:33:46 AM »
Thanks Alan and gile

This is for a routine in the link: http://www.theswamp.org/index.php?topic=8661.0
You and Jeff Mishler helped me with most of the setup of this routine. I am trying to dust it off and improve on it....but my lisp
skills are limited. Tim Willey is giving me a helping hand.

I know the Sheet Index routine (see link) is setup for our office, but when Tim and I get it working better, I will post it here.

Gary


Lispers

Here is Tim Willey's routine that he did for me. It was a total rewrite of the routine found in the link above.
Also inclosed are the attributed blocks that I use in paper space for each sheet file, along with the resulting files.

Thanks again Tim for a job well done. Tim was compensated for his work. If anyone needs custom work
done for their office, I highly recommend Tim.

Gary
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64