LoginSignup
0
0

More than 1 year has passed since last update.

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

Last updated at Posted at 2019-04-02
  • 強引に 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

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0