diff --git a/bin/weeutil/test/test_sun.py b/bin/weeutil/test/test_sun.py new file mode 100644 index 00000000..a83dc017 --- /dev/null +++ b/bin/weeutil/test/test_sun.py @@ -0,0 +1,33 @@ +# +# Copyright (c) 2018 Tom Keffer +# +# See the file LICENSE.txt for your full rights. +# +from __future__ import absolute_import + +import os +import time +import unittest + +from weeutil import Sun + + +class SunTest(unittest.TestCase): + + def test_sunRiseSet(self): + os.environ['TZ'] = 'Australia/Sydney' + time.tzset() + # Sydney, Australia + result = Sun.sunRiseSet(2012, 1, 1, 151.21, -33.86) + self.assertAlmostEqual(result[0], -5.223949864965772, 6) + self.assertAlmostEqual(result[1], 9.152208948206106, 6) + + os.environ['TZ'] = 'America/Los_Angeles' + time.tzset() + # Hood River, USA + result = Sun.sunRiseSet(2012, 1, 1, -121.566, 45.686) + self.assertAlmostEqual(result[0], 15.781521580780003, 6) + self.assertAlmostEqual(result[1], 24.528947667456983, 6) + + +unittest.main() diff --git a/bin/weeutil/test/test_weeutil.py b/bin/weeutil/test/test_weeutil.py index 999e15db..5be4ef27 100644 --- a/bin/weeutil/test/test_weeutil.py +++ b/bin/weeutil/test/test_weeutil.py @@ -1,6 +1,6 @@ # This Python file uses the following encoding: utf-8 # -# Copyright (c) 2009-2015 Tom Keffer +# Copyright (c) 2009-2018 Tom Keffer # # See the file LICENSE.txt for your full rights. # @@ -92,79 +92,77 @@ class WeeutilTest(unittest.TestCase): stop = time.mktime((2013, 3, 10, 5, 0, 0, 0, 0, -1)) result = list(intervalgen(start, stop, 1800)) self.assertEqual(result, - map(lambda t: TimeSpan(t[0], t[1]), [(1362902400, 1362904200), - (1362904200, 1362906000), - (1362906000, 1362907800), - (1362907800, 1362909600), - (1362909600, 1362911400), - (1362911400, 1362913200), - (1362913200, 1362915000), - (1362915000, 1362916800)])) + list(map(lambda t: TimeSpan(t[0], t[1]), [(1362902400, 1362904200), + (1362904200, 1362906000), + (1362906000, 1362907800), + (1362907800, 1362909600), + (1362909600, 1362911400), + (1362911400, 1362913200), + (1362913200, 1362915000), + (1362915000, 1362916800)]))) # Test the ending of DST using a 30 minute increment: start = time.mktime((2013, 11, 3, 0, 0, 0, 0, 0, -1)) stop = time.mktime((2013, 11, 3, 6, 0, 0, 0, 0, -1)) result = list(intervalgen(start, stop, 1800)) self.assertEqual(result, - map(lambda t: TimeSpan(t[0], t[1]), [(1383462000, 1383463800), - (1383463800, 1383465600), - (1383465600, 1383467400), - (1383467400, 1383472800), - (1383472800, 1383474600), - (1383474600, 1383476400), - (1383476400, 1383478200), - (1383478200, 1383480000), - (1383480000, 1383481800), - (1383481800, 1383483600), - (1383483600, 1383485400), - (1383485400, 1383487200)])) + list(map(lambda t: TimeSpan(t[0], t[1]), [(1383462000, 1383463800), + (1383463800, 1383465600), + (1383465600, 1383467400), + (1383467400, 1383472800), + (1383472800, 1383474600), + (1383474600, 1383476400), + (1383476400, 1383478200), + (1383478200, 1383480000), + (1383480000, 1383481800), + (1383481800, 1383483600), + (1383483600, 1383485400), + (1383485400, 1383487200)]))) # Test the start of DST using a 3 hour increment: start = time.mktime((2013, 3, 9, 12, 0, 0, 0, 0, -1)) stop = time.mktime((2013, 3, 10, 11, 0, 0, 0, 0, -1)) result = list(intervalgen(start, stop, 10800)) self.assertEqual(result, - map(lambda t: TimeSpan(t[0], t[1]), [(1362859200, 1362870000), - (1362870000, 1362880800), - (1362880800, 1362891600), - (1362891600, 1362902400), - (1362902400, 1362909600), - (1362909600, 1362920400), - (1362920400, 1362931200), - (1362931200, 1362938400)])) + list(map(lambda t: TimeSpan(t[0], t[1]), [(1362859200, 1362870000), + (1362870000, 1362880800), + (1362880800, 1362891600), + (1362891600, 1362902400), + (1362902400, 1362909600), + (1362909600, 1362920400), + (1362920400, 1362931200), + (1362931200, 1362938400)]))) # Test the ending of DST using a 3 hour increment: start = time.mktime((2013, 11, 2, 12, 0, 0, 0, 0, -1)) stop = time.mktime((2013, 11, 3, 12, 0, 0, 0, 0, -1)) result = list(intervalgen(start, stop, 10800)) self.assertEqual(result, - map(lambda t: TimeSpan(t[0], t[1]), [(1383418800, 1383429600), - (1383429600, 1383440400), - (1383440400, 1383451200), - (1383451200, 1383462000), - (1383462000, 1383476400), - (1383476400, 1383487200), - (1383487200, 1383498000), - (1383498000, 1383508800)])) + list(map(lambda t: TimeSpan(t[0], t[1]), [(1383418800, 1383429600), + (1383429600, 1383440400), + (1383440400, 1383451200), + (1383451200, 1383462000), + (1383462000, 1383476400), + (1383476400, 1383487200), + (1383487200, 1383498000), + (1383498000, 1383508800)]))) # Test a monthly increment: start = time.mktime((2013, 1, 1, 0, 0, 0, 0, 0, -1)) stop = time.mktime((2014, 1, 1, 0, 0, 0, 0, 0, -1)) result = list(intervalgen(start, stop, 365.25 / 12 * 24 * 3600)) - # for x in result: - # print '(', int(x[0]), ', ', int(x[1]), '),' - expected = map(lambda t: TimeSpan(t[0], t[1]), [(1357027200, 1359705600), - (1359705600, 1362124800), - (1362124800, 1364799600), - (1364799600, 1367391600), - (1367391600, 1370070000), - (1370070000, 1372662000), - (1372662000, 1375340400), - (1375340400, 1378018800), - (1378018800, 1380610800), - (1380610800, 1383289200), - (1383289200, 1385884800), - (1385884800, 1388563200)]) + expected = list(map(lambda t: TimeSpan(t[0], t[1]), [(1357027200, 1359705600), + (1359705600, 1362124800), + (1362124800, 1364799600), + (1364799600, 1367391600), + (1367391600, 1370070000), + (1370070000, 1372662000), + (1372662000, 1375340400), + (1375340400, 1378018800), + (1378018800, 1380610800), + (1380610800, 1383289200), + (1383289200, 1385884800), + (1385884800, 1388563200)])) self.assertEqual(result, expected) # The "roundTS" feature has been removed. Keep the tests. tk 1/24/2017. @@ -185,7 +183,7 @@ class WeeutilTest(unittest.TestCase): "[2013-07-04 01:00:00 PDT (1372924800) -> 2013-07-04 02:00:00 PDT (1372928400)]") self.assertEqual(str(archiveHoursAgoSpan(time_ts, hours_ago=2)), "[2013-07-03 23:00:00 PDT (1372917600) -> 2013-07-04 00:00:00 PDT (1372921200)]") - time_ts = time.mktime(datetime.date(2013, 07, 04).timetuple()) + time_ts = time.mktime(datetime.date(2013, 7, 4).timetuple()) self.assertEqual(str(archiveHoursAgoSpan(time_ts, hours_ago=0)), "[2013-07-03 23:00:00 PDT (1372917600) -> 2013-07-04 00:00:00 PDT (1372921200)]") self.assertEqual(str(archiveHoursAgoSpan(time_ts, hours_ago=24)), @@ -280,19 +278,19 @@ class WeeutilTest(unittest.TestCase): t_length = 30 * 60 t_test = time.mktime((2009, 3, 4, 1, 57, 17, 0, 0, 0)) - t_ans = time.mktime((2009, 3, 4, 1, 30, 00, 0, 0, 0)) + t_ans = time.mktime((2009, 3, 4, 1, 30, 0, 0, 0, 0)) t_start = startOfInterval(t_test, t_length) self.assertEqual(t_start, t_ans) t_length = 60 * 60 t_test = time.mktime((2009, 3, 4, 1, 57, 17, 0, 0, 0)) - t_ans = time.mktime((2009, 3, 4, 1, 00, 00, 0, 0, 0)) + t_ans = time.mktime((2009, 3, 4, 1, 0, 0, 0, 0, 0)) t_start = startOfInterval(t_test, t_length) self.assertEqual(t_start, t_ans) t_length = 120 * 60 t_test = time.mktime((2009, 3, 4, 1, 57, 17, 0, 0, 0)) - t_ans = time.mktime((2009, 3, 4, 0, 00, 00, 0, 0, 0)) + t_ans = time.mktime((2009, 3, 4, 0, 0, 0, 0, 0, 0)) t_start = startOfInterval(t_test, t_length) self.assertEqual(t_start, t_ans) @@ -300,8 +298,8 @@ class WeeutilTest(unittest.TestCase): # This is 03:22:05 DST, just after the change over. # The correct answer is 02:00:00 DST. t_length = 120 * 60 - t_test = time.mktime((2009, 3, 8, 3, 22, 05, 0, 0, 1)) - t_ans = time.mktime((2009, 3, 8, 2, 00, 00, 0, 0, 1)) + t_test = time.mktime((2009, 3, 8, 3, 22, 5, 0, 0, 1)) + t_ans = time.mktime((2009, 3, 8, 2, 0, 0, 0, 0, 1)) t_start = startOfInterval(t_test, t_length) self.assertEqual(t_start, t_ans) @@ -310,8 +308,8 @@ class WeeutilTest(unittest.TestCase): # instant of the change over. # Correct answer is 00:59:00 ST. t_length = 60 - t_test = time.mktime((2009, 3, 8, 1, 00, 00, 0, 0, 0)) - t_ans = time.mktime((2009, 3, 8, 0, 59, 00, 0, 0, 0)) + t_test = time.mktime((2009, 3, 8, 1, 0, 0, 0, 0, 0)) + t_ans = time.mktime((2009, 3, 8, 0, 59, 0, 0, 0, 0)) t_start = startOfInterval(t_test, t_length) self.assertEqual(t_start, t_ans) @@ -319,7 +317,7 @@ class WeeutilTest(unittest.TestCase): # This is 01:22:05 DST, just before the change over. # The correct answer is 00:00:00 DST. t_length = 120 * 60 - t_test = time.mktime((2009, 11, 1, 1, 22, 05, 0, 0, 1)) + t_test = time.mktime((2009, 11, 1, 1, 22, 5, 0, 0, 1)) t_ans = time.mktime((2009, 11, 1, 0, 0, 0, 0, 0, 1)) t_start = startOfInterval(t_test, t_length) self.assertEqual(t_start, t_ans) @@ -328,29 +326,29 @@ class WeeutilTest(unittest.TestCase): # This is 01:22:05 ST, just after the change over. # The correct answer is 00:00:00 ST (which is 01:00:00 DST). t_length = 120 * 60 - t_test = time.mktime((2009, 11, 1, 1, 22, 05, 0, 0, 0)) - t_ans = time.mktime((2009, 11, 1, 0, 00, 00, 0, 0, 0)) + t_test = time.mktime((2009, 11, 1, 1, 22, 5, 0, 0, 0)) + t_ans = time.mktime((2009, 11, 1, 0, 0, 0, 0, 0, 0)) t_start = startOfInterval(t_test, t_length) self.assertEqual(t_start, t_ans) # Once again at 01:22:05 ST, just before the change over, but w/shorter interval t_length = 5 * 60 - t_test = time.mktime((2009, 11, 1, 1, 22, 05, 0, 0, 1)) - t_ans = time.mktime((2009, 11, 1, 1, 20, 00, 0, 0, 1)) + t_test = time.mktime((2009, 11, 1, 1, 22, 5, 0, 0, 1)) + t_ans = time.mktime((2009, 11, 1, 1, 20, 0, 0, 0, 1)) t_start = startOfInterval(t_test, t_length) self.assertEqual(t_start, t_ans) # Once again at 01:22:05 ST, just after the change over, but w/shorter interval t_length = 5 * 60 - t_test = time.mktime((2009, 11, 1, 1, 22, 05, 0, 0, 0)) - t_ans = time.mktime((2009, 11, 1, 1, 20, 00, 0, 0, 0)) + t_test = time.mktime((2009, 11, 1, 1, 22, 5, 0, 0, 0)) + t_ans = time.mktime((2009, 11, 1, 1, 20, 0, 0, 0, 0)) t_start = startOfInterval(t_test, t_length) self.assertEqual(t_start, t_ans) # Once again at 01:22:05 ST, just after the change over, but with 1 hour interval t_length = 60 * 60 - t_test = time.mktime((2009, 11, 1, 1, 22, 05, 0, 0, 0)) - t_ans = time.mktime((2009, 11, 1, 1, 00, 00, 0, 0, 0)) + t_test = time.mktime((2009, 11, 1, 1, 22, 5, 0, 0, 0)) + t_ans = time.mktime((2009, 11, 1, 1, 0, 0, 0, 0, 0)) t_start = startOfInterval(t_test, t_length) self.assertEqual(t_start, t_ans) @@ -473,7 +471,7 @@ class WeeutilTest(unittest.TestCase): # Should generate throught 2007-12-23 20:00:00 throught 2007-12-24 4:00:00 start_ts = time.mktime((2007, 12, 23, 20, 15, 0, 0, 0, -1)) - stop_ts = time.mktime((2007, 12, 24, 03, 45, 0, 0, 0, -1)) + stop_ts = time.mktime((2007, 12, 24, 3, 45, 0, 0, 0, -1)) hourlist = [span for span in genHourSpans(start_ts, stop_ts)] @@ -611,99 +609,104 @@ class WeeutilTest(unittest.TestCase): ] expected = [ ( - ("lat: -33.86 lon: 151.21 sydney first: day", - "2012-01-02 00:00:00 UTC (1325462400) 2012-01-02 11:00:00 (1325462400)", - "2012-01-02 09:09:33 UTC (1325495373) 2012-01-02 20:09:33 (1325495373)", - "2012-01-02 18:48:44 UTC (1325530124) 2012-01-03 05:48:44 (1325530124)", - "2012-01-03 00:00:00 UTC (1325548800) 2012-01-03 11:00:00 (1325548800)", + ('lat: -33.86 lon: 151.21 sydney first: day', + '2012-01-02 00:00:00 UTC (1325462400) 2012-01-02 11:00:00 (1325462400)', + '2012-01-02 09:09:22 UTC (1325495362) 2012-01-02 20:09:22 (1325495362)', + '2012-01-02 18:48:02 UTC (1325530082) 2012-01-03 05:48:02 (1325530082)', + '2012-01-03 00:00:00 UTC (1325548800) 2012-01-03 11:00:00 (1325548800)' ), - ("lat: 35.6895 lon: 139.6917 seoul first: day", - "2012-01-02 00:00:00 UTC (1325462400) 2012-01-02 09:00:00 (1325462400)", - "2012-01-02 07:38:42 UTC (1325489922) 2012-01-02 16:38:42 (1325489922)", - "2012-01-02 21:51:08 UTC (1325541068) 2012-01-03 06:51:08 (1325541068)", - "2012-01-03 00:00:00 UTC (1325548800) 2012-01-03 09:00:00 (1325548800)", + ('lat: 35.6895 lon: 139.6917 seoul first: day', + '2012-01-02 00:00:00 UTC (1325462400) 2012-01-02 09:00:00 (1325462400)', + '2012-01-02 07:38:01 UTC (1325489881) 2012-01-02 16:38:01 (1325489881)', + '2012-01-02 21:50:59 UTC (1325541059) 2012-01-03 06:50:59 (1325541059)', + '2012-01-03 00:00:00 UTC (1325548800) 2012-01-03 09:00:00 (1325548800)' ), - ("lat: -33.93 lon: 18.42 cape town first: night", - "2012-01-02 00:00:00 UTC (1325462400) 2012-01-02 02:00:00 (1325462400)", - "2012-01-02 03:39:13 UTC (1325475553) 2012-01-02 05:39:13 (1325475553)", - "2012-01-02 18:00:57 UTC (1325527257) 2012-01-02 20:00:57 (1325527257)", - "2012-01-03 00:00:00 UTC (1325548800) 2012-01-03 02:00:00 (1325548800)", + ('lat: -33.93 lon: 18.42 cape town first: night', + '2012-01-02 00:00:00 UTC (1325462400) 2012-01-02 02:00:00 (1325462400)', + '2012-01-02 03:38:32 UTC (1325475512) 2012-01-02 05:38:32 (1325475512)', + '2012-01-02 18:00:47 UTC (1325527247) 2012-01-02 20:00:47 (1325527247)', + '2012-01-03 00:00:00 UTC (1325548800) 2012-01-03 02:00:00 (1325548800)' ), - ("lat: 51.4791 lon: 0 greenwich first: night", - "2012-01-02 00:00:00 UTC (1325462400) 2012-01-02 00:00:00 (1325462400)", - "2012-01-02 08:05:18 UTC (1325491518) 2012-01-02 08:05:18 (1325491518)", - "2012-01-02 16:02:17 UTC (1325520137) 2012-01-02 16:02:17 (1325520137)", - "2012-01-03 00:00:00 UTC (1325548800) 2012-01-03 00:00:00 (1325548800)", + ('lat: 51.4791 lon: 0 greenwich first: night', + '2012-01-02 00:00:00 UTC (1325462400) 2012-01-02 00:00:00 (1325462400)', + '2012-01-02 08:05:24 UTC (1325491524) 2012-01-02 08:05:24 (1325491524)', + '2012-01-02 16:01:20 UTC (1325520080) 2012-01-02 16:01:20 (1325520080)', + '2012-01-03 00:00:00 UTC (1325548800) 2012-01-03 00:00:00 (1325548800)' ), - ("lat: 42.358 lon: -71.06 boston first: night", - "2012-01-02 00:00:00 UTC (1325462400) 2012-01-01 19:00:00 (1325462400)", - "2012-01-02 12:13:26 UTC (1325506406) 2012-01-02 07:13:26 (1325506406)", - "2012-01-02 21:22:49 UTC (1325539369) 2012-01-02 16:22:49 (1325539369)", - "2012-01-03 00:00:00 UTC (1325548800) 2012-01-02 19:00:00 (1325548800)", + ('lat: 42.358 lon: -71.06 boston first: night', + '2012-01-02 00:00:00 UTC (1325462400) 2012-01-01 19:00:00 (1325462400)', + '2012-01-02 12:13:21 UTC (1325506401) 2012-01-02 07:13:21 (1325506401)', + '2012-01-02 21:22:02 UTC (1325539322) 2012-01-02 16:22:02 (1325539322)', + '2012-01-03 00:00:00 UTC (1325548800) 2012-01-02 19:00:00 (1325548800)' ), - ("lat: 21.3 lon: -157.8167 honolulu first: day", - "2012-01-02 00:00:00 UTC (1325462400) 2012-01-01 14:00:00 (1325462400)", - "2012-01-02 04:00:45 UTC (1325476845) 2012-01-01 18:00:45 (1325476845)", - "2012-01-02 17:09:09 UTC (1325524149) 2012-01-02 07:09:09 (1325524149)", - "2012-01-03 00:00:00 UTC (1325548800) 2012-01-02 14:00:00 (1325548800)", + ('lat: 21.3 lon: -157.8167 honolulu first: day', + '2012-01-02 00:00:00 UTC (1325462400) 2012-01-01 14:00:00 (1325462400)', + '2012-01-02 04:00:11 UTC (1325476811) 2012-01-01 18:00:11 (1325476811)', + '2012-01-02 17:08:52 UTC (1325524132) 2012-01-02 07:08:52 (1325524132)', + '2012-01-03 00:00:00 UTC (1325548800) 2012-01-02 14:00:00 (1325548800)' )), ( - ("lat: -33.86 lon: 151.21 sydney first: day", - "2012-01-02 22:00:00 UTC (1325541600) 2012-01-03 09:00:00 (1325541600)", - "2012-01-03 09:09:43 UTC (1325581783) 2012-01-03 20:09:43 (1325581783)", - "2012-01-03 18:49:31 UTC (1325616571) 2012-01-04 05:49:31 (1325616571)", - "2012-01-03 22:00:00 UTC (1325628000) 2012-01-04 09:00:00 (1325628000)", + ('lat: -33.86 lon: 151.21 sydney first: day', + '2012-01-02 22:00:00 UTC (1325541600) 2012-01-03 09:00:00 (1325541600)', + '2012-01-03 09:09:34 UTC (1325581774) 2012-01-03 20:09:34 (1325581774)', + '2012-01-03 18:48:48 UTC (1325616528) 2012-01-04 05:48:48 (1325616528)', + '2012-01-03 22:00:00 UTC (1325628000) 2012-01-04 09:00:00 (1325628000)' ), - ("lat: 35.6895 lon: 139.6917 seoul first: day", - "2012-01-02 22:00:00 UTC (1325541600) 2012-01-03 07:00:00 (1325541600)", - "2012-01-03 07:39:29 UTC (1325576369) 2012-01-03 16:39:29 (1325576369)", - "2012-01-03 21:51:16 UTC (1325627476) 2012-01-04 06:51:16 (1325627476)", - "2012-01-03 22:00:00 UTC (1325628000) 2012-01-04 07:00:00 (1325628000)", + ('lat: 35.6895 lon: 139.6917 seoul first: day', + '2012-01-02 22:00:00 UTC (1325541600) 2012-01-03 07:00:00 (1325541600)', + '2012-01-03 07:38:47 UTC (1325576327) 2012-01-03 16:38:47 (1325576327)', + '2012-01-03 21:51:09 UTC (1325627469) 2012-01-04 06:51:09 (1325627469)', + '2012-01-03 22:00:00 UTC (1325628000) 2012-01-04 07:00:00 (1325628000)' ), - ("lat: -33.93 lon: 18.42 cape town first: night", - "2012-01-02 22:00:00 UTC (1325541600) 2012-01-03 00:00:00 (1325541600)", - "2012-01-03 03:39:59 UTC (1325561999) 2012-01-03 05:39:59 (1325561999)", - "2012-01-03 18:01:07 UTC (1325613667) 2012-01-03 20:01:07 (1325613667)", - "2012-01-03 22:00:00 UTC (1325628000) 2012-01-04 00:00:00 (1325628000)", + ('lat: -33.93 lon: 18.42 cape town first: night', + '2012-01-02 22:00:00 UTC (1325541600) 2012-01-03 00:00:00 (1325541600)', + '2012-01-03 03:39:17 UTC (1325561957) 2012-01-03 05:39:17 (1325561957)', + '2012-01-03 18:00:58 UTC (1325613658) 2012-01-03 20:00:58 (1325613658)', + '2012-01-03 22:00:00 UTC (1325628000) 2012-01-04 00:00:00 (1325628000)' ), - ("lat: 51.4791 lon: 0 greenwich first: night", - "2012-01-02 22:00:00 UTC (1325541600) 2012-01-02 22:00:00 (1325541600)", - "2012-01-03 08:05:09 UTC (1325577909) 2012-01-03 08:05:09 (1325577909)", - "2012-01-03 16:03:22 UTC (1325606602) 2012-01-03 16:03:22 (1325606602)", - "2012-01-03 22:00:00 UTC (1325628000) 2012-01-03 22:00:00 (1325628000)", + ('lat: 51.4791 lon: 0 greenwich first: night', + '2012-01-02 22:00:00 UTC (1325541600) 2012-01-02 22:00:00 (1325541600)', + '2012-01-03 08:05:17 UTC (1325577917) 2012-01-03 08:05:17 (1325577917)', + '2012-01-03 16:02:23 UTC (1325606543) 2012-01-03 16:02:23 (1325606543)', + '2012-01-03 22:00:00 UTC (1325628000) 2012-01-03 22:00:00 (1325628000)' ), - ("lat: 42.358 lon: -71.06 boston first: night", - "2012-01-02 22:00:00 UTC (1325541600) 2012-01-02 17:00:00 (1325541600)", - "2012-01-03 12:13:28 UTC (1325592808) 2012-01-03 07:13:28 (1325592808)", - "2012-01-03 21:23:42 UTC (1325625822) 2012-01-03 16:23:42 (1325625822)", - "2012-01-03 22:00:00 UTC (1325628000) 2012-01-03 17:00:00 (1325628000)", + ('lat: 42.358 lon: -71.06 boston first: night', + '2012-01-02 22:00:00 UTC (1325541600) 2012-01-02 17:00:00 (1325541600)', + '2012-01-03 12:13:26 UTC (1325592806) 2012-01-03 07:13:26 (1325592806)', + '2012-01-03 21:22:54 UTC (1325625774) 2012-01-03 16:22:54 (1325625774)', + '2012-01-03 22:00:00 UTC (1325628000) 2012-01-03 17:00:00 (1325628000)' ), - ("lat: 21.3 lon: -157.8167 honolulu first: day", - "2012-01-02 22:00:00 UTC (1325541600) 2012-01-02 12:00:00 (1325541600)", - "2012-01-03 04:01:22 UTC (1325563282) 2012-01-02 18:01:22 (1325563282)", - "2012-01-03 17:09:26 UTC (1325610566) 2012-01-03 07:09:26 (1325610566)", - "2012-01-03 22:00:00 UTC (1325628000) 2012-01-03 12:00:00 (1325628000)" - )) + ('lat: 21.3 lon: -157.8167 honolulu first: day', + '2012-01-02 22:00:00 UTC (1325541600) 2012-01-02 12:00:00 (1325541600)', + '2012-01-03 04:00:48 UTC (1325563248) 2012-01-02 18:00:48 (1325563248)', + '2012-01-03 17:09:11 UTC (1325610551) 2012-01-03 07:09:11 (1325610551)', + '2012-01-03 22:00:00 UTC (1325628000) 2012-01-03 12:00:00 (1325628000)' + ) + ) ] + + self.assertEqual(times, [(1325462400, 1325548800), (1325541600, 1325628000)]) + for i, t in enumerate(times): for j, l in enumerate(locs): os.environ['TZ'] = l[3] first, values = getDayNightTransitions(t[0], t[1], l[0], l[1]) - self.assertEqual("lat: %s lon: %s %s first: %s" % - (l[0], l[1], l[2], first), - expected[i][j][0]) - self.assertEqual("%s %s" % (timestamp_to_gmtime(t[0]), - timestamp_to_local(t[0])), - expected[i][j][1]) - self.assertEqual("%s %s" % (timestamp_to_gmtime(values[0]), - timestamp_to_local(values[0])), - expected[i][j][2]) - self.assertEqual("%s %s" % (timestamp_to_gmtime(values[1]), - timestamp_to_local(values[1])), - expected[i][j][3]) - self.assertEqual("%s %s" % (timestamp_to_gmtime(t[1]), - timestamp_to_local(t[1])), - expected[i][j][4]) + + self.assertEqual("lat: %s lon: %s %s first: %s" % (l[0], l[1], l[2], first), + expected[i][j][0], + msg="times=%s; location=%s" % (t, l)) + self.assertEqual("%s %s" % (timestamp_to_gmtime(t[0]), timestamp_to_local(t[0])), + expected[i][j][1], + msg="times=%s; location=%s" % (t, l)) + self.assertEqual("%s %s" % (timestamp_to_gmtime(values[0]), timestamp_to_local(values[0])), + expected[i][j][2], + msg="times=%s; location=%s" % (t, l)) + self.assertEqual("%s %s" % (timestamp_to_gmtime(values[1]), timestamp_to_local(values[1])), + expected[i][j][3], + msg="times=%s; location=%s" % (t, l)) + self.assertEqual("%s %s" % (timestamp_to_gmtime(t[1]), timestamp_to_local(t[1])), + expected[i][j][4], + msg="times=%s; location=%s" % (t, l)) def test_utc_conversions(self): self.assertEqual(utc_to_ts(2009, 3, 27, 14.5), 1238164200) diff --git a/bin/weeutil/weeutil.py b/bin/weeutil/weeutil.py index 97157d8d..f2f8bdec 100644 --- a/bin/weeutil/weeutil.py +++ b/bin/weeutil/weeutil.py @@ -8,6 +8,7 @@ Works under Python 2 and Python 3. """ +from __future__ import absolute_import from __future__ import print_function import calendar @@ -26,7 +27,7 @@ except ImportError: from io import StringIO # For backwards compatibility: -import config +from weeutil import config search_up = config.search_up accumulateLeaves = config.accumulateLeaves merge_config = config.merge_config @@ -983,7 +984,7 @@ def getDayNightTransitions(start_ts, end_ts, lat, lon): returns: indication of whether the period from start to first transition is day or night, plus array of transitions (UTC). """ - import Sun + from weeutil import Sun first = None values = []