美文网首页
[Python] 用Astral库计算&比较日出日落时间

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

作者: simpleasyhowto | 来源:发表于2019-01-27 21:23 被阅读0次

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

相关文章

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

    Astral自带的样例没法直接用,搜索一圈下来只在stackoverflow上找到一个。根据Astral说明,Py...

  • Python:一篇文章掌握Numpy的基本用法

    前言 Numpy是一个开源的Python科学计算库,它是python科学计算库的基础库,许多其他著名的科学计算库如...

  • 科学计算-Numpy

    Numpy Numpy(Number Python):用Python实现的科学计算库包括:1.强大的N维数组对象a...

  • python 计算日出日落

    准备数据 调用js计算 {'rise': '06:18', 'center': '12:02', 'set': '...

  • Python计算日期差

    在hivesql建表的时候比较容易用到需要计算时间差,这个时候可以用python中的date进行计算。具体code...

  • python数据分析之Numpay

    介绍 一个用python实现的科学计算包。包括:1、一个强大的N维数组对象Array;2、比较成熟的(广播)函数库...

  • 科学计算工具-numpy

    简介 Numpy:提供了一个在Python中做科学计算的基础库,重在数值计算,主要用于多维数组(矩阵)处理的库。用...

  • Django网站搭建准备

    Python环境 Python2.7.3(我的网站用的是Python2,现在Python3已经比较成熟了,很多库也...

  • Time 库

    time库是Python中处理时间的标准库 计算机时间的表达 import time提供获取系统时间并格式化...

  • 数据分析课程笔记 - 10 - Numpy

    大家好呀,今天我们学习 Python 的一个科学计算库 NumPy,它是用 Python 做数据分析所必不可少的一...

网友评论

      本文标题:[Python] 用Astral库计算&比较日出日落时间

      本文链接:https://www.haomeiwen.com/subject/msbnjqtx.html