<bdo id='4HGng'></bdo><ul id='4HGng'></ul>

      <small id='4HGng'></small><noframes id='4HGng'>

    1. <tfoot id='4HGng'></tfoot>
      <i id='4HGng'><tr id='4HGng'><dt id='4HGng'><q id='4HGng'><span id='4HGng'><b id='4HGng'><form id='4HGng'><ins id='4HGng'></ins><ul id='4HGng'></ul><sub id='4HGng'></sub></form><legend id='4HGng'></legend><bdo id='4HGng'><pre id='4HGng'><center id='4HGng'></center></pre></bdo></b><th id='4HGng'></th></span></q></dt></tr></i><div id='4HGng'><tfoot id='4HGng'></tfoot><dl id='4HGng'><fieldset id='4HGng'></fieldset></dl></div>

    2. <legend id='4HGng'><style id='4HGng'><dir id='4HGng'><q id='4HGng'></q></dir></style></legend>

      Javascript 或 Python - 我如何判断是白天还是黑夜?

      时间:2023-09-06
        <tbody id='l4XDi'></tbody>
      <i id='l4XDi'><tr id='l4XDi'><dt id='l4XDi'><q id='l4XDi'><span id='l4XDi'><b id='l4XDi'><form id='l4XDi'><ins id='l4XDi'></ins><ul id='l4XDi'></ul><sub id='l4XDi'></sub></form><legend id='l4XDi'></legend><bdo id='l4XDi'><pre id='l4XDi'><center id='l4XDi'></center></pre></bdo></b><th id='l4XDi'></th></span></q></dt></tr></i><div id='l4XDi'><tfoot id='l4XDi'></tfoot><dl id='l4XDi'><fieldset id='l4XDi'></fieldset></dl></div>

              <bdo id='l4XDi'></bdo><ul id='l4XDi'></ul>
            • <legend id='l4XDi'><style id='l4XDi'><dir id='l4XDi'><q id='l4XDi'></q></dir></style></legend>

                <tfoot id='l4XDi'></tfoot>
              1. <small id='l4XDi'></small><noframes id='l4XDi'>

              2. 本文介绍了Javascript 或 Python - 我如何判断是白天还是黑夜?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                知道如何根据用户的时间和位置确定当前是夜晚/白天还是日出/黎明?

                我没有发现任何可以在客户端或后端使用的有用的东西.

                I haven't found anything useful that I could use within either the client or backend.

                棘手的是,小时不一定定义是白天还是黑夜,这在很大程度上取决于年、月、日、小时和地理坐标.

                What makes it tricky is the hour doesn't necessarily define if it is night and day, this depends pretty much on the year, month, day, hour and geo coordinates.

                为了澄清......模仿这样的东西.

                For clarification... to emulate something like this.

                一种近似此值的方法也非常有用.

                希望有人能帮忙!

                推荐答案

                Python 3 用户注意:我刚刚使用 Python 3.8.6 尝试了下面的代码,它也适用于该版本.我必须将 print 语句转换为 print() 函数调用,但仅此而已.

                Note to Python 3 users: I just tried the code below using Python 3.8.6 and it works in that version, too. I had to convert the print statements into print() function calls, but that was all.

                我注意到 tzinfo 类的示例实现与 Python 2 文档中的不同,但使用 tzinfo 类tzinfo_example.py 文件.org/3/library/datetime.html#tzinfo-objects" rel="nofollow noreferrer">最新文档 运行良好(但旧的 2.x 版本也是如此).

                I noticed that the example implementation of tzinfo classes is different from what's in the Python 2 documentation, but using the example tzinfo classes in the tzinfo_example.py file referenced in the latest documentation worked just fine (but so did the older 2.x versions).

                您可以从这里下载下面显示的 sunriseset.py 文件的 Python 3 版本.

                You can download a Python 3 version of the sunriseset.py file shown below from here.

                你可以像我一样使用这个公共域Sun.py 模块,用于计算太阳相对于地球位置的位置.(警告:它包含制表符,并假定制表符每 8 个字符.)它已经很老了,但多年来一直对我很好.我对其进行了一些表面上的修改以使其与 Python 2.7 保持同步,例如将其中的几个类设为新样式,但大部分都没有改变.

                You can do as I did and use this public domain Sun.py module to compute the position of the sun relative to positions on the Earth. (Warning: it contains tab characters and assumes tabs are every 8 characters.) It's pretty old, but has continued to work well for me for many years. I made a few superficial modifications to it to be more up-to-date with Python 2.7, such as making the few classes in it new-style, but for the most part it's unchanged.

                这是我创建的一个模块,名为 sunriseset.py,它展示了如何使用它来计算给定地理坐标和时区的特定位置的日出和日落时间.引用的 timezone 模块是 tzinfo 抽象基类在 datetime 模块文档中描述library/datetime.html?highlight=tzinfo#tzinfo-objects" rel="nofollow noreferrer">tzinfoobjects.

                Here's one module I created, called sunriseset.py, which shows how to use it to calculate the sunrise and sunset times for a specific location given its geographic coordinates and timezone. The referenced timezone module is an implementation of the tzinfo abstract base class described in the datetime module's documentation on tzinfoobjects.

                # -*- coding: iso-8859-1 -*-
                import datetime
                import timezone  # concrete tzinfo subclass based on the Python docs
                import math
                from Sun import Sun
                
                __all__ = ['getsuninfo', 'Place']
                
                class Place(object):
                    def __init__(self, name, coords, tz=timezone.Pacific):
                        self.name = name        # string
                        self.coords = coords    # tuple (E/W long, N/S lat)
                        self.tz = tz            # tzinfo constant
                
                def _hoursmins(hours):
                    """Convert floating point decimal time in hours to integer hrs,mins"""
                    frac,h = math.modf(hours)
                    m = round(frac*60, 0)
                    if m == 60: # rounded up to next hour
                        h += 1; m = 0
                    return int(h),int(m)
                
                def _ymd(date):
                    """Return y,m,d from datetime object as tuple"""
                    return date.timetuple()[:3]
                
                def getsuninfo(location, date=None):
                    """Return local datetime of sunrise, sunset, and length of day in hrs,mins)"""
                    if date == None:
                        querydate = datetime.date.today()
                    else: # date given should be datetime instance
                        querydate = date
                
                    args = _ymd(querydate) + location.coords
                    utcrise, utcset = Sun().sunRiseSet(*args)
                    daylength = Sun().dayLength(*args)
                    hrs,mins = _hoursmins(daylength)
                
                    risehour, risemin = _hoursmins(utcrise)
                    sethour, setmin   = _hoursmins(utcset)
                
                    # convert times to timedelta values (ie from midnight utc of the date)
                    midnight = datetime.datetime(tzinfo=timezone.utc, *_ymd(querydate))
                    deltarise = datetime.timedelta(hours=risehour, minutes=risemin)
                    utcdatetimerise = midnight+deltarise
                    deltaset = datetime.timedelta(hours=sethour, minutes=setmin)
                    utcdatetimeset  = midnight+deltaset
                
                    # convert results from UTC time to local time of location
                    localrise = utcdatetimerise.astimezone(location.tz)
                    localset  = utcdatetimeset.astimezone(location.tz)
                
                    return localrise, localset, hrs, mins
                
                if __name__ == "__main__":
                    import datetime, timezone
                
                    def unittest(location, testdate):
                        risetime, settime, hrs, mins = getsuninfo(location, testdate)
                
                        print "Location:", location.name
                        print "Date:", testdate.strftime("%a %x")
                        print risetime.strftime("Sunrise %I:%M %p"), settime.strftime("- Sunset %I:%M %p (%Z)")
                        print "daylight: %d:%02d" % (hrs,mins)
                        print
                
                    place = Place("My House", (-121.990278, 47.204444), timezone.Pacific)
                
                    # test dates just before and after DST transitions
                    print "pre 2007"
                    print "========="
                    unittest(place, datetime.date(2006, 4, 1))
                    unittest(place, datetime.date(2006, 4, 2))
                    unittest(place, datetime.date(2006, 10, 28))
                    unittest(place, datetime.date(2006, 10, 29))
                
                    print "2007"
                    print "========="
                    unittest(place, datetime.date(2007, 3, 10))
                    unittest(place, datetime.date(2007, 3, 11))
                    unittest(place, datetime.date(2007, 11, 3))
                    unittest(place, datetime.date(2007, 11, 4))
                

                这篇关于Javascript 或 Python - 我如何判断是白天还是黑夜?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:通过 html5 和 javascript 获取用户地理位置 下一篇:html 导航器“用户拒绝地理位置"

                相关文章

                最新文章

                  <bdo id='oSeSl'></bdo><ul id='oSeSl'></ul>
                <legend id='oSeSl'><style id='oSeSl'><dir id='oSeSl'><q id='oSeSl'></q></dir></style></legend>

                1. <i id='oSeSl'><tr id='oSeSl'><dt id='oSeSl'><q id='oSeSl'><span id='oSeSl'><b id='oSeSl'><form id='oSeSl'><ins id='oSeSl'></ins><ul id='oSeSl'></ul><sub id='oSeSl'></sub></form><legend id='oSeSl'></legend><bdo id='oSeSl'><pre id='oSeSl'><center id='oSeSl'></center></pre></bdo></b><th id='oSeSl'></th></span></q></dt></tr></i><div id='oSeSl'><tfoot id='oSeSl'></tfoot><dl id='oSeSl'><fieldset id='oSeSl'></fieldset></dl></div>

                2. <tfoot id='oSeSl'></tfoot>

                  <small id='oSeSl'></small><noframes id='oSeSl'>