CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 文档中心

关于获取和计算日期\时间值(AutoLISP)

2023-1-8 11:28| 发布者: admin| 查看: 576| 评论: 0|来自: AutoCAD

摘要: AutoCAD 环境提供了多个系统变量,可用于获取当前系统日期\时间以及有关当前图形的日期\时间相关信息。

AutoCAD 环境提供了多个系统变量,可用于获取当前系统日期\时间以及有关当前图形的日期\时间相关信息。

在标题栏中填充日期字段时,获取当前系统日期\时间会很有帮助,而当您希望让用户知道完成流程所花费的时间或为自定义程序提供基本的投资回报 (ROI) 分析时,计算经过的时间可能会很有用。AutoCAD 环境中的日期\时间值存储为实数,这些值以两种不同的格式表示日期\时间;公历和修改后的儒略历。实数的整数部分(小数点左侧)表示日期,而实数的小数部分(小数点右侧)表示时间。

例如,下面是 CDATE 系统变量返回的当前系统日期\时间的示例:

20170306.15023500

20170306 = 日期: 03/06/2017

15023500 = 时间:下午 3:02:25:00

注意:从基于 AutoCAD 2017 的产品开始,除毫秒外,所有与日期\时间相关的系统变量仅精确到最接近的秒;而在以前的版本中,时间还包括毫秒。如果您需要跟踪时间更改,请注意,即使可能已经过去了 0 到 999 毫秒,在经过一秒之前,您也不会再看到时间变化。当需要计算小于一秒的时间差时,请考虑使用 MILLISECS 系统变量返回的值。

日期\时间相关系统变量

本节列出了与获取与当前图形关联的当前系统日期\时间或日期\时间值相关的系统变量。

当前系统
  • CDATE - 以编码的十进制格式存储当前日期和时间。
  • DATE - 以“修改的儒略日期”格式存储当前日期和时间。
  • 毫秒 - 存储自系统启动以来经过的毫秒数。
与当前图形关联
注意:这些系统变量返回的值采用修改儒略日期格式。
  • TDCREATE - 存储创建图形的本地时间和日期。
  • TDINDWG - 存储总编辑时间,即保存当前图形之间的总经过时间。
  • TDUCREATE - 存储创建图形的通用时间和日期。
  • TDUPDATE - 存储上次更新/保存的本地时间和日期。
  • TDUSRTIMER - 存储用户经过的计时器。
  • TDUUPDATE - 存储上次更新或保存的通用时间和日期。

显示日期\时间相关系统变量的完整值

当使用函数获取存储实数的系统变量的值时,输出到命令提示符时的值以科学记数法显示。虽然科学记数法使 AutoCAD 程序更容易显示较大的十进制数字,但它并不能使它更易于阅读或理解。该函数可用于显示函数返回的实数的所有有效数字。GETVARRTOSGETVAR

Command: (getvar "cdate")
2.01703e+07

Command: (rtos (getvar "cdate") 2 6)
"20170306.175302"
注意:函数的第三个参数控制返回实数的精度。值为 6 表示应返回实数,小数点后有六位有效数字。在基于 AutoCAD 2017 的产品之前,存储在 CDATE 中的值包括毫秒,这些毫秒由小数点后的第 7 位和第 8 位有效数字表示。RTOS

设置 CDATE 返回的日期\时间值的格式

存储在CDATE中的实数以编码的十进制格式表示当前系统日期和时间;将该实数转换为字符串可以更轻松地提取特定数字。使用该函数,可以返回字符串的一部分。SUBSTR

Command: (setq cdate_val (rtos (getvar "cdate") 2 6))
"20170306.175302"

Command: (substr cdate_val 1 4)
"2017"

下面分解了每个数字的重要性:

  • 1-4:年
  • 5-6:月
  • 7-8:日
  • 10-11:小时
  • 12-13:分钟
  • 14-15:秒
  • 16-17:毫秒(仅限基于 AutoCAD 2016 的产品及更早版本)

以下示例代码显示了一个函数示例,可用于提取 CDATE 返回的值的不同日期和时间部分:

