| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
"""This provides data model dumping functionality.""" |
| 20 |
|
| 21 |
import re |
| 22 |
|
| 23 |
from . import parser_spec |
| 24 |
|
| 25 |
from functools import lru_cache |
| 26 |
from metomi.isodatetime.exceptions import ( |
| 27 |
ISO8601SyntaxError, TimePointDumperBoundsError) |
| 28 |
|
| 29 |
|
| 30 |
class TimePointDumper(object): |
| 31 |
|
| 32 |
"""Dump TimePoint instances to strings using particular formats. |
| 33 |
|
| 34 |
A format can be specified in the self.dump method via the |
| 35 |
formatting_string argument. Unlike Python's datetime strftime |
| 36 |
method, this uses normal/Unicode character patterns to represent |
| 37 |
which pieces of information to output where. A full reference |
| 38 |
of valid patterns is found in the parser_spec module, with lots |
| 39 |
of examples (coincidentally, used to generate the parsing). |
| 40 |
Anything not matched will get left as it is in the string. |
| 41 |
Specifying a particular time zone will result in a time zone |
| 42 |
conversion of the date/time information before it is output. |
| 43 |
|
| 44 |
For example, the following formatting_string |
| 45 |
'CCYYMMDDThhmmZ' is made up of: |
| 46 |
CC - year (century) information, e.g. 19 |
| 47 |
YY - year (decade, year of decade) information, e.g. 85 |
| 48 |
MM - month of year information, e.g. 05 |
| 49 |
DD - day of month information, e.g. 31 |
| 50 |
T - left alone, date/time separator |
| 51 |
hh - hour of day information, e.g. 06 |
| 52 |
mm - minute of hour information, e.g. 58 |
| 53 |
Z - Zulu or UTC zero-offset time zone, left in, forces time zone |
| 54 |
conversion |
| 55 |
and might dump a TimePoint instance like this: '19850531T0658Z'. |
| 56 |
|
| 57 |
Keyword arguments: |
| 58 |
num_expanded_year_digits - an integer (default 2) that indicates |
| 59 |
how many extra year digits to apply if appropriate (and if the |
| 60 |
user requests that information). |
| 61 |
|
| 62 |
""" |
| 63 |
|
| 64 |
def __init__(self, num_expanded_year_digits=2): |
| 65 |
self._timepoint_parser = None |
| 66 |
self._rec_formats = {"date": [], "time": [], "time_zone": []} |
| 67 |
self._time_designator = parser_spec.TIME_DESIGNATOR |
| 68 |
self.num_expanded_year_digits = num_expanded_year_digits |
| 69 |
for info, key in [ |
| 70 |
(parser_spec.get_date_translate_info( |
| 71 |
num_expanded_year_digits), |
| 72 |
"date"), |
| 73 |
(parser_spec.get_time_translate_info(), "time"), |
| 74 |
(parser_spec.get_time_zone_translate_info(), "time_zone")]: |
| 75 |
for regex, _, format_sub, prop_name in info: |
| 76 |
rec = re.compile(regex) |
| 77 |
self._rec_formats[key].append((rec, format_sub, prop_name)) |
| 78 |
|
| 79 |
def dump(self, timepoint, formatting_string): |
| 80 |
"""Dump a timepoint according to formatting_string. |
| 81 |
|
| 82 |
The syntax for formatting_string is the syntax used for the |
| 83 |
TimePointParser internals. See TimePointParser.*_TRANSLATE_INFO. |
| 84 |
|
| 85 |
""" |
| 86 |
if "%" in formatting_string: |
| 87 |
try: |
| 88 |
return self.strftime(timepoint, formatting_string) |
| 89 |
except TimePointDumperBoundsError: |
| 90 |
raise |
| 91 |
except ValueError: |
| 92 |
pass |
| 93 |
expression, properties, custom_time_zone = ( |
| 94 |
self._get_expression_and_properties(formatting_string)) |
| 95 |
return self._dump_expression_with_properties( |
| 96 |
timepoint, expression, properties, |
| 97 |
custom_time_zone=custom_time_zone |
| 98 |
) |
| 99 |
|
| 100 |
def strftime(self, timepoint, formatting_string): |
| 101 |
"""Implement equivalent of Python 2's datetime.datetime.strftime. |
| 102 |
|
| 103 |
Dump timepoint based on the format given in formatting_string. |
| 104 |
|
| 105 |
""" |
| 106 |
split_format = parser_spec.REC_SPLIT_STRFTIME_DIRECTIVE.split( |
| 107 |
formatting_string) |
| 108 |
expression = "" |
| 109 |
properties = [] |
| 110 |
for item in split_format: |
| 111 |
if parser_spec.REC_STRFTIME_DIRECTIVE_TOKEN.search(item): |
| 112 |
item_expression, item_properties = ( |
| 113 |
parser_spec.translate_strftime_token(item)) |
| 114 |
expression += item_expression |
| 115 |
properties += item_properties |
| 116 |
else: |
| 117 |
expression += item |
| 118 |
return self._dump_expression_with_properties( |
| 119 |
timepoint, expression, properties) |
| 120 |
|
| 121 |
def _dump_expression_with_properties(self, timepoint, expression, |
| 122 |
properties, custom_time_zone=None): |
| 123 |
if not timepoint.truncated: |
| 124 |
if "week_of_year" in properties or "day_of_week" in properties: |
| 125 |
if not ("month_of_year" in properties or |
| 126 |
"day_of_month" in properties or |
| 127 |
"day_of_year" in properties): |
| 128 |
|
| 129 |
timepoint = timepoint.to_week_date() |
| 130 |
elif (timepoint.get_is_week_date() and ( |
| 131 |
"month_of_year" in properties or |
| 132 |
"day_of_month" in properties or |
| 133 |
"day_of_year" in properties)): |
| 134 |
|
| 135 |
timepoint = timepoint.to_calendar_date() |
| 136 |
|
| 137 |
if custom_time_zone is not None: |
| 138 |
if custom_time_zone == (0, 0): |
| 139 |
timepoint = timepoint.to_utc() |
| 140 |
else: |
| 141 |
from .data import TimeZone |
| 142 |
new_time_zone = TimeZone(hours=custom_time_zone[0], |
| 143 |
minutes=custom_time_zone[1]) |
| 144 |
timepoint = timepoint.to_time_zone(new_time_zone) |
| 145 |
property_map = {} |
| 146 |
for property_ in properties: |
| 147 |
property_map[property_] = getattr(timepoint, property_) |
| 148 |
if (property_ == "century" and |
| 149 |
("expanded_year_digits" not in properties or |
| 150 |
not self.num_expanded_year_digits)): |
| 151 |
min_value = 0 |
| 152 |
max_value = 9999 |
| 153 |
elif property_ == "expanded_year_digits": |
| 154 |
max_value = (10 ** (self.num_expanded_year_digits + 4)) - 1 |
| 155 |
min_value = -max_value |
| 156 |
else: |
| 157 |
continue |
| 158 |
value = timepoint.year |
| 159 |
if not (min_value <= value <= max_value): |
| 160 |
raise TimePointDumperBoundsError( |
| 161 |
"year", value, min_value, max_value) |
| 162 |
return expression % property_map |
| 163 |
|
| 164 |
@lru_cache(maxsize=100000) |
| 165 |
def _get_expression_and_properties(self, formatting_string): |
| 166 |
date_time_strings = formatting_string.split( |
| 167 |
self._time_designator) |
| 168 |
date_string = date_time_strings[0] |
| 169 |
time_string = "" |
| 170 |
time_zone_string = "" |
| 171 |
custom_time_zone = None |
| 172 |
has_time_string = len(date_time_strings) > 1 |
| 173 |
if has_time_string: |
| 174 |
time_string = date_time_strings[1] |
| 175 |
if time_string.endswith("Z"): |
| 176 |
time_string = time_string[:-1] |
| 177 |
time_zone_string = "Z" |
| 178 |
custom_time_zone = (0, 0) |
| 179 |
elif "+hh" in time_string: |
| 180 |
time_string, time_zone_string = time_string.split("+") |
| 181 |
time_zone_string = "+" + time_zone_string |
| 182 |
elif "+" in time_string: |
| 183 |
time_string, time_zone_string = time_string.split("+") |
| 184 |
time_zone_string = "+" + time_zone_string |
| 185 |
custom_time_zone = self.get_time_zone(time_zone_string) |
| 186 |
elif "-" in time_string.lstrip("-"): |
| 187 |
time_string, time_zone_string = time_string.split("-") |
| 188 |
time_zone_string = "-" + time_zone_string |
| 189 |
custom_time_zone = self.get_time_zone(time_zone_string) |
| 190 |
point_prop_list = [] |
| 191 |
string_map = {"date": "", "time": "", "time_zone": ""} |
| 192 |
for string, key in [(date_string, "date"), |
| 193 |
(time_string, "time"), |
| 194 |
(time_zone_string, "time_zone")]: |
| 195 |
for rec, format_sub, prop in self._rec_formats[key]: |
| 196 |
new_string = rec.sub(format_sub, string) |
| 197 |
if new_string != string and prop is not None: |
| 198 |
point_prop_list.append(prop) |
| 199 |
string = new_string |
| 200 |
string_map[key] = string |
| 201 |
expression = string_map["date"] |
| 202 |
if has_time_string: |
| 203 |
expression += self._time_designator + string_map["time"] + \ |
| 204 |
string_map["time_zone"] |
| 205 |
return expression, tuple(point_prop_list), custom_time_zone |
| 206 |
|
| 207 |
@lru_cache(maxsize=100000) |
| 208 |
def get_time_zone(self, time_zone_string): |
| 209 |
"""Parse and return time zone from time_zone_string.""" |
| 210 |
from . import parsers |
| 211 |
if self._timepoint_parser is None: |
| 212 |
self._timepoint_parser = parsers.TimePointParser() |
| 213 |
try: |
| 214 |
info = self._timepoint_parser.get_time_zone_info( |
| 215 |
time_zone_string)[1] |
| 216 |
except ISO8601SyntaxError: |
| 217 |
return None |
| 218 |
info = self._timepoint_parser.process_time_zone_info(info) |
| 219 |
if info.get('time_zone_utc'): |
| 220 |
return 0, 0 |
| 221 |
if "time_zone_hour" not in info and "time_zone_minute" not in info: |
| 222 |
return None |
| 223 |
hour = int(info.get("time_zone_hour", 0)) |
| 224 |
minute = int(info.get("time_zone_minute", 0)) |
| 225 |
return hour, minute |