jsup/avle code update - you don't have to write post everyday

by joviansummer original STEEMIT post: https://steemit.com/blog/@joviansummer/jsup-avle-code-update-you-don-t-have-to-write-post-everyday Hello, this is @joviansummer, developer of @jsup and@avle voting service. Voting service code for @jsup and @avle has been updated. I had been considering this update for a while, and recently I could come up with detailed implementation plan which resulted in a relatively swift code revision. The new added feature is capability to give daily upvote to the first thing you write for the day(timezone GMT+9) regardless of post or comment/reply. Writing a post everyday can be challenging. In that case, you can write a reply to receive daily upvote because writing a reply everyday is much easier and also a good way to engage in your community. Writing a post everyday just to get an upvote can be very tedious and exhausting, and post itself could easily become low-quality. There may be people who enjoy writing a post everyday, but we can't ignore th...

파이썬에서 시간대(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

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

[ENG] jsup 2.0 - make your upvote great again

[EN] STEEM-services: dapps/services webpage with sort and search functions