【问题标题】:Get UTC DateTime using AutoLISP使用 AutoLISP 获取 UTC 日期时间
【发布时间】:2017-03-27 02:50:16
【问题描述】:

在 AutoLISP 中,我发现了两种调用日期的方法CDATEDATE。但是我需要获取 UTC 日期时间。我一直在寻找它的功能,但找不到任何功能。

有没有办法获取 UTC 日期时间?

是否可以手动减去或增加当前时间?

非常感谢!

【问题讨论】:

    标签: datetime autocad autocad-plugin autolisp


    【解决方案1】:

    您可以使用以下函数从 NIST 互联网时间服务器检索 UTC 日期时间:

    ;; Internet Time  -  Lee Mac
    ;; Returns the date and/or UTC time as a string in the format specified.
    ;; Data is sourced from a NIST Internet Time Server.
    ;; 
    ;; The format argument may use the following identifiers to represent date & time quantities:
    ;; YYYY = 4-digit year
    ;; YY   = Year
    ;; MO   = Month
    ;; DD   = Day
    ;; HH   = Hour
    ;; MM   = Minutes
    ;; SS   = Seconds
    
    (defun LM:internettime ( format / result rgx server xml )
        (setq server "http://time.nist.gov:13")
        (setq result
            (vl-catch-all-apply
                (function
                    (lambda ( / str )
                        (setq xml (vlax-create-object "msxml2.xmlhttp.3.0"))
                        (setq rgx (vlax-create-object "vbscript.regexp"))
                        (vlax-invoke-method xml 'open "POST" server :vlax-false)
                        (vlax-invoke-method xml 'send)
                        (if (setq str (vlax-get-property xml 'responsetext))
                            (progn
                                (vlax-put-property rgx 'global     actrue)
                                (vlax-put-property rgx 'ignorecase actrue)
                                (vlax-put-property rgx 'multiline  actrue)
                                (setq str (strcat " " (itoa (jtoy (+ (atoi (substr str 2 5)) 2400000.5))) (substr str 7)))
                                (mapcar
                                    (function
                                        (lambda ( a b )
                                            (vlax-put-property rgx 'pattern a)
                                            (setq format (vlax-invoke rgx 'replace format b))
                                        )
                                    )
                                   '("YYYY" "YY" "MO" "DD" "HH" "MM" "SS")
                                   '( "$1"  "$2" "$3" "$4" "$5" "$6" "$7")
                                )
                                (vlax-put-property rgx 'pattern
                                    (strcat
                                        "(?:[^\\d]+)([\\d]+)(?:[^\\d]+)([\\d]+)"
                                        "(?:[^\\d]+)([\\d]+)(?:[^\\d]+)([\\d]+)"
                                        "(?:[^\\d]+)([\\d]+)(?:[^\\d]+)([\\d]+)"
                                        "(?:[^\\d]+)([\\d]+)(?:.+)\\n"
                                    )
                                )
                                (vlax-invoke-method rgx 'replace str format)
                            )
                        )
                    )
                )
            )
        )
        (if xml  (vlax-release-object xml))
        (if rgx  (vlax-release-object rgx))
        (if (not (vl-catch-all-error-p result)) result)
    )
    
    ;; Julian Date to Calendar Year
    ;; Algorithm from: Meeus, Jean.  Astronomical Algorithms.
    
    (defun jtoy ( j / a b c d )
        (setq j (fix j)
              a (fix (/ (- j 1867216.25) 36524.25))
              b (+ (- (+ j 1 a) (fix (/ a 4))) 1524)
              c (fix (/ (- b 122.1) 365.25))
              d (fix (/ (- b (fix (* 365.25 c))) 30.6001))
        )
        (fix (- c (if (< 2 (fix (if (< d 14) (1- d) (- d 13)))) 4716 4715)))
    )
    

    这是一个例子:

    _$ (LM:internettime "YYYY-MO-DD HH:MM:SS")
    "2018-01-07 11:34:24"
    

    【讨论】:

      【解决方案2】:

      您可以使用 vlax-create-object 调用 PowerShell 来执行:(Get-Date).ToUniversalTime()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-09
        • 1970-01-01
        • 1970-01-01
        • 2020-06-02
        • 1970-01-01
        • 1970-01-01
        • 2019-07-28
        • 1970-01-01
        相关资源
        最近更新 更多