Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the local time zone as the default #43

Merged
merged 6 commits into from
May 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions isodatetime/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ def __str__(self):
time_string = "-%02d:%02d"
return time_string % (abs(self.hours), abs(self.minutes))

def __repr__(self):
return "<isodatetime.data.TimeZone:" + repr(str(self)) + ">"


class TimePoint(object):

Expand Down Expand Up @@ -560,11 +563,11 @@ class TimePoint(object):
for leap seconds at 60 yet)
second_of_minute_decimal - a float between 0 and 1, if using decimal
accuracy for seconds.
time_zone_hour - (default 0) an integer denoting the hour timezone
time_zone_hour - (default 0) an integer denoting the hour time zone
offset from UTC. Note that unless this is a truncated
representation, 0 will be assumed if this is not provided.
time_zone_minute - (default 0) an integer between 0 and 59 denoting
the minute component of the timezone offset from UTC.
the minute component of the time zone offset from UTC.
dump_format - a custom format string to control the stringification
of the timepoint. See isodatetime.parser_spec for more details.
truncated - (default False) a boolean denoting whether the
Expand Down Expand Up @@ -916,8 +919,8 @@ def set_time_zone(self, dest_time_zone):
self.time_zone = dest_time_zone

def set_time_zone_to_local(self):
"""Set the time zone to the local timezone, if it's not already."""
local_hours, local_minutes = timezone.get_timezone_for_locale()
"""Set the time zone to the local time zone, if it's not already."""
local_hours, local_minutes = timezone.get_local_time_zone()
self.set_time_zone(TimeZone(hours=local_hours, minutes=local_minutes))

def set_time_zone_to_utc(self):
Expand Down
18 changes: 10 additions & 8 deletions isodatetime/dumpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TimePointDumper(object):
of valid patterns is found in the parser_spec module, with lots
of examples (coincidentally, used to generate the parsing).
Anything not matched will get left as it is in the string.
Specifying a particular timezone will result in a timezone
Specifying a particular time zone will result in a time zone
conversion of the date/time information before it is output.

For example, the following formatting_string
Expand All @@ -47,7 +47,7 @@ class TimePointDumper(object):
T - left alone, date/time separator
hh - hour of day information, e.g. 06
mm - minute of hour information, e.g. 58
Z - Zulu or UTC zero-offset timezone, left in, forces timezone
Z - Zulu or UTC zero-offset time zone, left in, forces time zone
conversion
and might dump a TimePoint instance like this: '19850531T0658Z'.

