Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#python で日付を JST=日本時刻=現地時刻で与えると日の始まりの unixtimstamp を出力する例 #1031

Open
YumaInaura opened this issue Apr 2, 2019 · 0 comments

Comments

@YumaInaura
Copy link
Owner

YumaInaura commented Apr 2, 2019

  • 強引に string の日付を split して replace に与えています
#!/usr/bin/env python3

from datetime import datetime
import pytz, time, os

tz = pytz.timezone('Asia/Tokyo')

date = os.environ.get('DATE')

year = int(date.split('-')[0])
month = int(date.split('-')[1])
day = int(date.split('-')[2])

local_datetime = datetime.now(tz=tz).replace(year=year, month=month, day=day, hour=0, minute=0, second=0, microsecond=0)

unixtimestamp = time.mktime(local_datetime.timetuple())

print(unixtimestamp)
$ DATE=2016-03-02 ./jst-unixtimestamp.py
1456844400.0
$ DATE=2016-03-03 ./jst-unixtimestamp.py
1456930800.0

shellと組み合わせて使ってみる例

$ DATE=$(date --date="1 days ago" +'%Y-%m-%d') ./jst-unixtimestamp.py
1554044400.0
$ DATE=$(date +'%Y-%m-%d') ./jst-unixtimestamp.py
1554130800.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant