파이썬[Python]: datetime - fromutc 메서드

datetime 모듈 - timezone 클래스 - fromutc 메서드(method)


/// 설명

UTC+offset 시간이 반환됩니다.(dt 는 UTC시간이고, dt 의 tzinfo는 원하는 timezone을 사용하시면 됩니다.)

※ 형식
timezone.fromutc(dt)

reference
https://docs.python.org/3/library/datetime.html#module-datetime

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import datetime
 
year = datetime.datetime.utcnow().year
month = datetime.datetime.utcnow().month
day = datetime.datetime.utcnow().day
hour = datetime.datetime.utcnow().hour
minute = datetime.datetime.utcnow().minute
second = datetime.datetime.utcnow().second
microsecond = datetime.datetime.utcnow().microsecond
 
# Korean Standard Time: KST = UTC + 9
test_timezone = datetime.timezone(datetime.timedelta(hours=9), 'KST')
print(test_timezone)  # KST
 
# UTC -> KST(UTC + 9)
test_dt = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute,
                            second=second, microsecond=microsecond, tzinfo=test_timezone)
 
print(test_dt)  # 2021-11-24 04:58:09.620863+09:00
 
test_fromutc = test_timezone.fromutc(test_dt)
print(test_fromutc)  # 2021-11-24 13:58:09.620863+09:00
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수