Time

Time

Racket은 에폭(epoch, 1970년 1월 1일 UTC 자정) 이후의 시간과 날짜/시간 변환 함수들을 제공해요. 시스템 시계에 민감한 값과 모노토닉(monotonic) 시계 값을 구분해서 쓰는 게 중요해요.

출처: Racket Reference

본문

(current-seconds) → exact-integer?

에폭(epoch, 1970년 1월 1일 UTC 자정) 이후의 시간을 초 단위로 돌려줘요.

(current-inexact-milliseconds) → real?

에폭 이후의 시간을 밀리초 단위로 돌려줘요. 결과는 밀리초의 분수를 포함할 수 있어요.

> (current-inexact-milliseconds)
1289513737015.418

이 예에서 1289513737015는 밀리초이고 418은 마이크로초예요.

(current-inexact-monotonic-milliseconds) → real?

지정되지 않은 시작 시점 이후의 밀리초 수를 돌려줘요. 시스템 시계에 민감해서 시스템 시계가 조정되면 실제 시간보다 뒤로 물러서거나 더 빨리 진행될 수 있는 current-inexact-milliseconds와 달리, current-inexact-monotonic-milliseconds의 결과는 Racket 프로세스 안에서 항상 실제 시간과 함께 진행돼요. 하지만 프로세스 간에는 결과를 비교할 수 없어요.

> (current-inexact-monotonic-milliseconds)
12772.418

base 패키지 8.1.0.4 버전에서 추가되었어요.

(seconds->date secs-n [local-time?]) → date*?
secs-n : real?
local-time? : any/c = #t

secs-n — 에폭 이후의 초 단위 시간((current-seconds), (file-or-directory-modify-seconds path), 또는 (/ (current-inexact-milliseconds) 1000) 같은 값) — 을 받아 date* 구조체 타입의 인스턴스를 돌려줘요. secs-n은 초의 분수를 포함할 수 있어요. secs-n이 너무 작거나 크면 exn:fail 예외가 일어나요.

local-time?#t이면 결과 date*는 로컬 시간대의 시간을 반영하고, 그렇지 않으면 UTC의 날짜를 반영해요.

