[Python] 用Astral库计算&比较日出日落时间

Astral自带的样例没法直接用,搜索一圈下来只在stackoverflow上找到一个。根据Astral说明Python官方文档和刚才提到的实例,以下是新版程序。本程序可判断系统时间和日出日落时间的关系,结合Raspberry Pi的GPIO库应该能做点有用的东西。

程序参数修改的说明

城市名:这三个字替换成单一城市英文名,有4处需要替换;
纬度:所需单一城市的纬度,请自行百度/必应;
经度:所需单一城市的经度。
另注:运行本程序前请自行安装python3,pip,astral。

import datetime
import astral

location_城市名 = astral.Location(('城市名', 'China', 纬度, 经度, 'Asia/Shanghai', 0))
time_sunrise=location_城市名.sunrise(date=datetime.date.today(),local=True)
time_sunrise_update=time_sunrise + datetime.timedelta(hours=0) #提前/延后日出时间量
time_sunrise_new=str(time_sunrise_update)
print(time_sunrise_new)
time_sunset=location_城市名.sunset(date=datetime.date.today(),local=True)

time_sunset_update=time_sunset + datetime.timedelta(hours=0) #提前/延后日落时间量
time_sunset_new=str(time_sunset_update)
print(time_sunset_new)

time_current=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S+08:00")
print(time_current)

if (time_sunrise_new<=time_current<=time_sunset_new):
    print('low')
else:
    print('High')

本实例在Python3.7下测试通过。


Astral库说明文件:https://pypi.org/project/astral/

datetime官方说明:https://docs.python.org/3/library/datetime.html?highlight=date#

GPIO库链接:https://pypi.org/project/RPi.GPIO/

Stackoverflow实例:https://stackoverflow.com/questions/19615350/calculate-sunrise-and-sunset-times-for-a-given-gps-coordinate-within-postgresql/49233018#49233018

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,516评论 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • 推荐使用arrow包进行日期和时间处理,其方法更简单、更容易理解~ 一、模块介绍 datetime模块包含5个类对...
    惑也阅读 4,362评论 0 1
  • 在工业文明、信息文明、智能文明日益发展的今天,我们对天时似乎越来越不太关心了。这是否是正确的人类文明发展之路...
    润树阅读 274评论 1 4
  • 风从北边把我呼唤而来,来到你的城市。我们其实不常联系,偶尔的聊天无非是“吃饭”、“睡觉”、“你在干什么?”一类...
    白芲阅读 296评论 0 0