1515
1616def parse_delivery_time (abstract_weather_code , time_string ):
1717 """
18- Parses the delivery time of a METAR/TAF
18+ Parses the delivery time of a METAR/TAF. It will return False
19+ if it is not a delivery time but a validity time. If the delivery time
20+ is not specified, we can assume the start of the validity time is the delivery time.
1921 :param abstract_weather_code: The TAF or METAR object
2022 :param time_string: The string representing the delivery time
2123 :return: None
2224 """
23- abstract_weather_code .day = int (time_string [0 :2 ])
24- abstract_weather_code .time = time (int (time_string [2 :4 ]), int (time_string [4 :6 ]))
25+ if len (time_string ) > 6 and "/" in time_string :
26+ # This is a validity string, not a delivery time.
27+ abstract_weather_code .day = int (time_string [0 :2 ])
28+ abstract_weather_code .time = time (hour = int (time_string [2 :4 ]))
29+ return False
30+ else :
31+ abstract_weather_code .day = int (time_string [0 :2 ])
32+ abstract_weather_code .time = time (int (time_string [2 :4 ]), int (time_string [4 :6 ]))
33+ return True
2534
2635
2736def _parse_flags (abstract_weather_code , flag_string ):
@@ -246,12 +255,7 @@ def __init__(self):
246255 self ._validity_pattern = re .compile (r'^\d{4}/\d{4}$' )
247256 self ._taf_command_supplier = TAFCommandSupplier ()
248257
249- def parse (self , input : str ):
250- """
251- Parses a message into a TAF
252- :param input: the message to parse
253- :return: a TAF object or None if the message is invalid
254- """
258+ def _parse_initial_taf (self , input : str ):
255259 taf = TAF ()
256260 lines = self ._extract_lines_tokens (input )
257261 if TAFParser .TAF != lines [0 ][0 ]:
@@ -265,10 +269,20 @@ def parse(self, input: str):
265269 taf .station = lines [0 ][index ]
266270 index += 1
267271 taf .message = input
268- parse_delivery_time (taf , lines [0 ][index ])
269- index += 1
272+ if parse_delivery_time (taf , lines [0 ][index ]):
273+ index += 1
270274 taf .validity = _parse_validity (lines [0 ][index ])
271275
276+ return taf , lines , index
277+
278+ def parse (self , input : str ):
279+ """
280+ Parses a message into a TAF
281+ :param input: the message to parse
282+ :return: a TAF object or None if the message is invalid
283+ """
284+ taf , lines , index = self ._parse_initial_taf (input )
285+
272286 for i in range (index + 1 , len (lines [0 ])):
273287 token = lines [0 ][i ]
274288 command = self ._taf_command_supplier .get (token )
@@ -311,7 +325,7 @@ def _extract_lines_tokens(self, taf_code: str):
311325 lines_token [len (lines ) - 1 ] = list (filter (lambda x : not x .startswith (TAFParser .TX ) and not x .startswith (TAFParser .TN ), last_line ))
312326 return lines_token
313327
314- def _parse_line (self , taf : TAF , line_tokens : list ):
328+ def _parse_line (self , taf : ' TAF' , line_tokens : list ):
315329 """
316330 Parses the tokens of the line and updates the TAF object.
317331 :param taf: TAF object to update
0 commit comments