Add time.tzset() after setting timezone

This commit is contained in:
Tom Keffer
2019-02-10 05:39:07 -08:00
parent 35bfb641fe
commit 8eba60f4aa
9 changed files with 83 additions and 30 deletions

View File

@@ -1,4 +1,3 @@
When doing a --dump on Vantage, times come out wrong.

View File

@@ -13,7 +13,7 @@ import unittest
from weeutil.weeutil import * # @UnusedWildImport
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
def timestamp_to_local(ts):
"""Return a string in local time"""
@@ -43,6 +43,7 @@ class WeeutilTest(unittest.TestCase):
def test_stampgen(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
# Test the start of DST using a 30 minute increment:
start = time.mktime((2013, 3, 10, 0, 0, 0, 0, 0, -1))
@@ -86,6 +87,7 @@ class WeeutilTest(unittest.TestCase):
def test_intervalgen(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
# Test the start of DST using a 30 minute increment:
start = time.mktime((2013, 3, 10, 0, 0, 0, 0, 0, -1))
@@ -178,6 +180,7 @@ class WeeutilTest(unittest.TestCase):
def test_archiveHoursAgoSpan(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
time_ts = time.mktime(time.strptime("2013-07-04 01:57:35", "%Y-%m-%d %H:%M:%S"))
self.assertEqual(str(archiveHoursAgoSpan(time_ts, hours_ago=0)),
"[2013-07-04 01:00:00 PDT (1372924800) -> 2013-07-04 02:00:00 PDT (1372928400)]")
@@ -192,6 +195,7 @@ class WeeutilTest(unittest.TestCase):
def test_archiveSpanSpan(self):
os.environ['TZ'] = 'Australia/Brisbane'
time.tzset()
time_ts = time.mktime(time.strptime("2015-07-21 09:05:35", "%Y-%m-%d %H:%M:%S"))
self.assertEqual(time_ts, 1437433535)
self.assertEqual(archiveSpanSpan(time_ts, time_delta=3600), TimeSpan(1437429935, 1437433535))
@@ -206,6 +210,7 @@ class WeeutilTest(unittest.TestCase):
# Test over a DST boundary. Because Brisbane does not observe DST, we need to
# switch timezones.
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
time_ts = time.mktime(time.strptime("2016-03-13 10:00:00", "%Y-%m-%d %H:%M:%S"))
self.assertEqual(time_ts, 1457888400)
span = archiveSpanSpan(time_ts, day_delta=1)
@@ -216,16 +221,19 @@ class WeeutilTest(unittest.TestCase):
def test_isMidnight(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
self.assertFalse(isMidnight(time.mktime(time.strptime("2013-07-04 01:57:35", "%Y-%m-%d %H:%M:%S"))))
self.assertTrue(isMidnight(time.mktime(time.strptime("2013-07-04 00:00:00", "%Y-%m-%d %H:%M:%S"))))
def test_isStartOfDay(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
self.assertFalse(isStartOfDay(time.mktime(time.strptime("2013-07-04 01:57:35", "%Y-%m-%d %H:%M:%S"))))
self.assertTrue(isStartOfDay(time.mktime(time.strptime("2013-07-04 00:00:00", "%Y-%m-%d %H:%M:%S"))))
# Brazilian DST starts at midnight
os.environ['TZ'] = 'America/Sao_Paulo'
time.tzset()
# This time is the start of DST and considered the start of the day: 4-11-2018 0100
self.assertTrue(isStartOfDay(1541300400))
self.assertFalse(isStartOfDay(1541300400 - 10))
@@ -233,6 +241,7 @@ class WeeutilTest(unittest.TestCase):
def test_startOfInterval(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
t_length = 1 * 60
t_test = time.mktime((2009, 3, 4, 1, 57, 17, 0, 0, 0))
@@ -385,6 +394,7 @@ class WeeutilTest(unittest.TestCase):
def test_genYearSpans(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
# Should generate years 2007 through 2008:"
start_ts = time.mktime((2007, 12, 3, 10, 15, 0, 0, 0, -1))
@@ -401,6 +411,7 @@ class WeeutilTest(unittest.TestCase):
def test_genMonthSpans(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
# Should generate months 2007-12 through 2008-02:
start_ts = time.mktime((2007, 12, 3, 10, 15, 0, 0, 0, -1))
@@ -432,6 +443,7 @@ class WeeutilTest(unittest.TestCase):
def test_genDaySpans(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
# Should generate 2007-12-23 through 2008-1-5:"
start_ts = time.mktime((2007, 12, 23, 10, 15, 0, 0, 0, -1))
@@ -468,6 +480,7 @@ class WeeutilTest(unittest.TestCase):
def test_genHourSpans(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
# 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))
@@ -499,6 +512,7 @@ class WeeutilTest(unittest.TestCase):
def test_archiveDaySpan(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
self.assertEqual(archiveDaySpan(time.mktime((2007, 12, 13, 10, 15, 0, 0, 0, -1))),
TimeSpan(time.mktime((2007, 12, 13, 0, 0, 0, 0, 0, -1)),
@@ -519,6 +533,7 @@ class WeeutilTest(unittest.TestCase):
def test_archiveWeekSpan(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
self.assertEqual(archiveWeekSpan(time.mktime((2007, 12, 13, 10, 15, 0, 0, 0, -1))),
TimeSpan(time.mktime((2007, 12, 9, 0, 0, 0, 0, 0, -1)),
@@ -535,6 +550,7 @@ class WeeutilTest(unittest.TestCase):
def test_archiveMonthSpan(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
self.assertEqual(archiveMonthSpan(time.mktime((2007, 12, 13, 10, 15, 0, 0, 0, -1))),
TimeSpan(time.mktime((2007, 12, 1, 0, 0, 0, 0, 0, -1)),
@@ -554,6 +570,7 @@ class WeeutilTest(unittest.TestCase):
def test_archiveYearSpan(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
self.assertEqual(archiveYearSpan(time.mktime((2007, 12, 13, 10, 15, 0, 0, 0, -1))),
TimeSpan(time.mktime((2007, 1, 1, 0, 0, 0, 0, 0, -1)),
@@ -570,6 +587,7 @@ class WeeutilTest(unittest.TestCase):
def test_archiveRainYearSpan(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
self.assertEqual(archiveRainYearSpan(time.mktime((2007, 2, 13, 10, 15, 0, 0, 0, -1)), 10),
TimeSpan(time.mktime((2006, 10, 1, 0, 0, 0, 0, 0, -1)),
@@ -583,6 +601,7 @@ class WeeutilTest(unittest.TestCase):
def test_DST(self):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
# Test start-of-day routines around a DST boundary:
start_ts = time.mktime((2007, 3, 11, 1, 0, 0, 0, 0, -1))
@@ -690,6 +709,7 @@ class WeeutilTest(unittest.TestCase):
for i, t in enumerate(times):
for j, l in enumerate(locs):
os.environ['TZ'] = l[3]
time.tzset()
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),
@@ -711,6 +731,7 @@ class WeeutilTest(unittest.TestCase):
def test_utc_conversions(self):
self.assertEqual(utc_to_ts(2009, 3, 27, 14.5), 1238164200)
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
tt = utc_to_local_tt(2009, 3, 27, 14.5)
self.assertEqual(tt[0:5], (2009, 3, 27, 7, 30))

View File

@@ -21,7 +21,7 @@ import traceback
# Compatibility shims
import six
from six.moves import StringIO
from six.moves import StringIO, input
# For backwards compatibility:
from weeutil import config
@@ -100,6 +100,7 @@ def stampgen(startstamp, stopstamp, interval):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> startstamp = 1236560400
>>> print(timestamp_to_string(startstamp))
2009-03-08 18:00:00 PDT (1236560400)
@@ -178,6 +179,7 @@ def startOfInterval(time_ts, interval):
Examples:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> start_ts = time.mktime(time.strptime("2013-07-04 01:57:35", "%Y-%m-%d %H:%M:%S"))
>>> time.ctime(startOfInterval(start_ts, 300))
'Thu Jul 4 01:55:00 2013'
@@ -304,6 +306,7 @@ def intervalgen(start_ts, stop_ts, interval):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> startstamp = 1236477600
>>> print(timestamp_to_string(startstamp))
2009-03-07 18:00:00 PST (1236477600)
@@ -391,6 +394,7 @@ def archiveHoursAgoSpan(time_ts, hours_ago=0, grace=1):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> time_ts = time.mktime(time.strptime("2013-07-04 01:57:35", "%Y-%m-%d %H:%M:%S"))
>>> print(archiveHoursAgoSpan(time_ts, hours_ago=0))
[2013-07-04 01:00:00 PDT (1372924800) -> 2013-07-04 02:00:00 PDT (1372928400)]
@@ -421,6 +425,7 @@ def archiveSpanSpan(time_ts, time_delta=0, hour_delta=0, day_delta=0, week_delta
Example:
>>> os.environ['TZ'] = 'Australia/Brisbane'
>>> time.tzset()
>>> time_ts = time.mktime(time.strptime("2015-07-21 09:05:35", "%Y-%m-%d %H:%M:%S"))
>>> print(archiveSpanSpan(time_ts, time_delta=3600))
[2015-07-21 08:05:35 AEST (1437429935) -> 2015-07-21 09:05:35 AEST (1437433535)]
@@ -442,6 +447,7 @@ def archiveSpanSpan(time_ts, time_delta=0, hour_delta=0, day_delta=0, week_delta
Example over a DST boundary. Because Brisbane does not observe DST, we need to
switch timezones.
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> time_ts = 1457888400
>>> print(timestamp_to_string(time_ts))
2016-03-13 10:00:00 PDT (1457888400)
@@ -486,6 +492,7 @@ def isMidnight(time_ts):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> time_ts = time.mktime(time.strptime("2013-07-04 01:57:35", "%Y-%m-%d %H:%M:%S"))
>>> print(isMidnight(time_ts))
False
@@ -502,6 +509,7 @@ def isStartOfDay(time_ts):
"""Is the indicated time at the start of the day, local time?
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> time_ts = time.mktime(time.strptime("2013-07-04 01:57:35", "%Y-%m-%d %H:%M:%S"))
>>> print(isStartOfDay(time_ts))
False
@@ -509,6 +517,7 @@ def isStartOfDay(time_ts):
>>> print(isStartOfDay(time_ts))
True
>>> os.environ['TZ'] = 'America/Sao_Paulo'
>>> time.tzset()
>>> time_ts = 1541300400
>>> print(isStartOfDay(time_ts))
True
@@ -541,6 +550,7 @@ def archiveDaySpan(time_ts, grace=1, days_ago=0):
Example, which spans the end-of-year boundary
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> time_ts = time.mktime(time.strptime("2014-01-01 01:57:35", "%Y-%m-%d %H:%M:%S"))
As for today:
@@ -589,6 +599,7 @@ def archiveWeekSpan(time_ts, startOfWeek=6, grace=1, weeks_ago=0):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> time_ts = 1483429962
>>> print(timestamp_to_string(time_ts))
2017-01-02 23:52:42 PST (1483429962)
@@ -631,6 +642,7 @@ def archiveMonthSpan(time_ts, grace=1, months_ago=0):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> time_ts = 1483429962
>>> print(timestamp_to_string(time_ts))
2017-01-02 23:52:42 PST (1483429962)
@@ -725,6 +737,7 @@ def genHourSpans(start_ts, stop_ts):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> start_ts = 1204796460
>>> stop_ts = 1204818360
@@ -768,6 +781,7 @@ def genDaySpans(start_ts, stop_ts):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> start_ts = 1204796460
>>> stop_ts = 1205265720
@@ -815,6 +829,7 @@ def genMonthSpans(start_ts, stop_ts):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> start_ts = 1196705700
>>> stop_ts = 1206101100
>>> print("start time is %s" % timestamp_to_string(start_ts))
@@ -902,6 +917,7 @@ def startOfGregorianDay(date_greg):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> date_greg = 735973 # 10-Jan-2016
>>> print(startOfGregorianDay(date_greg))
1452412800
@@ -921,6 +937,7 @@ def toGregorianDay(time_ts):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> time_ts = 1452412800 # Midnight, 10-Jan-2016
>>> print(toGregorianDay(time_ts))
735972
@@ -1023,6 +1040,7 @@ def timestamp_to_string(ts, format_str="%Y-%m-%d %H:%M:%S %Z"):
Example:
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> print(timestamp_to_string(1196705700))
2007-12-03 10:15:00 PST (1196705700)
>>> print(timestamp_to_string(None))
@@ -1077,6 +1095,7 @@ def utc_to_local_tt(y, m, d, hrs_utc):
Returns: A timetuple with the local time.
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> tt=utc_to_local_tt(2009, 3, 27, 14.5)
>>> print(tt.tm_year, tt.tm_mon, tt.tm_mday, tt.tm_hour, tt.tm_min)
2009 3 27 7 30
@@ -1179,7 +1198,7 @@ class GenWithPeek(object):
def __iter__(self):
return self
def next(self):
def __next__(self):
"""Advance to the next object"""
if self.have_peek:
self.have_peek = False
@@ -1187,6 +1206,9 @@ class GenWithPeek(object):
else:
return next(self.generator)
# For Python 2:
next = __next__
def peek(self):
"""Take a peek at the next object"""
if not self.have_peek:
@@ -1394,7 +1416,7 @@ def y_or_n(msg, noprompt):
ans = None
while ans not in ['y', 'n']:
ans = six.input(msg)
ans = input(msg)
return ans

View File

@@ -57,6 +57,7 @@ class Almanac(object):
These examples are designed to work in the Pacific timezone
>>> import os
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> from weeutil.weeutil import timestamp_to_string, timestamp_to_gmtime
>>> t = 1238180400
>>> print(timestamp_to_string(t))

View File

@@ -19,6 +19,7 @@ import unittest
import configobj
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
import weeutil.weeutil
import weewx.tags

View File

@@ -15,6 +15,7 @@ import os.path
import configobj
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
import weedb
import weeutil.weeutil

View File

@@ -23,6 +23,7 @@ import configobj
#
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
# This will use the locale specified by the environment variable 'LANG'
# Other options are possible. See:

View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009-2015 Tom Keffer <tkeffer@gmail.com>
# Copyright (c) 2009-2019 Tom Keffer <tkeffer@gmail.com>
#
# See the file LICENSE.txt for your full rights.
#
@@ -9,6 +9,8 @@
import unittest
import operator
import six
import weewx.units
from weewx.units import ValueTuple
@@ -77,14 +79,14 @@ class ConverterTest(unittest.TestCase):
d_test = c.convertDict(d_m)
self.assertEqual(d_test['outTemp'], d_us['outTemp'])
self.assertEqual(d_test['barometer'], d_us['barometer'])
self.assertFalse(d_test.has_key('usUnits'))
self.assertFalse('usUnits' in d_test)
# Go the other way:
cm = weewx.units.Converter(weewx.units.MetricUnits)
d_test = cm.convertDict(d_us)
self.assertEqual(d_test['outTemp'], d_m['outTemp'])
self.assertEqual(d_test['barometer'], d_m['barometer'])
self.assertFalse(d_test.has_key('usUnits'))
self.assertFalse('usUnits' in d_test)
# Test impossible conversions:
d_m['outTemp'] = (20.01, 'foo', 'group_temperature')
@@ -107,42 +109,43 @@ class ValueHelperTest(unittest.TestCase):
def testFormatting(self):
value_t = (68.01, "degree_F", "group_temperature")
vh = weewx.units.ValueHelper(value_t)
self.assertEqual(vh.string(), "68.0°F")
self.assertEqual(vh.nolabel("T=%.3f"), "T=68.010")
self.assertEqual(vh.formatted, "68.0")
self.assertEqual(vh.string(), u"68.0°F")
self.assertEqual(vh.nolabel("T=%.3f"), u"T=68.010")
self.assertEqual(vh.formatted, u"68.0")
self.assertEqual(vh.raw, 68.01)
self.assertEqual(str(vh), "68.0°F")
self.assertEqual(str(vh.degree_F), "68.0°F")
self.assertEqual(str(vh.degree_C), "20.0°C")
self.assertEqual(six.text_type(vh), u"68.0°F")
self.assertEqual(six.text_type(vh.degree_F), u"68.0°F")
self.assertEqual(six.text_type(vh.degree_C), u"20.0°C")
def testFormattingWithConversion(self):
value_t = (68.01, "degree_F", "group_temperature")
c_m = weewx.units.Converter(weewx.units.MetricUnits)
vh = weewx.units.ValueHelper(value_t, converter=c_m)
self.assertEqual(str(vh), "20.0°C")
self.assertEqual(str(vh.degree_F), "68.0°F")
self.assertEqual(str(vh.degree_C), "20.0°C")
self.assertEqual(six.text_type(vh), u"20.0°C")
self.assertEqual(six.text_type(vh.degree_F), u"68.0°F")
self.assertEqual(six.text_type(vh.degree_C), u"20.0°C")
# Try an impossible conversion:
self.assertRaises(AttributeError, getattr, vh, 'meter')
def testExplicitConversion(self):
value_t = (10.0, "meter_per_second", "group_speed")
vh = weewx.units.ValueHelper(value_t)
self.assertEqual(str(vh), "22 mph")
self.assertEqual(str(vh.knot), "19 knots")
self.assertEqual(six.text_type(vh), "22 mph")
self.assertEqual(six.text_type(vh.knot), "19 knots")
def testNoneValue(self):
value_t = (None, "degree_C", "group_temperature")
converter = weewx.units.Converter()
vh = weewx.units.ValueHelper(value_t, converter=converter)
self.assertEqual(str(vh), " N/A")
self.assertEqual(str(vh.degree_C), " N/A")
self.assertEqual(six.text_type(vh), " N/A")
self.assertEqual(six.text_type(vh.degree_C), " N/A")
def testElapsedTime(self):
value_t = (2*86400 + 1*3600 + 5*60 + 12, "second", "group_deltatime")
vh = weewx.units.ValueHelper(value_t)
self.assertEqual(vh.string(), "2 days, 1 hour, 5 minutes")
format_label = "%(day)d%(day_label)s, %(hour)d%(hour_label)s, %(minute)d%(minute_label)s, %(second)d%(second_label)s"
format_label = "%(day)d%(day_label)s, %(hour)d%(hour_label)s, " \
"%(minute)d%(minute_label)s, %(second)d%(second_label)s"
self.assertEqual(vh.format(format_label), "2 days, 1 hour, 5 minutes, 12 seconds")
# Now try a 'None' value:
vh = weewx.units.ValueHelper((None, "second", "group_deltatime"))

View File

@@ -485,6 +485,7 @@ class Formatter(object):
Examples (using the default formatters):
>>> import os
>>> os.environ['TZ'] = 'America/Los_Angeles'
>>> time.tzset()
>>> f = Formatter()
>>> print f.toString((20.0, "degree_C", "group_temperature"))
20.0°C
@@ -1135,7 +1136,7 @@ def convert(val_t, target_unit_type):
# Try converting a sequence first. A TypeError exception will occur if
# the value is actually a scalar:
try:
new_val = map(lambda x : conversion_func(x) if x is not None else None, val_t[0])
new_val = list(map(lambda x : conversion_func(x) if x is not None else None, val_t[0]))
except TypeError:
new_val = conversion_func(val_t[0]) if val_t[0] is not None else None
# Add on the unit type and the group type and return the results:
@@ -1245,13 +1246,16 @@ class GenWithConvert(object):
def __iter__(self):
return self
def next(self):
_record = self.input_generator.next()
if self.target_unit_system is None or _record['usUnits'] == self.target_unit_system:
def __next__(self):
_record = next(self.input_generator)
if self.target_unit_system is None:
return _record
_record_c = StdUnitConverters[self.target_unit_system].convertDict(_record)
_record_c['usUnits'] = self.target_unit_system
return _record_c
else:
return to_std_system(_record, self.target_unit_system)
# For Python 2:
next = __next__
def to_US(datadict):
"""Convert the units used in a dictionary to US Customary."""