[ENG] STEEM Security update - added power down alert

by joviansummer original STEEMIT post: https://steemit.com/blog/@joviansummer/eng-steem-security-update-added-power-down-alert STEEM Security - receive STEEM alert messages via Discord Hello, this is @joviansummer(witness: @jswit). STEEM Security code has been revised. Previously, STEEM Security was sending alert message via discord for the following 3 kinds of operations: sending STEEM/SBD to another account changing SP delegation account update Now, power down operation has been added. If there is a power down operation, you will also receive alert message. Please check the original post of the above link for detailed information on how to use the service. Thank you for reading, and have a wonderful day! @joviansummer's STEEM projects @jswit witness: I'm running a STEEM witness node. I'd really appreciate it if you vote for my witness account @jswit. ( https://steemitwallet.com/~witnesses ) [ENG] Introducing @jswit witness project @jsup curation: [ENG] Int...

파이썬에서 시간대(timezone) 변경

by joviansummer
original STEEMIT post: https://steemit.com/blog/@joviansummer/timezone


파이썬에서 시간대(timezone)를 변경하는 방법을 간단하게 메모해 둡니다.

datetime 모듈과 dateutil 모듈을 사용해서 현재 시간을 다른 시간대로 변환해서 출력할 수 있습니다.

from datetime import datetime
import pytz

# 현재 시각을 UTC/GMT 기준으로 now에 할당
now = datetime.utcnow()

# 현재 시각을 지역 시간대 기준으로 now_local에 할당
now_local = datetime.now()

datetime 객체인 now에 저장된 시간 정보를 원하는 형식에 맞춰서 문자열로 출력하기 위해 strftime() 함수를 이용합니다.

# 현재 시각 출력 (UTC/GMT)
now.strftime("%Y-%m-%d %H:%M:%S")

'2021-04-10 13:48:54'

# 현재 시각 출력 (KST)
now_local.strftime("%Y-%m-%d %H:%M:%S")

'2021-04-10 22:48:54'

위의 값은 UTC/GMT 기준이므로, 이것을 한국 시간대로 바꿉니다. 우선 시간대 정보(tzinfo)를 가져와 변수 UTC, KST에 할당합니다.

# 한국 시간대 정보를 KST에 할당
UTC = pytz.timezone('UTC')
KST = pytz.timezone('Asia/Seoul')

now에 할당된 datetime 객체에 시간대가 GMT/UTC라는 정보를 추가합니다.

# now의 시간대 정보를 UTC로 설정하여 now_utc에 할당
now_utc = now.replace(tzinfo=UTC)

now_utc의 시간대가 GMT/UTC라고 명시되었으므로, 이 시간 정보를 다른 시간대 기준으로 환산할 수 있습니다.

# now_utc의 시간대를 한국 시간대로 변환하여 now_kst에 할당
now_kst=now_utc.astimezone(KST)

시간대 정보를 한국 시간대로 변경한 now_kst의 시간 정보를 출력해 봅니다.

now_kst.strftime("%Y-%m-%d %H:%M:%S") '2021-04-10 22:48:54'

Comments

Popular posts from this blog

[ENG] jsup 2.0 - make your upvote great again

jsup/avle 보팅 코드 갱신 - 1일1포스팅 강박에서 벗어나기

Nuitka - 파이썬 스크립트를 바이너리 실행 파일로 변환