ts
Emacs의 날짜 및 시간 라이브러리입니다. (ts-year (ts-now))
와 같은 쉬운 접근자를 제공하여 (string-to-number (format-time-string "%Y"))
와 같은 패턴보다 더 편리해지는 것을 목표로 합니다.
성능을 (상당히) 향상시키기 위해 형식이 지정된 날짜 부분은 타임스탬프 객체가 인스턴스화될 때보다는 느리게 계산되며, 계산된 부분은 다시 계산하지 않고 나중에 액세스할 수 있도록 캐시됩니다. 이는 뒤에서는 놀라울 정도로 비용이 많이 드는 불필요한 (string-to-number (format-time-string...
호출을 방지합니다.
현재 날짜의 일부를 가져옵니다.
; ; When the current date is 2018-12-08 23:09:14 -0600:
(ts-year (ts-now)) ; => 2018
(ts-month (ts-now)) ; => 12
(ts-day (ts-now)) ; => 8
(ts-hour (ts-now)) ; => 23
(ts-minute (ts-now)) ; => 9
(ts-second (ts-now)) ; => 14
(ts-tz-offset (ts-now)) ; => "-0600"
(ts-dow (ts-now)) ; => 6
(ts-day-abbr (ts-now)) ; => "Sat"
(ts-day-name (ts-now)) ; => "Saturday"
(ts-month-abbr (ts-now)) ; => "Dec"
(ts-month-name (ts-now)) ; => "December"
(ts-tz-abbr (ts-now)) ; => "CST"
현재 날짜를 증가시킵니다.
; ; By 10 years:
( list :now (ts-format)
:future (ts-format (ts-adjust 'year 10 (ts-now))))
; ;=> ( :now "2018-12-15 22:00:34 -0600"
; ; :future "2028-12-15 22:00:34 -0600")
; ; By 10 years, 2 months, 3 days, 5 hours, and 4 seconds:
( list :now (ts-format)
:future (ts-format
(ts-adjust 'year 10 'month 2 'day 3
'hour 5 'second 4
(ts-now))))
; ;=> ( :now "2018-12-15 22:02:31 -0600"
; ; :future "2029-02-19 03:02:35 -0600")
이틀 전은 무슨 요일이었습니까?
(ts-day-name (ts-dec 'day 2 (ts-now))) ; => "Thursday"
; ; Or, with threading macros:
( thread-last (ts-now) (ts-dec 'day 2 ) ts-day-name) ; => "Thursday"
(-> > (ts-now) (ts-dec 'day 2 ) ts-day-name) ; => "Thursday"
지난 주 이 시간에 대한 타임스탬프를 가져옵니다.
(ts-unix (ts-adjust 'day -7 (ts-now)))
; ;=> 1543728398.0
; ; To confirm that the difference really is 7 days:
( / ( - (ts-unix (ts-now))
(ts-unix (ts-adjust 'day -7 (ts-now))))
86400 )
; ;=> 7.000000567521762
; ; Or human-friendly as a list:
(ts-human-duration
(ts-difference (ts-now)
(ts-dec 'day 7 (ts-now))))
; ;=> (:years 0 :days 7 :hours 0 :minutes 0 :seconds 0)
; ; Or as a string:
(ts-human-format-duration
(ts-difference (ts-now)
(ts-dec 'day 7 (ts-now))))
; ;=> "7 days"
; ; Or confirm by formatting:
( list :now (ts-format)
:last-week (ts-format (ts-dec 'day 7 (ts-now))))
; ;=> ( :now "2018-12-08 23:31:37 -0600"
; ; :last-week "2018-12-01 23:31:37 -0600")
일부 접근자에는 format-time-string
생성자와 유사한 별칭이 있습니다.
(ts-hour (ts-now)) ; => 0
(ts-H (ts-now)) ; => 0
(ts-minute (ts-now)) ; => 56
(ts-min (ts-now)) ; => 56
(ts-M (ts-now)) ; => 56
(ts-second (ts-now)) ; => 38
(ts-sec (ts-now)) ; => 38
(ts-S (ts-now)) ; => 38
(ts-year (ts-now)) ; => 2018
(ts-Y (ts-now)) ; => 2018
(ts-month (ts-now)) ; => 12
(ts-m (ts-now)) ; => 12
(ts-day (ts-now)) ; => 9
(ts-d (ts-now)) ; => 9
문자열을 타임스탬프 객체로 구문 분석하고 형식을 다시 지정합니다.
(ts-format (ts-parse " sat dec 8 2018 12:12:12 " )) ; => "2018-12-08 12:12:12 -0600"
; ; With a threading macro:
(-> > " sat dec 8 2018 12:12:12 "
ts-parse
ts-format) ; ;=> "2018-12-08 12:12:12 -0600"
두 타임스탬프 간의 차이 형식을 지정합니다.
(ts-human-format-duration
(ts-difference (ts-now)
(ts-adjust 'day -400
'hour -2 'minute -1 'second -5
(ts-now))))
; ; => "1 years, 35 days, 2 hours, 1 minutes, 5 seconds"
; ; Abbreviated:
(ts-human-format-duration
(ts-difference (ts-now)
(ts-adjust 'day -400
'hour -2 'minute -1 'second -5
(ts-now)))
'abbr )
; ; => "1y35d2h1m5s"
org-element-context
에서 직접 Org 타임스탬프 요소를 구문 분석하고 현재와 차이점을 찾습니다.
( with-temp-buffer
( org-mode )
( save-excursion
( insert " <2015-09-24 Thu .+1d> " ))
(ts-human-format-duration
(ts-difference (ts-now)
(ts-parse-org-element ( org-element-context )))))
; ;=> "3 years, 308 days, 2 hours, 24 minutes, 21 seconds"
조직 타임스탬프 문자열(반복기가 있음)을 구문 분석하고 연도 및 월 형식을 지정합니다.
; ; Note the use of `format' rather than `concat' , because `ts-year'
; ; returns the year as a number rather than a string.
( let* ((ts (ts-parse-org " <2015-09-24 Thu .+1d> " )))
( format " %s , %s " (ts-month-name ts) (ts-year ts)))
; ;=> "September, 2015"
; ; Or, using dash.el:
(-- > (ts-parse-org " <2015-09-24 Thu .+1d> " )
( format " %s , %s " (ts-month-name it) (ts-year it)))
; ;=> "September, 2015"
; ; Or, if you remember the format specifiers:
(ts-format " %B, %Y " (ts-parse-org " <2015-09-24 Thu .+1d> " ))
; ;=> "September, 2015"
1970년이면 이 날짜가 얼마나 오래 전이었나요?
( let* ((now (ts-now))
(then (ts-apply :year 1970 now)))
( list (ts-format then)
(ts-human-format-duration
(ts-difference now then))))
; ;=> ("1970-08-04 07:07:10 -0500"
; ; "49 years, 12 days")
시대는 얼마나 오래 전에 시작되었는가?
(ts-human-format-duration
(ts-diff (ts-now) (make-ts :unix 0 )))
; ;=> "49 years, 227 days, 12 hours, 12 minutes, 30 seconds"
지난 100년 중 크리스마스가 토요일이었던 때는 언제였습니까?
( let ((ts (ts-parse " 2019-12-25 " ))
(limit ( - (ts-year (ts-now)) 100 )))
( cl-loop while ( >= (ts-year ts) limit)
when ( string= " Saturday " (ts-day-name ts))
collect (ts-year ts)
do (ts-decf (ts-year ts))))
; ;=> (2010 2004 1999 1993 1982 1976 1971 1965 1954 1948 1943 1937 1926 1920)
더 흥미로운 예를 들어보겠습니다. 타임스탬프가 이전 주에 속합니까?
; ; First, define a function to return the range of the previous calendar week.
( defun last-week-range ()
" Return timestamps (BEG . END) spanning the previous calendar week. "
( let* ( ; ; Bind `now' to the current timestamp to ensure all calculations
; ; begin from the same timestamp. (In the unlikely event that
; ; the execution of this code spanned from one day into the next,
; ; that would cause a wrong result.)
(now (ts-now))
; ; We start by calculating the offsets for the beginning and
; ; ending timestamps using the current day of the week. Note
; ; that the `ts-dow' slot uses the "%w" format specifier, which
; ; counts from Sunday to Saturday as a number from 0 to 6.
(adjust-beg-day ( - ( + 7 (ts-dow now))))
(adjust-end-day ( - ( - 7 ( - 6 (ts-dow now)))))
; ; Make beginning/end timestamps based on `now' , with adjusted
; ; day and hour/minute/second values. These functions return
; ; new timestamps, so `now' is unchanged.
(beg ( thread-last now
; ; `ts-adjust' makes relative adjustments to timestamps.
(ts-adjust 'day adjust-beg-day)
; ; `ts-apply' applies absolute values to timestamps.
(ts-apply :hour 0 :minute 0 :second 0 )))
(end ( thread-last now
(ts-adjust 'day adjust-end-day)
(ts-apply :hour 23 :minute 59 :second 59 ))))
( cons beg end)))
(-let* ( ; ; Bind the default format string for `ts-format' , so the
; ; results are easy to understand.
(ts-default-format " %a, %Y-%m-%d %H:%M:%S %z " )
; ; Get the timestamp for 3 days before now.
(check-ts (ts-adjust 'day -3 (ts-now)))
; ; Get the range for the previous week from the function we defined.
((beg . end) (last-week-range)))
( list :last-week-beg (ts-format beg)
:check-ts (ts-format check-ts)
:last-week-end (ts-format end)
:in-range-p (ts-in beg end check-ts)))
; ;=> (:last-week-beg "Sun, 2019-08-04 00:00:00 -0500"
; ; :check-ts "Fri, 2019-08-09 10:00:34 -0500"
; ; :last-week-end "Sat, 2019-08-10 23:59:59 -0500"
; ; :in-range-p t)
ts-B (STRUCT)
ts
struct STRUCT
의 슬롯 "월 이름"에 액세스합니다.ts-H (STRUCT)
ts
구조체 STRUCT
의 슬롯 "시간"에 액세스합니다.ts-M (STRUCT)
ts
struct STRUCT
의 슬롯 "분"에 액세스합니다.ts-S (STRUCT)
ts
struct STRUCT
의 "두 번째" 슬롯에 액세스합니다.ts-Y (STRUCT)
ts
struct STRUCT
의 슬롯 "연도"에 액세스합니다.ts-b (STRUCT)
ts
구조체 STRUCT
의 슬롯 "month-abbr"에 액세스합니다.ts-d (STRUCT)
ts
struct STRUCT
의 슬롯 "day"에 액세스합니다.ts-day (STRUCT)
ts
struct STRUCT
의 슬롯 "day"에 액세스합니다.ts-day-abbr (STRUCT)
ts
struct STRUCT
의 슬롯 "day-abbr"에 액세스합니다.ts-day-name (STRUCT)
ts
struct STRUCT
의 슬롯 "day-name"에 액세스합니다.ts-day-of-month-num (STRUCT)
ts
struct STRUCT
의 슬롯 "day"에 액세스합니다.ts-day-of-week-abbr (STRUCT)
ts
struct STRUCT
의 슬롯 "day-abbr"에 액세스합니다.ts-day-of-week-name (STRUCT)
ts
struct STRUCT
의 슬롯 "day-name"에 액세스합니다.ts-day-of-week-num (STRUCT)
ts
struct STRUCT
의 슬롯 "dow"에 액세스합니다.ts-day-of-year (STRUCT)
ts
struct STRUCT
의 슬롯 "doy"에 액세스합니다.ts-dom (STRUCT)
ts
struct STRUCT
의 슬롯 "day"에 액세스합니다.ts-dow (STRUCT)
ts
struct STRUCT
의 슬롯 "dow"에 액세스합니다.ts-doy (STRUCT)
ts
struct STRUCT
의 슬롯 "doy"에 액세스합니다.ts-hour (STRUCT)
ts
구조체 STRUCT
의 슬롯 "시간"에 액세스합니다.ts-internal (STRUCT)
ts
struct STRUCT
의 "내부" 슬롯에 액세스합니다. 슬롯은 Emacs 내부 시간 값을 나타냅니다(예: current-time
에 의해 반환됨).ts-m (STRUCT)
ts
구조체 STRUCT
의 슬롯 "월"에 액세스합니다.ts-min (STRUCT)
ts
struct STRUCT
의 슬롯 "분"에 액세스합니다.ts-minute (STRUCT)
ts
struct STRUCT
의 슬롯 "분"에 액세스합니다.ts-month (STRUCT)
ts
구조체 STRUCT
의 슬롯 "월"에 액세스합니다.ts-month-abbr (STRUCT)
ts
구조체 STRUCT
의 슬롯 "month-abbr"에 액세스합니다.ts-month-name (STRUCT)
ts
struct STRUCT
의 슬롯 "월 이름"에 액세스합니다.ts-month-num (STRUCT)
ts
구조체 STRUCT
의 슬롯 "월"에 액세스합니다.ts-moy (STRUCT)
ts
구조체 STRUCT
의 슬롯 "월"에 액세스합니다.ts-sec (STRUCT)
ts
struct STRUCT
의 "두 번째" 슬롯에 액세스합니다.ts-second (STRUCT)
ts
struct STRUCT
의 "두 번째" 슬롯에 액세스합니다.ts-tz-abbr (STRUCT)
ts
struct STRUCT
의 슬롯 "tz-abbr"에 액세스합니다.ts-tz-offset (STRUCT)
ts
struct STRUCT
의 슬롯 "tz-offset"에 액세스합니다.ts-unix (STRUCT)
ts
struct STRUCT
의 슬롯 "unix"에 액세스합니다.ts-week (STRUCT)
ts
struct STRUCT
의 슬롯 "woy"에 액세스합니다.ts-week-of-year (STRUCT)
ts
struct STRUCT
의 슬롯 "woy"에 액세스합니다.ts-woy (STRUCT)
ts
struct STRUCT
의 슬롯 "woy"에 액세스합니다.ts-year (STRUCT)
ts
struct STRUCT
의 슬롯 "연도"에 액세스합니다. ts-apply (&rest SLOTS TS)
TS
기반으로 새 타임스탬프를 반환합니다. 타임스탬프 슬롯을 채우고, 지정된 슬롯 값을 덮어쓰고, 새 슬롯 값에서 파생된 Unix 타임스탬프 값으로 새 타임스탬프를 반환합니다. SLOTS
make-ts
에 전달된 것과 같은 교대 키-값 쌍의 목록입니다.ts-adjust (&rest ADJUSTMENTS)
TS
에 ADJUSTMENTS
적용한 새 타임스탬프를 반환합니다. ADJUSTMENTS
이를 조정하는 일련의 SLOTS
와 VALUES
교대로 이루어져야 합니다. 예를 들어, 이 양식은 47시간 후의 새 타임스탬프를 반환합니다. (ts-adjust 'hour -1 'day +2 (ts-now))
타임스탬프 인수는 마지막이므로 스레딩 매크로에 사용하기에 적합합니다.
ts-dec (SLOT VALUE TS)
SLOT
이 VALUE
만큼 감소된 TS
기반으로 새 타임스탬프를 반환합니다. SLOT
키워드가 아닌 일반 기호로 지정해야 합니다.ts-inc (SLOT VALUE TS)
SLOT
이 VALUE
만큼 증가한 TS
기반으로 새 타임스탬프를 반환합니다. SLOT
키워드가 아닌 일반 기호로 지정해야 합니다.ts-update (TS)
TS
반환합니다. 비파괴적. 예를 들어 ts-fill
사용하여 슬롯을 설정한 후에 사용됩니다.파괴적인
ts-adjustf (TS &rest ADJUSTMENTS)
ADJUSTMENTS
적용한 타임스탬프 TS
반환합니다. 이 함수는 TS
에서 setf
호출하므로 파괴적입니다. ADJUSTMENTS
이를 조정하는 일련의 SLOTS
와 VALUES
교대로 이루어져야 합니다. 예를 들어, 이 양식은 타임스탬프를 미래 47시간으로 조정합니다.
(let ((ts (ts-now))) (ts-adjustf ts 'hour -1 'day +2))
ts-decf (PLACE &optional (VALUE 1))
PLACE
VALUE
(기본값 1)만큼 감소시키고 Unix 타임스탬프를 업데이트한 후 PLACE
의 새 값을 반환합니다.ts-incf (PLACE &optional (VALUE 1))
PLACE
VALUE
(기본값 1)만큼 증가시키고 Unix 타임스탬프를 업데이트한 후 PLACE
의 새 값을 반환합니다. ts-in (BEG END TS)
TS
BEG
~ END
범위 내에 있으면 nil이 아닌 값을 반환합니다. 모든 인수는 ts
구조체여야 합니다.ts< (AB)
A
가 타임스탬프 B
보다 작으면 0이 아닌 값을 반환합니다.ts<= (AB)
A
가 <= 타임스탬프 B
인 경우 nil이 아닌 값을 반환합니다.ts= (AB)
A
타임스탬프 B
와 동일한 경우 nil이 아닌 값을 반환합니다. 타임스탬프의 unix
슬롯만 비교합니다. 타임스탬프의 Unix 슬롯은 부동 소수점이며 1초 미만의 차이가 있을 수 있으므로 타임스탬프의 형식화된 부분이 모두 동일하더라도 동일하지 않을 수 있습니다.ts> (AB)
A
가 타임스탬프 B
보다 큰 경우 nil이 아닌 값을 반환합니다.ts>= (AB)
A
가 >= 타임스탬프 B
인 경우 nil이 아닌 값을 반환합니다. ts-human-duration (SECONDS)
SECONDS
기간을 연, 일, 시, 분, 초 단위로 설명하는 plist를 반환합니다. 이는 윤년, 윤초 등을 고려하지 않은 간단한 계산입니다.ts-human-format-duration (SECONDS &optional ABBREVIATE)
SECONDS
기간을 설명하는 인간 형식의 문자열을 반환합니다. SECONDS
1보다 작으면 "0 seconds"
반환합니다. ABBREVIATE
nil이 아닌 경우 공백 없이 더 짧은 버전을 반환합니다. 이는 윤년, 윤초 등을 고려하지 않은 간단한 계산입니다. ts-format (&optional TS-OR-FORMAT-STRING TS)
format-time-string
사용하여 타임스탬프 형식을 지정합니다. TS-OR-FORMAT-STRING
이 타임스탬프이거나 nil인 경우 ts-default-format
값을 사용하세요. TS-OR-FORMAT-STRING
과 TS
모두 nil이면 현재 시간을 사용합니다. ts-parse (STRING)
parse-time-string
으로 STRING
구문 분석하여 새로운 ts
구조체를 반환합니다.ts-parse-fill (FILL STRING)
parse-time-string
으로 STRING
구문 분석하여 새로운 ts
구조체를 반환합니다. 빈 시간/분/초 값은 FILL
에 따라 채워집니다. begin
경우 0으로; end
인 경우 시간은 23으로 채워지고 분/초는 59로 채워집니다. nil인 경우 시간 값이 비어 있으면 오류가 표시될 수 있습니다. FILL
이 end
인 경우 "12:12"와 같은 시간 값은 "12:12:59"가 아닌 "12:12:00"으로 채워집니다.ts-parse-org (ORG-TS-STRING)
ORG-TS-STRING
에 대한 타임스탬프 개체를 반환합니다. org-parse-time-string
함수가 호출되며 이 함수를 호출하기 전에 로드되어야 합니다.ts-parse-org-fill (FILL ORG-TS-STRING)
ORG-TS-STRING
에 대한 타임스탬프 개체를 반환합니다. org-parse-time-string
함수가 호출되며 이 함수를 호출하기 전에 로드되어야 합니다. 시/분/초 값은 FILL
에 따라 채워집니다. begin
하면 0으로, 그렇지 않으면 0으로 채워집니다. end
인 경우 시간은 23으로 채워지고 분/초는 59로 채워집니다 org-parse-time-string
초를 포함하는 타임스탬프를 지원하지 않습니다.ts-parse-org-element (ELEMENT)
ELEMENT
에 대한 타임스탬프 개체를 반환합니다. 요소는 org-element
에 의해 구문 분석된 요소와 같아야 하며, 첫 번째 요소는 timestamp
입니다. 타임스탬프가 범위가 아니라고 가정합니다. copy-ts (TS)
TS
의 복사본을 반환합니다.ts-difference (AB)
A
와 B
사이의 차이(초)를 반환합니다.ts-diff
ts-difference
의 별칭입니다.ts-fill (TS &optional ZONE)
TS
반환합니다. 이는 비파괴적입니다. ZONE
format-time-string
으로 전달됩니다.ts-now
ts
구조체를 반환합니다.ts-p (STRUCT)
ts-reset (TS)
unix
제외한 모든 슬롯이 지워진 TS
반환합니다. 비파괴적. 다음과 동일: (make-ts :unix (ts-unix ts))
ts-defstruct (&rest ARGS)
cl-defstruct
와 비슷하지만 추가 슬롯 옵션이 있습니다.추가 슬롯 옵션 및 값:
:accessor-init
: 슬롯이 nil인 경우 접근자의 슬롯을 초기화하는 sexp입니다. 기호 struct
현재 구조체에 바인딩됩니다. 접근자는 구조체가 완전히 정의된 후에 정의되므로 구조체 정의를 참조할 수 있습니다(예: cl-struct
pcase
매크로 사용).
:aliases
: 구조체 이름이 앞에 추가된 슬롯 접근자에게 별칭이 지정될 기호 A
입니다(예: 슬롯 year
및 별칭 y
있는 구조체 ts
별칭 ts-y
생성합니다).
ts-human-format-duration
대 format-seconds
Emacs에는 ts-human-format-duration
과 유사한 출력을 생성하는 내장 함수 format-seconds
있습니다. 출력은 형식 문자열을 사용하여 제어할 수도 있습니다. 그러나 ts-human-format-duration
의 출력이 충분하면 format-seconds
보다 훨씬 더 나은 성능을 발휘합니다. 100,000번 실행된 이 간단한 벤치마크는 훨씬 더 빠르게 실행되고 더 적은 쓰레기를 생성한다는 것을 보여줍니다.
(bench-multi-lexical :times 100000
:forms (( " ts-human-format-duration " (ts-human-format-duration 15780.910933971405 t ))
( " format-seconds " ( format-seconds " %yy%dd%hh%mm%ss%z " 15780.910933971405 ))))
형태 | x 다음보다 빠르다 | 총 런타임 | GC 수 | 총 GC 런타임 |
---|---|---|---|---|
ts-사람-형식-기간 | 5.82 | 0.832945 | 3 | 0.574929 |
형식-초 | 가장 느린 | 4.848253 | 17 | 3.288799 |
( bench-multi-lexical
매크로에 대해서는 Emacs 패키지 개발자 핸드북을 참조하십시오.)
추가됨
ts-fill
함수는 format-time-string
에 전달된 것과 같은 ZONE
인수를 허용합니다. 결정된
ts-human-format-duration
1초 미만의 기간 동안 빈 문자열을 반환했습니다(이제 "0 seconds"
반환함). 추가됨
ts-parse-fill
및 ts-parse-org-fill
.ts-in
.변경됨
ts-now
함수는 더 이상 인라인되지 않습니다. 이를 통해 런타임 시 테스트에 도움이 되는 cl-letf
등을 사용하여 변경할 수 있습니다.결정된
ts-fill
에 일치 데이터를 저장합니다. (호출되는 함수 split-string
일치 데이터를 수정합니다.)ts-parse-org
에 일치 데이터를 저장합니다. (호출되는 org-parse-time-string
함수는 일치 데이터를 수정합니다.)선적 서류 비치
첫 번째 태그 릴리스. MELPA에 게시되었습니다.
GPLv3