Mercurial > code > home > repos > homeauto
comparison service/environment/twilight.py @ 71:5c9927555df6
twilight computation for rules to use
Ignore-this: c40016acb3c3f957cb17d2b918698c77
author | drewp@bigasterisk.com |
---|---|
date | Wed, 10 Apr 2013 21:32:21 -0700 |
parents | |
children | a93fbf0d0daa |
comparison
equal
deleted
inserted
replaced
70:acb00897f54a | 71:5c9927555df6 |
---|---|
1 from datetime import time, timedelta, datetime | |
2 from dateutil.tz import tzlocal, tzutc | |
3 | |
4 """ | |
5 data from http://aa.usno.navy.mil/data/docs/RS_OneYear.php (2013, san francisco) | |
6 which I found via http://www.perceptiveautomation.com/userforum/viewtopic.php?t=8627&p=54621 | |
7 """ | |
8 _src = """ | |
9 Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec. | |
10 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 | |
11 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 | |
12 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 | |
13 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 | |
14 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 | |
15 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 | |
16 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 | |
17 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 | |
18 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 | |
19 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 | |
20 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 | |
21 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 | |
22 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 | |
23 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 | |
24 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 | |
25 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 | |
26 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 | |
27 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 | |
28 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 | |
29 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 | |
30 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 | |
31 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 | |
32 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 | |
33 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 | |
34 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 | |
35 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 | |
36 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 | |
37 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 | |
38 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 | |
39 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 | |
40 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 | |
41 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 | |
42 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 | |
43 """ | |
44 | |
45 _data = {} # mm-dd : (morningtime, eveningtime) | |
46 for row in _src.splitlines()[4:]: | |
47 words = row.split() | |
48 day = words[0] | |
49 for month, t1, t2 in zip(range(1,13), words[1::2], words[2::2]): | |
50 if t1 == 'x': | |
51 continue | |
52 _data["%02d-%s" % (month, day)] = time(int(t1[:2]), int(t1[2:])), time(int(t2[:2]), int(t2[2:])) | |
53 | |
54 def isWithinTwilight(t): | |
55 """is the datetime within the civil twilight for san francisco""" | |
56 standardTime = t.astimezone(tzutc()) + timedelta(seconds=-8 * 3600) | |
57 t1, t2 = _data["%02d-%02d" % (standardTime.month, standardTime.day)] | |
58 return not (t1 < standardTime.time() < t2) | |
59 | |
60 if __name__ == '__main__': | |
61 print isWithinTwilight(datetime.now(tzlocal())) | |
62 |