Expand All @@ -66,7 +66,7 @@ def __init__(self, num_expanded_year_digits=2):
num_expanded_year_digits),
"date"),
(parser_spec.get_time_translate_info(), "time"),
(parser_spec.get_timezone_translate_info(), "time_zone")]:
(parser_spec.get_time_zone_translate_info(), "time_zone")]:
for regex, regex_sub, format_sub, prop_name in info:
rec = re.compile(regex)
self._rec_formats[key].append((rec, format_sub, prop_name))
Expand Down Expand Up @@ -164,11 +164,11 @@ def _get_expression_and_properties(self, formatting_string):
elif "+" in time_string:
time_string, time_zone_string = time_string.split("+")
time_zone_string = "+" + time_zone_string
custom_time_zone = self._get_time_zone(time_zone_string)
custom_time_zone = self.get_time_zone(time_zone_string)
elif "-" in time_string.lstrip("-"):
time_string, time_zone_string = time_string.split("-")
time_zone_string = "-" + time_zone_string
custom_time_zone = self._get_time_zone(time_zone_string)
custom_time_zone = self.get_time_zone(time_zone_string)
point_prop_list = []
string_map = {"date": "", "time": "", "time_zone": ""}
for string, key in [(date_string, "date"),
Expand All @@ -187,16 +187,18 @@ def _get_expression_and_properties(self, formatting_string):
return expression, tuple(point_prop_list), custom_time_zone

@util.cache_results
def _get_time_zone(self, time_zone_string):
def get_time_zone(self, time_zone_string):
from . import parsers
if not hasattr(self, "_timepoint_parser"):
self._timepoint_parser = parsers.TimePointParser()
try:
(expr, info) = (
self._timepoint_parser.get_timezone_info(time_zone_string))
self._timepoint_parser.get_time_zone_info(time_zone_string))
except parsers.ISO8601SyntaxError as e:
return None
info = self._timepoint_parser.process_timezone_info(info)
info = self._timepoint_parser.process_time_zone_info(info)
if info.get('time_zone_utc'):
return (0, 0)
if "time_zone_hour" not in info and "time_zone_minute" not in info:
return None
return info.get("time_zone_hour", 0), info.get("time_zone_minute", 0)
26 changes: 13 additions & 13 deletions isodatetime/parser_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
--ss.tt # Deviation? Not allowed in standard ?
""" }
}
TIMEZONE_EXPRESSIONS = {
TIME_ZONE_EXPRESSIONS = {
"basic": u"""
Z
±hh
Expand Down Expand Up @@ -220,7 +220,7 @@
(u"^-", "(?P<truncated>-)",
"-", None)
]
_TIMEZONE_TRANSLATE_INFO = [
_TIME_ZONE_TRANSLATE_INFO = [
(u"(?<=±hh)mm", "(?P<time_zone_minute>\d\d)",
"%(time_zone_minute_abs)02d", "time_zone_minute_abs"),
(u"(?<=±hh:)mm", "(?P<time_zone_minute>\d\d)",
Expand All @@ -233,15 +233,15 @@
"Z", None)
]

LOCALE_TIMEZONE_BASIC = timezone.get_timezone_format_for_locale()
LOCALE_TIMEZONE_BASIC_NO_Z = LOCALE_TIMEZONE_BASIC
if LOCALE_TIMEZONE_BASIC_NO_Z == "Z":
LOCALE_TIMEZONE_BASIC_NO_Z = "+0000"
LOCALE_TIMEZONE_EXTENDED = timezone.get_timezone_format_for_locale(
LOCAL_TIME_ZONE_BASIC = timezone.get_local_time_zone_format()
LOCAL_TIME_ZONE_BASIC_NO_Z = LOCAL_TIME_ZONE_BASIC
if LOCAL_TIME_ZONE_BASIC_NO_Z == "Z":
LOCAL_TIME_ZONE_BASIC_NO_Z = "+0000"
LOCAL_TIME_ZONE_EXTENDED = timezone.get_local_time_zone_format(
extended_mode=True)
LOCALE_TIMEZONE_EXTENDED_NO_Z = LOCALE_TIMEZONE_EXTENDED
if LOCALE_TIMEZONE_EXTENDED_NO_Z == "Z":
LOCALE_TIMEZONE_EXTENDED_NO_Z = "+0000"
LOCAL_TIME_ZONE_EXTENDED_NO_Z = LOCAL_TIME_ZONE_EXTENDED
if LOCAL_TIME_ZONE_EXTENDED_NO_Z == "Z":
LOCAL_TIME_ZONE_EXTENDED_NO_Z = "+0000"

# Note: we only accept the following subset of strftime syntax.
# This is due to inconsistencies with the ISO 8601 representations.
Expand Down Expand Up @@ -294,8 +294,8 @@ def get_time_translate_info():
return _TIME_TRANSLATE_INFO


def get_timezone_translate_info():
return _TIMEZONE_TRANSLATE_INFO
def get_time_zone_translate_info():
return _TIME_ZONE_TRANSLATE_INFO


def translate_strftime_token(strftime_token, num_expanded_year_digits=2):
Expand Down Expand Up @@ -323,7 +323,7 @@ def _translate_strftime_token(strftime_token, dump_mode=False,
get_date_translate_info(
num_expanded_year_digits=num_expanded_year_digits) +
get_time_translate_info() +
get_timezone_translate_info()
get_time_zone_translate_info()
)
attr_names = STRFTIME_TRANSLATE_INFO[strftime_token]
if isinstance(attr_names, basestring):
Expand Down
Loading