날짜를 문자열로 변환하기

Lua:
날짜를 문자열로 변환하기

How to (방법)

-- 현재 날짜와 시간 가져오기
local current_time = os.date("*t")

-- 날짜와 시간을 문자열로 변환하기
local date_string = string.format("%04d-%02d-%02d %02d:%02d:%02d", 
                                  current_time.year, current_time.month, current_time.day, 
                                  current_time.hour, current_time.min, current_time.sec)

print(date_string) -- 출력 예: 2023-04-05 15:26:08
-- 간단한 날짜 포맷 (`os.date` 사용)
local date_simple = os.date("%Y-%m-%d")
print(date_simple) -- 출력 예: 2023-04-05

Deep Dive (심층 분석)

Lua에서 날짜를 문자열로 변환하는 기능은 주로 os.date 함수를 사용합니다. 이 함수는 ISO C 함수 strftime을 기반으로 하며, 다양한 포맷 지정자를 통해 원하는 날짜 형식을 얻을 수 있습니다. 이 방법은 1970년대부터 UNIX 시간과 C 표준 라이브러리에서 사용되어 왔습니다. 대안으로는 os.time 함수를 사용하여 시간을 초 단위로 먼저 구한 뒤 이를 다시 문자열로 변환하는 방법도 있습니다. 구현상의 세부 사항에 있어 Lua는 내부적으로 C의 strftime을 활용하므로 Lua의 날짜 및 시간 함수들은 C 표준에 크게 의존합니다.

See Also (함께 보기)