import time
from datetime import datetime,timezone, timedelta
import pytz # $ pip install pytz
from dateutil.parser import parse
# la = pytz.timezone("America/Los_Angeles")
# fmt = '%Y-%m-%d %H:%M:%S %Z%z'
# nowutc = datetime.now(timezone.utc)
# now = datetime.now(la)
# now2 = la.localize(datetime.now())
# now3 = datetime.now()
# print(nowutc.strftime(fmt))
# print(now.strftime(fmt))
# print(now2.strftime(fmt))
# print(now3.strftime(fmt))
#### 得到时区日期 #####
# from pytz import timezone
# tz = timezone("Europe/Paris")
# print(tz._utc_transition_times)
## 得到时区全称
import datetime
import pytz
from babel import Locale
from babel.dates import get_timezone_name, get_timezone
tz = get_timezone('Europe/Berlin')
r=get_timezone_name(tz, locale=Locale.parse('en_US'))
print(r)
##是否夏令时
import pytz
from datetime import datetime
def is_dst(dt=None, timezone="UTC"):
if dt is None:
dt = datetime.utcnow()
timezone = pytz.timezone(timezone)
timezone_aware_date = timezone.localize(dt, is_dst=None)
print(timezone_aware_date.tzinfo._dst)
return timezone_aware_date.tzinfo._dst.seconds != 0
r = is_dst(datetime(2021, 10, 12), timezone="America/New_York")
print(r)
##另一个方法
import re, random, pytz, math
from datetime import datetime
timezone = pytz.timezone('America/Los_Angeles')
timezone_aware_date = timezone.localize(datetime(2021, 11, 7, 0, 59))
dst_timedelta = timezone_aware_date.dst()
print(timezone_aware_date)
r = "True" if dst_timedelta else "False"
print(r)
#一句话判断当前时间是否夏令时
"True" if datetime.now(tz=pytz.timezone('America/Los_Angeles')).dst() else ""
from ephem import *
import math
wpf=Observer()
wpf.lat='40.7142700'#纬度
wpf.lon='-74.0059700'#经度
wpf.elevation=12#海拔
wpf.pressure=0
body={-2:"恒星",-1:"太阳",0:"月亮",1:"水星",2:"金星",4:"火星",5:"木星",6:"土星",7:"天王星",8:"海王星"}
#构造一个天体,短时间一般坐标不变,视需求修改天体的坐标(单位弧度)
ra=10.0*math.pi/180
dec=-1.84*math.pi/180
s=FixedBody()
s._ra=ra
s._dec=dec
s._epoch=wpf.date#这个要和上面给的赤经赤纬的历元一致
#obs:观测者,n:天体编号,dec:天体的赤纬,t:观测时间,num:计算的次数
def pos_time(obs,n,t,num,hoffset=8):
obs.horizon='0.0'
if n==0:
p=Moon()#月球
obs.horizon='-0:34'#原因同太阳
elif n==1:
p=Mercury()#水星
elif n==2:
p=Venus()#金星
elif n==4:
p=Mars()#火星
elif n==5:
p=Jupiter()#木星
elif n==6:
p=Saturn()#土星
elif n==7:
p=Uranus()#天王星
elif n==8:
p=Neptune()#海王星
elif n==-1:
p=Sun()#太阳
obs.horizon='-0:34'#对于太阳,由于大气折射和太阳的视半径,高度角修改为34角秒时升起或者落下,此值视地区而异
else:
p=s
if n in [-1,0,1,2,4,5,6,7,8]:#太阳、月亮和行星
p.compute(t)#ephem库已存在的天体需要先compute才能输出dec
dec=p.dec
else:
dec=p._dec#构造的天体没有.dec属性
obs.date=t
if abs(obs.lat)+abs(dec)>math.pi/2:#天体处于观测者的恒显圈或者恒隐圈
if abs(obs.lat+dec)<math.pi/2:
print("{0}处于观测者的恒隐圈内,所以永远不会升起".format(body[n]))
return
else:
print("{0}处于观测者的恒显圈内,所以永远不会落下".format(body[n]))
for i in range(num):
obs.date=obs.next_transit(p)
d=(Date(obs.date+hoffset/24)).tuple()
p.compute(obs)
print("{0}/{1:02d}/{2:02d}的{7}中天时间:{3:02d}:{4:02d}:{5:05.2f},高度角:{6:05.2f}".format(d[0],d[1],d[2],d[3],d[4],d[5],p.alt*180/math.pi,body[n]))
else:
for i in range(num):
obs.date=obs.next_rising(p)
d=(Date(obs.date+hoffset/24)).tuple()
print("****")
p.compute(obs)#计算方位角和高度角
print("{0}/{1:02d}/{2:02d}的{7}升起时间:{3:02d}:{4:02d}:{5:05.2f},方位角:{6:09.6f}".format(d[0],d[1],d[2],d[3],d[4],d[5],p.az*180/math.pi,body[n]))
obs.date=obs.next_transit(p)
d=(Date(obs.date+hoffset/24)).tuple()
p.compute(obs)
print("{0}/{1:02d}/{2:02d}的{7}中天时间:{3:02d}:{4:02d}:{5:05.2f},高度角:{6:05.2f}".format(d[0],d[1],d[2],d[3],d[4],d[5],p.alt*180/math.pi,body[n]))
obs.date=obs.next_setting(p)
d=(Date(obs.date+hoffset/24)).tuple()
p.compute(obs)
print("{0}/{1:02d}/{2:02d}的{7}落下时间:{3:02d}:{4:02d}:{5:05.2f},方位角:{6:09.6f}".format(d[0],d[1],d[2],d[3],d[4],d[5],p.az*180/math.pi,body[n]))
print("地上时长:{0}分钟".format((wpf.next_setting(p)-wpf.next_rising(p))*24*60))
# t=Date('2020/04/11 13:37:30')#日期时间
t = now()-1
import datetime
import pytz
hoursoffset = datetime.datetime.now(pytz.timezone('America/New_York'))
hoffset = hoursoffset.utcoffset().total_seconds()/60/60
for i in [-1,0,1,2,4,5,6,7,8,-2]:#要计算的天体
pos_time(wpf,i,t,2,hoffset)#求连续2天的升起中天落下时间