Needing to populate a title block or stamp a drawing with the current date/time is a very common task in the AutoCAD product along with determining the time it might have taken to execute a custom program for return of investment (ROI) analysis. AutoCAD-based products support several system variables that can be used to determine the current date/time, these system variables are:
- CDATE - Stores the current date and time as a decimal value.
- DATE - Stores the current date and time in Modified Julian Date format.
- MILLISECS - Stores the current number of milliseconds since the computer was booted.
NOTE: Starting with AutoCAD 2017, the CDATE
and DATE
system variables no longer store the current time down to milliseconds.
DATE System Variable
The DATE
system variable can be used to get the current date and time within the AutoCAD environment while using AutoLISP, but unlick the CDATE
system variable will require some work to transform the Modified Julian Date format into something recognizable. When queried, the DATE
system variable returns a value as a real number. The GETVAR
function is used to return the value of a system variable for use with the AutoLISP programming language.
Here is an example of getting the value stored in the DATE
system variable:
Command: (getvar "DATE")
2.45781e+06
As previously mentioned, the value of the DATE
system variable is a real number. By default, it isn't possible to see the full value of the DATE
system variable without a bit of extra help. It is best to convert the real number to a string, so you can see and work with the full value which can be accomplished using the RTOS
function. The RTOS
function accepts 3 arguments, the real number to convert to a string and two optional arguments which represent the linear unit mode and precision in which the value should be formatted.
Here is an example of converting the value stored in the DATE
system variable to a string that represents the real value as a decimal with a precision of 9:
Command: (rtos (getvar "DATE") 2 9)
"2457806.411851852"
The date part of the Modified Julian Date format is the integer part of the number (what is to the left of the decimal), while time is represented by the fraction part of the number (what is to the right of the decimal). The date is the number of days since Noon on January 1, 4713 BC... Why not, Noon makes sense and time is expressed as a fraction of the since Midnight which can be calculated by multiplying the fraction by 86,400. If your head is spinning, you are not the only one. Feel free to take up some light reading on the subject about the Julian Day. I am sure you are thinking about just sticking with the Gregorian Calendar whenever possible since it is familiar to most people on the planet, which is easier said than done when working with dates in AutoCAD as drawing creation, last updated, and several other dates are all stored as Modified Julian Dates.
These system variables also store dates and/or time values expressed as a Modified Julian Date:
As you can imagine based on the description, there is a bit of math that is involved to calculate both the date and time using the value returned by the DATE
system variable. While there are no standard AutoLISP functions available for converting a Julian Date to equivalent Gregorian Calendar Date, there are 5 AutoLISP functions in the Express Tools that can be used to make the task easier. These functions are made available after the Julian.lsp file is loaded into the AutoCAD environment, which can be done using the DATE
command or the AutoLISP load
function.
The date related AutoLISP functions that are accessible after the Julian.lsp file is loaded are:
CTOJ
- Converts a Gregorian Calendar date and time into the Julian dateDTOJ
- Converts an AutoCAD formatted calendar date and time into the Julian dateJTOC
- Converts a Julian date to a list that contains a Gregorian Calendar date and timeJTOD
- Converts a Julian date to an AutoCAD formatted calendar date and timeJTOW
- Returns the day of the week based on the specified Julian day
Command: (jtoc (getvar "date"))
(2017 2 22 9 21 27.0)
Command: (rtos (jtod (getvar "date")) 2 6)
"20170222.092127"
Since the AutoLISP functions are defined in a LSP file and that LSP file is not compiled, it is possible to open the Julian.lsp file and see how the functions were written.
Converting a Julian Date to A Gregorian Calendar Date with AutoLISP
While the functions defined in the Express Tools are great, there is one downside to them and that is they are only available when the Express Tools are installed. This code sample demonstrates how to take the Julian Date from the DATE
system variable and convert that value into the equivalent value returned by the CDATE
system variable:
; Converts a Modified Julian Date to the same output as CDATE
; Usage: (Julian2DateTime (getvar "DATE"))
(defun Julian2DateTime (julianTime / jDate jTime AA BB CC DD
EE FF GG Y N D HH MM SS)
; Break the real number into its integer and fractional parts
; Time is based on a fraction of a day, so multiplying that fraction
; by 86,400 gives you the number of seconds that have passed
(setq jDate (fix julianTime)
jTime (* 86400.0 (- julianTime jDate)))
; Calculate the Gregorian date from the Modified Julian Date
; based on the whole number
(setq AA (+ jDate 68569))
(setq BB (/ (* 4 AA) 146097))
(setq CC (- AA (/ (+ (* 146097 BB) 3) 4)))
(setq DD (/ (* 4000 (+ CC 1)) 1461001))
(setq EE (+ (- CC (/ (* 1461 DD) 4)) 31))
(setq FF (/ (* 80 EE) 2447))
(setq GG (/ FF 11))
; Year
(setq Y (+ (* 100 (- BB 49)) DD GG))
; Month
(setq M (* (- (+ FF 2) 12) GG))
; Date
(setq D (- EE (/ (* 2447 FF) 80)))
; Calculate the time from the decimal value of the Modified Julian Date
; Hours
(setq HH (fix (/ jTime 3600.0)))
(setq jTime (- jTime (* HH 3600.0)))
; Minutes
(setq MM (fix (/ jTime 60.0)))
; Seconds
(setq SS (fix (- jTime (* MM 60))))
; Output the date as a string in the YYYYMMDD.HHMMSS format to match CDATE
(strcat (itoa Y)
(if (> M 10)(itoa M)(strcat "0" (itoa M)))
(if (> D 10)(itoa D)(strcat "0" (itoa D)))
"."
(if (> HH 10)(itoa HH)(strcat "0" (itoa HH)))
(if (> MM 10)(itoa MM)(strcat "0" (itoa MM)))
(if (> SS 10)(itoa SS)(strcat "0" (itoa SS)))
)
)
The output that is generated from the Julian2DateTime
function will look similar to the following:
(Julian2DateTime (getvar "DATE"))
"20170222.093707"
(Julian2DateTime (getvar "TDCREATE"))
"20170222.090846"
Hope this article gives you a better understanding of how the value of the DATE
system variable can be used to get and format the current date with AutoLISP.
Other related articles:
- Getting the Current Date/Time with AutoLISP - Part 1
- PSA: Do Your AutoLISP Programs Use the CDATE or DATE System Variables?
- PSA: Additional Date Related System Variables to Be Aware Of in Custom AutoLISP Programs
Sincerely,
Lee
Comments