--2016.12.12
4. Corrected the def argument set to date = datetime.datetime.today () in the script.
If this is the case, the default will be the time when def was loaded once. Because it becomes.
――It is tedious to manually switch and judge whether the world's major stock exchanges are this summer time. Can it be automated?
――I'm trying to find out more about the time zone.
-Although it is mostly explained in [Python] Convert time zone, I wonder if you can make it a check function so that you do not have to worry every time.
--python3.x (probably 2.7 is ok)
--Required packages -pytz
--Pytz's dst (summer time? Is like Japanese English, like daylight saving time) is 0 to determine whether it is daylight saving time.
――Do you set the date and time to judge daylight saving time in local time? Is it judged by Japan time? Is a point of concern, so I can check either one.
--Since the time when switching daylight saving time (time) is delicate, please refer to "5. Precautions for checking daylight saving time".
――The summer time can be checked in the United States (New York), United Kingdom (London), Canada (Toronto), Brazil (Sao Paulo), Australia (Sydney), Germany (Frankfurt * Frankfurt, Hessen), so Berlin ), But I think we can handle it anywhere by adding a time zone.
You can check it by embedding the following script.
--calcdifftime: Time difference from the specified time zone of the specified time (Japan if not specified) This must be necessary.
--checkdst: Checks whether the time zone of the designated securities company is daylight saving time at the specified time (specified time zone (Japan if not specified))
Please refer to 5. Verification for specific usage.
import pytz ,datetime
#Add from here if necessary.
tzUTC = pytz.timezone("UTC") #Standard time
tzNYC = pytz.timezone("America/New_York") #USA(New York)
tzTYO = pytz.timezone("Asia/Tokyo") #Japan(Tokyo)
tzLON = pytz.timezone("Europe/London") #England(London)
tzYTO = pytz.timezone("America/Toronto") #Canada(Toronto)
tzSAO = pytz.timezone("America/Sao_Paulo") #Brazil(Sao paulo)
tzSYD = pytz.timezone("Australia/Sydney") #Australia(Sydney)
tzFRA = pytz.timezone("Europe/Berlin") #Germany(Frankfurt) *Frankfurt、ヘッセン州がなかったのでベルリン
#So far
def calcdifftime(tz = tzNYC, date = -1,tzbase = -1):
"""
[param]
tz : checked pytz.timezone
date:checked datetime
tzbase:bassis pytz.timezone
[return]
float:different hours
"""
if(date==-1):
date = datetime.datetime.today()
if(tzbase==-1):
tzbase = tzTYO
tabse = tzbase.localize(date)
tztime = tz.localize(date)
diff = tabse - tztime
diff_hours = diff.total_seconds()/3600
return(diff_hours)
def checkdst(tz = tzNYC, date=-1,tzbase=-1):
"""
[param]
tz : checked pytz.timezone
date:checked datetime
tzbase:bassis pytz.timezone
[return]
bool: True:dst , False:std
"""
if(date==-1):
date = datetime.datetime.today()
if(tzbase==-1):
tzbase = tzTYO
tbase = tzbase.localize(date)
chgdate = tbase.astimezone(tz)
#print(chgdate)
if(chgdate.dst().seconds !=0):
return(True)
else:
return(False)
For the time being, we are checking the start and end times of daylight saving time in the United States (US) in Japan time and local time.
def tztest(t,tz,tzbase = -1):
diff = calcdifftime(tz,t) #Time difference with Japan
if(tzbase == -1): #When tzbase is not specified(In Japan time)Operation test
if(checkdst(tz,t)):
st = "daylight saving time"
else:
st = "standard time"
elif(checkdst(tz,t,tzbase)): #Test when tzbase is specified (for basic local time)
st = "daylight saving time"
else:
st = "standard time"
if(tzbase == -1):
tzbasename = tzTYO.zone
else:
tzbasename = tzbase.zone
print("Check time ",t,tzbasename)
print("tz name".ljust(20),"Time diff".ljust(10),"DST STD")
print(tz.zone.ljust(20),str(diff).ljust(10),st)
print("Check in Japan time")
#US (US) Daylight Saving Time Check
#Check time 2016-03-13 16:00:00 Asia/Tokyo
#tz name Time diff DST STD
#America/New_York -13.0 daylight saving time
t = datetime.datetime(2016, 3, 13, 16, 0)
tztest(t,tzNYC)
#Normal check in the United States (US)
#Check time 2016-11-06 15:00:00 Asia/Tokyo
#tz name Time diff DST STD
#America/New_York -14.0 standard time
t = datetime.datetime(2016, 11, 6, 15, 0)
tztest(t,tzNYC)
print("Check in local time")
#US (US) Daylight Saving Time Check
#Check time 2016-03-13 03:00:00 America/New_York
#tz name Time diff DST STD
#America/New_York -13.0 daylight saving time
t = datetime.datetime(2016, 3, 13, 3, 0)
tztest(t,tzNYC,tzNYC)
#Normal check in the United States (US)
#Check time 2016-11-06 01:00:00 America/New_York
#tz name Time diff DST STD
#America/New_York -14.0 standard time
t = datetime.datetime(2016, 11, 6, 1, 0)
tztest(t,tzNYC,tzNYC)
――The daylight saving time in the United States (US) changes at 2:00 on March 13, 2016, so what about daylight saving time from here? However, please note that it will be possible to judge daylight saving time at 3:00 (one hour ahead) on March 13, 2016, when it actually started.
It will return to standard time at 2:00 on November 6, 2016, but it seems that daylight saving time can be judged until 1:00 on November 6, 2016 (one hour back). (1 o'clock on November 6, 2016 is judged to be standard time)
――Since Germany and other countries are similar, it seems that you need to be careful about the time to check.
――Strictly speaking, there are some strange (uncheckable) times, but you may not have to worry about the start and end times of daylight saving time because the market is closed at midnight on Sunday. However, be careful when checking once a week (such as Sunday).
――This time, I referred to the name of a certain city of a securities company in each country. What should you do with the abbreviation? Appropriate? ?? I used the abbreviation for the local city, but at that time, the city / airport code might be cheaper to use. I am referring to it.
-[Python] Convert Time Zone
-[[python] Time zone list in pytz module](http://komaken.me/blog/2013/02/15/pythonpytz%E3%83%A2%E3%82%B8%E3%83%A5% E3% 83% BC% E3% 83% AB% E3% 81% A7% E3% 81% AE% E3% 82% BF% E3% 82% A4% E3% 83% A0% E3% 82% BE% E3% 83% BC% E3% 83% B3% E4% B8% 80% E8% A6% A7 /)
-[List of Stock Exchanges](https://ja.wikipedia.org/wiki/%E8%A8%BC%E5%88%B8%E5%8F%96%E5%BC%95%E6%89% 80% E3% 81% AE% E4% B8% 80% E8% A6% A7)
-City / Airport Code Search
Recommended Posts