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)
def getDayNightTransitions(start_ts, end_ts, lat, lon): """Return the day-night transitions between the start and end times. start_ts: A timestamp (UTC) indicating the beginning of the period end_ts: A timestamp (UTC) indicating the end of the period returns: indication of whether the period from start to first transition is day or night, plus array of transitions (UTC). """ from weeutil import Sun first = None values = [] for t in range(start_ts - 3600 * 24, end_ts + 3600 * 24 + 1, 3600 * 24): x = startOfDayUTC(t) x_tt = time.gmtime(x) y, m, d = x_tt[:3] (sunrise_utc, sunset_utc) = Sun.sunRiseSet(y, m, d, lon, lat) daystart_ts = calendar.timegm((y, m, d, 0, 0, 0, 0, 0, -1)) sunrise_ts = int(daystart_ts + sunrise_utc * 3600.0 + 0.5) sunset_ts = int(daystart_ts + sunset_utc * 3600.0 + 0.5) if start_ts < sunrise_ts < end_ts: values.append(sunrise_ts) if first is None: first = 'night' if start_ts < sunset_ts < end_ts: values.append(sunset_ts) if first is None: first = 'day' return first, values