(struct date (second minute hour day month year week-day year-day dst? time-zone-offset)
  #:extra-constructor-name make-date
  #:transparent)

second           : (integer-in 0 60)
minute           : (integer-in 0 59)
hour             : (integer-in 0 23)
day              : (integer-in 1 31)
month            : (integer-in 1 12)
year             : exact-integer?
week-day         : (integer-in 0 6)
year-day         : (integer-in 0 365)
dst?             : boolean?
time-zone-offset : exact-integer?

struct

날짜를 나타내요. second 필드는 윤초(leap second)에서만 60에 도달해요. week-day 필드는 일요일이 0, 월요일이 1 등이에요. year-day 필드는 1월 1일이 0, 1월 2일이 1 등이며, year-day 필드는 윤년에서만 365에 도달해요.

dst? 필드는 날짜가 일광절약(daylight-saving) 조정을 반영하면 #t예요. time-zone-offset 필드는 현재 시간대에 대해 UTC(GMT) 동쪽의 초 수(예: 태평양 표준시는 -28800)를 보고하며, 일광절약 조정을 포함해요(예: 태평양 일광시는 -25200). seconds->date가 두 번째 인자로 #f를 받아 날짜 기록을 생성하면, dst?time-zone-offset 필드는 각각 #f0이 돼요.

date 생성자는 dst?에 대해 어떤 값도 받아들이며, #f가 아닌 값을 #t로 변환해요.

time-zone-offset 필드에 생성된 값은 특히 Unix 플랫폼에서 TZ 환경 변수의 값에 민감한 경향이 있어요. 자세한 내용은 시스템 문서(보통 tzset 아래)를 참고하세요.

racket/date 라이브러리도 참고하세요.

(struct date* date (nanosecond time-zone-name)
  #:extra-constructor-name make-date*)

nanosecond     : (integer-in 0 999999999)
time-zone-name : (and/c string? immutable?)

struct

date를 나노초와 시간대 이름으로 확장해요. 시간대 이름은 "MDT", "Mountain Daylight Time", "UTC" 같은 것이에요.

seconds->date가 두 번째 인자로 #f를 받아 date* 기록을 생성하면 time-zone-name 필드는 "UTC"예요.

date* 생성자는 time-zone-name에 대해 가변 문자열을 받아들이고 그것을 불변 문자열로 변환해요.

(current-milliseconds) → exact-integer?

current-inexact-milliseconds와 같지만 fixnum(음수일 수 있음)으로 강제돼요. 결과가 fixnum이므로 32비트 플랫폼에서는 값이 제한된(하지만 상당히 긴) 시간 동안만 증가해요.

(current-process-milliseconds [scope]) → exact-integer?
scope : (or/c #f thread? 'subprocesses) = #f

기본 운영체제에서 사용된 프로세서 시간을 fixnum 밀리초 단위로 돌려줘요. 사용자 시간과 시스템 시간을 모두 포함해요.

scope#f이면 모든 Racket 스레드와 place에 대한 시간을 보고해요.

scope가 스레드이면 결과는 그 스레드가 실행되는 동안의 시간에 한정되지만, 다른 place의 시간을 포함할 수 있어요. 스레드가 다른 스레드들과 많이 동기화할수록 스레드별 프로세서 시간은 덜 정확하게 기록돼요.

scope'subprocesses이면 결과는 모든 place에 걸쳐 알려진 완료된 서브프로세스(Processes 문서 참고) — 그리고 Unix와 Mac OS에서는 서브프로세스의 알려진 완료된 자식 등 — 의 프로세스 시간의 합이에요.

결과의 정밀도는 플랫폼별로 다르고, 결과가 fixnum이므로 32비트 플랫폼에서는 값이 제한된(하지만 상당히 긴) 시간 동안만 증가해요.

base 패키지 6.1.1.4 버전에서 변경됨: 'subprocesses 모드가 추가되었어요.

(current-gc-milliseconds) → exact-integer?

지금까지 Racket의 가비지 컬렉션이 소비한 프로세서 시간을 fixnum 밀리초 단위로 돌려줘요. 이 시간은 (current-process-milliseconds)가 보고하는 시간의 일부이며, 역시 비슷하게 제한돼요.

(time-apply proc lst)
 → list?
   exact-integer?
   exact-integer?
   exact-integer?

proc : procedure?
lst  : list?

프로시저 적용에 대한 타이밍 정보를 수집해요. 네 개의 값이 돌려져요: lst의 인자들에 proc을 적용한 결과(들)를 담은 리스트, 그 결과를 얻는 데 필요한 CPU 시간(밀리초), 그 결과에 필요한 "실제(real)" 밀리초 수, 그리고 가비지 컬렉션에 소비된 CPU 시간(첫 번째 결과에 포함됨)의 밀리초 수.

타이밍 숫자의 신뢰성은 플랫폼에 달려 있어요. 여러 Racket 스레드가 실행 중이면 보고된 시간은 다른 스레드가 수행한 작업을 포함할 수 있어요.

(time body ...+)

syntax

expr(본문)의 평가에 대한 time-apply 스타일 타이밍 정보를 현재 출력 포트에 직접 보고해요. 결과는 마지막 body의 결과예요.

Date Utilities

더 많은 날짜 & 시간 연산은 Gregor: Date and Time 문서나 srfi/19를 참고하세요.

이 절에 문서화된 바인딩들은 racket/date 라이브러리가 제공하며, racket/baseracket이 제공하는 게 아니에요.

(require racket/date) ; package: base
(current-date) → date*?

(seconds->date (* 0.001 (current-inexact-milliseconds)))의 줄임말이에요.

(date->string date [time?]) → string?
date : date?
time? : any/c = #f

날짜를 문자열로 변환해요. time?일 때만 돌려주는 문자열이 시간을 포함해요. date-display-format도 참고하세요.

(date-display-format format) → void?
format : (or/c 'american 'chinese 'german 'indian 'irish 'iso-8601 'rfc2822 'julian)

parameter

날짜 문자열 형식을 결정하는 파라미터예요. 초기 형식은 'american이에요.

(date->seconds date [local-time?]) → exact-integer?
date : date?
local-time? : any/c = #t

플랫폼별 초로 날짜의 표현을 찾아요. 플랫폼이 지정된 날짜를 표현할 수 없으면 exn:fail 예외가 일어나요.

dateweek-day, year-day 필드는 무시돼요. datedst?time-zone-offset 필드도 무시되며, 날짜는 기본적으로 로컬 시간으로 가정되거나 local-time?#f이면 UTC로 가정돼요.

(date*->seconds date [local-time?]) → real?
date : date?
local-time? : any/c = #t

date->seconds와 같지만, datedate* 인스턴스이면 (date*-nanosecond date)에 기반해 초의 분수를 포함할 수 있는 정확한 숫자를 돌려줘요.

(find-seconds second minute hour day month year [local-time?]) → exact-integer?

second         : (integer-in 0 61)
minute         : (integer-in 0 59)
hour           : (integer-in 0 23)
day            : (integer-in 1 31)
month          : (integer-in 1 12)
year           : exact-integer?
local-time?    : any/c = #t

procedure

플랫폼별 초로 날짜의 표현을 찾아요. 인자들은 date 구조체의 필드에 대응하며, 기본적으로 로컬 시간, local-time?#f이면 UTC예요. 플랫폼이 지정된 날짜를 표현할 수 없으면 오류가 신호되고, 그렇지 않으면 정수가 돌려져요.

base 패키지 9.0.0.4 버전에서 변경됨: year에 음수를 허용해요.

(date->julian/scaliger date) → exact-integer?
date : date?

date 구조체(기원전 2099년까지의 그레고리력)를 줄리안(Julian) 날짜 번호로 변환해요. 반환된 값은 엄격한 줄리안 번호가 아니라 계산이 더 쉬운스칼리거(Scaliger) 버전으로, 1만큼 어긋나 있어요.

(julian/scaliger->string date-number) → string?
date-number : exact-integer?

줄리안 번호(스칼리거의 1만큼 어긋난 버전)를 문자열로 변환해요.

(date->julian/scalinger date) → exact-integer?
date : date?
(julian/scalinger->string date-number) → string?
date-number : exact-integer?

date->julian/scaligerjulian/scaliger->string과 같지만, 철자가 틀린 버전이에요.

더 알아보기