view service/environment/twilight.py @ 1433:be9b456717bd

round numbers. py3 updates. ws Ignore-this: d1191987ce8070c9700a26b89436478a darcs-hash:284883b8403f0563572a154cca19d5e96a5e5654
author drewp <drewp@bigasterisk.com>
date Sun, 11 Aug 2019 03:25:20 -0700
parents 351292938d7c
children a93fbf0d0daa
line wrap: on
line source

from datetime import time, timedelta, datetime
from dateutil.tz import tzlocal, tzutc

"""
data from http://aa.usno.navy.mil/data/docs/RS_OneYear.php (2013, san francisco)
which I found via http://www.perceptiveautomation.com/userforum/viewtopic.php?t=8627&p=54621
"""
_src = """
       Jan.       Feb.       Mar.       Apr.       May        June       July       Aug.       Sept.      Oct.       Nov.       Dec.  
Day Begin End  Begin End  Begin End  Begin End  Begin End  Begin End  Begin End  Begin End  Begin End  Begin End  Begin End  Begin End
     h m  h m   h m  h m   h m  h m   h m  h m   h m  h m   h m  h m   h m  h m   h m  h m   h m  h m   h m  h m   h m  h m   h m  h m
01  0656 1731  0646 1801  0614 1830  0528 1859  0445 1929  0419 1957  0421 2007  0445 1947  0513 1905  0539 1818  0608 1738  0638 1720
02  0656 1732  0645 1803  0613 1831  0527 1900  0444 1930  0418 1958  0421 2006  0445 1946  0514 1904  0540 1817  0609 1737  0639 1720
03  0656 1733  0644 1804  0612 1832  0525 1901  0443 1931  0418 1958  0422 2006  0446 1945  0515 1902  0541 1815  0610 1736  0640 1720
04  0656 1734  0643 1805  0610 1833  0524 1902  0442 1932  0418 1959  0422 2006  0447 1944  0516 1901  0542 1814  0611 1735  0640 1720
05  0656 1735  0642 1806  0609 1834  0522 1903  0441 1933  0417 2000  0423 2006  0448 1943  0517 1859  0543 1812  0612 1734  0641 1720
06  0656 1735  0641 1807  0607 1835  0520 1904  0440 1934  0417 2000  0423 2005  0449 1941  0518 1857  0544 1811  0613 1733  0642 1720
07  0656 1736  0641 1808  0606 1836  0519 1905  0438 1935  0417 2001  0424 2005  0450 1940  0519 1856  0545 1810  0614 1732  0643 1720
08  0656 1737  0640 1809  0605 1837  0517 1906  0437 1936  0417 2001  0425 2005  0451 1939  0519 1854  0546 1808  0615 1731  0644 1720
09  0656 1738  0639 1810  0603 1838  0516 1907  0436 1937  0416 2002  0425 2004  0452 1938  0520 1853  0547 1807  0616 1731  0644 1720
10  0656 1739  0638 1811  0602 1839  0514 1908  0435 1938  0416 2003  0426 2004  0453 1936  0521 1851  0547 1805  0617 1730  0645 1720
11  0656 1740  0637 1812  0600 1840  0513 1909  0434 1939  0416 2003  0427 2004  0454 1935  0522 1850  0548 1804  0618 1729  0646 1720
12  0656 1741  0635 1813  0559 1841  0511 1910  0433 1940  0416 2003  0427 2003  0455 1934  0523 1848  0549 1802  0619 1728  0647 1721
13  0656 1742  0634 1814  0557 1842  0510 1911  0432 1941  0416 2004  0428 2003  0456 1933  0524 1846  0550 1801  0620 1728  0647 1721
14  0655 1743  0633 1815  0556 1843  0509 1912  0431 1942  0416 2004  0429 2002  0457 1931  0525 1845  0551 1800  0621 1727  0648 1721
15  0655 1744  0632 1816  0554 1844  0507 1913  0430 1943  0416 2005  0430 2001  0458 1930  0526 1843  0552 1758  0622 1726  0649 1721
16  0655 1745  0631 1817  0553 1844  0506 1914  0429 1944  0416 2005  0431 2001  0459 1929  0526 1842  0553 1757  0623 1726  0649 1722
17  0655 1746  0630 1818  0551 1845  0504 1915  0428 1944  0416 2005  0431 2000  0500 1927  0527 1840  0554 1756  0624 1725  0650 1722
18  0654 1747  0629 1819  0550 1846  0503 1916  0428 1945  0416 2006  0432 1959  0500 1926  0528 1839  0555 1754  0625 1724  0651 1723
19  0654 1748  0627 1820  0548 1847  0501 1917  0427 1946  0416 2006  0433 1959  0501 1924  0529 1837  0556 1753  0626 1724  0651 1723
20  0653 1749  0626 1821  0547 1848  0500 1918  0426 1947  0417 2006  0434 1958  0502 1923  0530 1835  0557 1752  0627 1723  0652 1724
21  0653 1750  0625 1822  0545 1849  0459 1919  0425 1948  0417 2006  0435 1957  0503 1922  0531 1834  0558 1750  0628 1723  0652 1724
22  0652 1751  0624 1823  0544 1850  0457 1920  0424 1949  0417 2007  0436 1956  0504 1920  0532 1832  0559 1749  0629 1722  0653 1725
23  0652 1752  0622 1824  0542 1851  0456 1921  0424 1950  0417 2007  0436 1956  0505 1919  0532 1831  0600 1748  0630 1722  0653 1725
24  0651 1753  0621 1825  0540 1852  0454 1922  0423 1951  0418 2007  0437 1955  0506 1917  0533 1829  0600 1747  0631 1722  0654 1726
25  0651 1754  0620 1826  0539 1853  0453 1923  0422 1952  0418 2007  0438 1954  0507 1916  0534 1828  0601 1746  0632 1721  0654 1726
26  0650 1755  0619 1827  0537 1854  0452 1924  0422 1952  0418 2007  0439 1953  0508 1914  0535 1826  0602 1744  0633 1721  0654 1727
27  0649 1756  0617 1828  0536 1855  0450 1925  0421 1953  0419 2007  0440 1952  0509 1913  0536 1825  0603 1743  0634 1721  0655 1728
28  0649 1757  0616 1829  0534 1856  0449 1926  0421 1954  0419 2007  0441 1951  0510 1911  0537 1823  0604 1742  0635 1721  0655 1728
29  0648 1758  x    x     0533 1857  0448 1927  0420 1955  0420 2007  0442 1950  0511 1910  0538 1821  0605 1741  0636 1720  0655 1729
30  0647 1759  x    x     0531 1858  0447 1928  0420 1955  0420 2007  0443 1949  0511 1908  0539 1820  0606 1740  0637 1720  0656 1730
31  0647 1800  x    x     0530 1859  x    x     0419 1956  x    x     0444 1948  0512 1907  x    x     0607 1739  x    x     0656 1730
"""

_data = {} # mm-dd : (morningtime, eveningtime)
for row in _src.splitlines()[4:]:
    words = row.split()
    day = words[0]
    for month, t1, t2 in zip(range(1,13), words[1::2], words[2::2]):
        if t1 == 'x':
            continue
        _data["%02d-%s" % (month, day)] = time(int(t1[:2]), int(t1[2:])), time(int(t2[:2]), int(t2[2:]))

def isWithinTwilight(t):
    """is the datetime within the civil twilight for san francisco"""
    standardTime = t.astimezone(tzutc()) + timedelta(seconds=-8 * 3600)
    t1, t2 = _data["%02d-%02d" % (standardTime.month, standardTime.day)]
    return not (t1 < standardTime.time() < t2)

if __name__ == '__main__':
    print isWithinTwilight(datetime.now(tzlocal()))