# 4.12.    Date & Time

파이썬은 여러 가지 방법으로 날짜와 시간을 처리할 수 있습니다. 시간 및 달력 모듈은 날짜와 시간을 처리하는데 매우 유용합니다.

#### 3.12.1.     Tick, Tuple이 무엇이지?

시간 간격은 초 단위의 부동 소수점 숫자인데 특정 시간은 1970 년 1 월 1 일 오전 12시 (epoch) 이후 초 단위로 표시됩니다. Python에는 시간에 대한 작업과 표현 간의 변환을 위한 함수를 제공하는 시간 모듈이 있습니다.  time.time () 함수는 1970 년 1 월 1 일 오전 12시부터 현재 시스템 시간을 ticks 단위로 반환합니다.

```python
import time;  # This is required to include time module.
ticks = time.time()
print "Number of ticks since 12:00am, January 1, 1970:", ticks
```

날짜 계산은 tick 을 사용하면 쉽게 계산 할 수 있습니다. 그러나 신기원(epoch) 이전의 날짜는 이 형식으로 표현 될 수 없습니다. 먼 미래의 날짜도 이런 방식으로 표현될 수 없습니다. 컷오프 포인트는  2038년 어떤 시점의 유닉스와 윈도우를 위한 것입니다.

또한 많은 파이썬의 시간 함수는 시간을 9 개의 숫자로 된 튜플 시퀀스로 처리합니다.

| Index | Field            | Values                                    |
| ----- | ---------------- | ----------------------------------------- |
| 0     | 4-digit year     | 2008                                      |
| 1     | Month            | 1 to 12                                   |
| 2     | Day              | 1 to 31                                   |
| 3     | Hour             | 0 to 23                                   |
| 4     | Minute           | 0 to 59                                   |
| 5     | Second           | 0 to 61 (60 or 61 are leap-seconds)       |
| 6     | Day of Week      | 0 to 6 (0 is Monday)                      |
| 7     | Day of year      | 1 to 366 (Julian day)                     |
| 8     | Daylight savings | -1, 0, 1, -1 means library determines DST |

위의 튜플은 C언어의 struct\_time 구조체와 같습니다. 이 구조체에는 다음과 같은 특성이 있습니다.

| Index | Attributes | Values                                    |
| ----- | ---------- | ----------------------------------------- |
| 0     | tm\_year   | 2008                                      |
| 1     | tm\_mon    | 1 to 12                                   |
| 2     | tm\_mday   | 1 to 31                                   |
| 3     | tm\_hour   | 0 to 23                                   |
| 4     | tm\_min    | 0 to 59                                   |
| 5     | tm\_sec    | 0 to 61 (60 or 61 are leap-seconds)       |
| 6     | tm\_wday   | 0 to 6 (0 is Monday)                      |
| 7     | tm\_yday   | 1 to 366 (Julian day)                     |
| 8     | tm\_isdst  | -1, 0, 1, -1 means library determines DST |

#### 3.12.2.     현재 시간 구하기

time.time() 함수에서 구한 부동 소수점 tick 값을 튜플로 변환한 후 시간을 초 단위로 변환하려면 9 개의 항목이 모두 포함 된 시간-튜플을 변환하는 함수 localtime을 사용하면 됩니다

```python
import time  #time module을 위해 필요한 모듈.

 ticks = time.time()
 print("1970년 1월 1일 오전 12시 이후의 Tick 숫자:", ticks)

 localtime = time.localtime(time.time())
 print("Local 현재 시간 :", localtime)
localtime = time.asctime(time.localtime(time.time()))
 print("Local 현재 시간 :", localtime)
 

```

위의 코드를 실행 하면 다음과 같은 결과를 얻을 수 있습니다.

```python
1970년 1월 1일 오전 12시 이후의 Tick 숫자: 1552554699.0884836
Local 현재 시간 : time.struct_time(tm_year=2019, tm_mon=3, tm_mday=14, tm_hour=18, tm_min=11, tm_sec=39, tm_wday=3, tm_yday=73, tm_isdst=0)
Local 현재 시간 : Thu Mar 14 18:16:04 2019
```

요구 사항에 따라 언제든지 형식을 지정할 수 있지만 읽을 수 있는 형식으로 시간을 가져 오는 간단한 방법은 asctime() 함수를 사용하는 것입니다.

```python
import time;
localtime = time.asctime( time.localtime(time.time()) )
print "Local current time :", localtime
```

#### 3.12.3.     캘린더 모듈

캘린더 모듈은 연간 및 월간 캘린더로 표시하는있는 다양한 방법을 제공합니다.

```python
import calendar
cal = calendar.month(2019, 1)
print(cal)
```

```
March 2019
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
```

#### 3.12.4.     date & time Module

Python에서 제공하는 시간에 대한 작업과 표현 간의 변환을 위한 함수를 제공하는 time 모듈이 있습니다. 다음은 사용 가능한 모든 time method 와 attribute 목록입니다.&#x20;

```
time.altzone
time.asctime([tupletime])
time.clock( )
time.ctime([secs])
time.gmtime([secs])
time.localtime([secs])
time.mktime(tupletime)
time.sleep(secs)
time.strftime(fmt[,tupletime])
time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
time.time( )
time.tzset()
time.timezone
time.tzname
```

캘린더 모듈은 주어진 달 또는 연도의 텍스트 달력을 인쇄하는 기능을 포함하여 달력 관련 기능을 제공합니다. 기본적으로 캘린더는 월요일을 첫 번째 요일로, 일요일을 마지막 요일로 사용합니다. 이를 변경하려면 calendar.setfirstweekday() 함수를 사용하면 됩니다.

다음은 캘린더 모듈에서 사용할 수 있는 함수 목록입니다.

```
calendar.calendar(year,w=2,l=1,c=6)
calendar.firstweekday( )
calendar.isleap(year)
calendar.leapdays(y1,y2)
calendar.month(year,month,w=2,l=1)
calendar.monthcalendar(year,month)
calendar.monthrange(year,month)
calendar.prcal(year,w=2,l=1,c=6)
calendar.prmonth(year,month,w=2,l=1)
calendar.setfirstweekday(weekday)
calendar.timegm(tupletime)
calendar.weekday(year,month,day)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sdc-james.gitbook.io/onebook/3./3.11.-date-and-time.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