; Returns or outputs the current date and time
; bOutput = T - Output the string to the Command prompt
; bOutput = nil - Return a string in the MM/DD/YYYY  HH:MM:SS format
; Usage: (CurDate T)
; Usage: (CurDate nil)

(defun CurDate (bOuput / cdate_val YYYY M D HH MM SS)
  ; Get the current date/time
  (setq cdate_val (rtos (getvar "CDATE") 2 6))

  ; Break up the string into its separate date and time parts
  (setq YYYY (substr cdate_val 1 4)
        M    (substr cdate_val 5 2)
        D    (substr cdate_val 7 2)
        HH   (substr cdate_val 10 2)
        MM   (substr cdate_val 12 2)
        SS   (substr cdate_val 14 2)
  )

  ; Output the current date and time to the Command
  ; prompt or return the formatted output as a string
  (if bOuput
    (progn
      (prompt (strcat "\nDate: " M "/" D "/" YYYY
                      "\nTime: " HH ":" MM ":" SS
              )
      )
      (princ)
    )
    (strcat M "/" D "/" YYYY "  " HH ":" MM ":" SS)
  )
)

The output or return value of the function will look similar to the following: CurDate

(CurDate T)
Date: 02/14/2017
Time: 14:38:57

(CurDate nil)
"02/14/2017  14:39:04"

Format Modified Julian Date Values

All other date\time related system variables store time in the Modified Julian Date format with the exceptions of CDATE and MILLISECS. The date part of the Modified Julian Date format is the integer part of the number (what is to the left of the decimal) and it represents the number of days since Noon on January 1, 4713 BC, while time is the decimal fraction part of the number (what is to the right of the decimal) and it represents the time that has elapsed since Midnight which can be calculated by multiplying the decimal fraction by 86,400.

The calculations required to convert a value in Modified Julian Date format to something more meaningful can be complex, but there are five date\time related AutoLISP functions in the Express Tools that can be helpful. The date\time related functions are only available after the Julian.lsp file has been loaded into the AutoCAD environment, which can be done by using the DATE command or AutoLISP function. LOAD

These are the date\time related AutoLISP functions that are available after the Julian.lsp file is loaded:

  • CTOJ - Converts calendar date and time to Julian date
  • DTOJ - Converts AutoCAD calendar date/time to Julian date
  • JTOC - Converts Julian date to calendar date list
  • JTOD - Converts Julian date to AutoCAD calendar date/time
  • JTOW - Determines day of the week for a given Julian day

The following are examples of the and functions: JTOCJTOD

Command: (jtoc (getvar "date"))
(2017 2 22 9 21 27.0)

Command: (rtos (jtod (getvar "date")) 2 6)
"20170222.092127"

Calculate Elapsed Time

The calculation of elapsed time can be accomplished by subtracting two different date and time, or just time only values. Based on the precision needed, you can use the value stored in the CDATE or MILLISECS system variables. For very small changes in time, fractions of a second, the value stored in the MILLISECS system variable would be best.

Command: (setq start (getvar "MILLISECS"))
25494432

Command: (setq end (getvar "MILLISECS"))
25499471

Command: (- end start)
5039

The following sample code shows an example of a function that can be used to get the difference between two values that represent different start and end times in milliseconds:

; Takes two values that represent milliseconds and returns
; the difference as an integer/long or double/float
;
; bMilliseconds = T   - Return value as milliseconds
; bMilliseconds = nil - Return value as seconds
;
; Usage: (TimeDiff T1 T2 T) - Milliseconds example
; Usage: (TimeDiff T1 T2 nil) - Seconds example

(defun TimeDiff (nStart nEnd bReturnMilliseconds / nDiff)
  
  ; Get the time difference
  (if (> nStart nEnd)
    (setq nDiff (- nStart nEnd))
    (setq nDiff (- nEnd nStart))
  )

  ; Return the time difference in milliseconds or seconds
  (if (= bReturnMilliseconds nil)
    (/ nDiff 1000.0)
    nDiff
  )
)

函数的返回值可能类似于以下内容:TimeDiff

(setq T1 24092498)
(setq T2 24188267)
(TimeDiff T1 T2 T)
95769

(TimeDiff T1 T2 nil)
95.769

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 16:17

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部