99
1010import time
1111
12- from moin .macros ._base import MacroInlineBase
12+ from moin .macros ._base import MacroInlineBase , fail_message
1313from moin .utils import show_time
14+ from moin .i18n import _
1415
1516
1617class MacroDateTimeBase (MacroInlineBase ):
@@ -19,10 +20,10 @@ def parse_time(self, args):
1920 Parse a time specification argument for usage by Date and DateTime macro.
2021 Not all ISO 8601 format variations are accepted as input.
2122
22- :param args: float/int UNIX timestamp or ISO 8601 formatted date time:
23+ :param args: float/int UNIX timestamp or null or ISO 8601 formatted date time:
2324 YYYY-MM-DDTHH:MM:SS (plus optional Z or z for UTC, or +/-HHMM) or
2425 YYYY-MM-DD HH:MM:SS (same as above but replacing T separator with " ")
25- :returns: UNIX timestamp (UTC)
26+ :returns: UNIX timestamp (UTC) or raises one of AttributeError, OSError, AssertionError, ValueError, OverflowError
2627 """
2728 if (
2829 len (args ) >= 19
@@ -32,41 +33,42 @@ def parse_time(self, args):
3233 and args [13 ] == ":"
3334 and args [16 ] == ":"
3435 ):
35- try :
36- year , month , day = int (args [0 :4 ]), int (args [5 :7 ]), int (args [8 :10 ])
37- hour , minute , second = int (args [11 :13 ]), int (args [14 :16 ]), int (args [17 :19 ])
38- tz = args [19 :] # +HHMM, -HHMM or Z or nothing (then we assume Z)
39- tzoffset = 0 # we assume UTC no matter if there is a Z
40- if tz :
41- sign = tz [0 ]
42- if sign in "+-\u2212 " : # ascii plus, ascii hyphen-minus, unicode minus
43- tzh , tzm = int (tz [1 :3 ]), int (tz [3 :])
44- tzoffset = (tzh * 60 + tzm ) * 60
45- if sign in "-\u2212 " : # ascii hyphen-minus, unicode minus
46- tzoffset = - tzoffset
47- tm = year , month , day , hour , minute , second , 0 , 0 , 0
48- except ValueError as err :
49- raise ValueError (f"Bad timestamp { args !r} : { err } " )
36+ year , month , day = int (args [0 :4 ]), int (args [5 :7 ]), int (args [8 :10 ])
37+ hour , minute , second = int (args [11 :13 ]), int (args [14 :16 ]), int (args [17 :19 ])
38+ tz = args [19 :] # +HHMM, -HHMM or Z or nothing (then we assume Z)
39+ tzoffset = 0 # we assume UTC no matter if there is a Z
40+ if tz :
41+ sign = tz [0 ]
42+ if sign in "+-\u2212 " : # ascii plus, ascii hyphen-minus, unicode minus
43+ tzh , tzm = int (tz [1 :3 ]), int (tz [3 :])
44+ tzoffset = (tzh * 60 + tzm ) * 60
45+ if sign in "-\u2212 " : # ascii hyphen-minus, unicode minus
46+ tzoffset = - tzoffset
47+ tm = year , month , day , hour , minute , second , 0 , 0 , 0
48+
5049 # as mktime wants a localtime argument (but we only have UTC),
5150 # we adjust by our local timezone's offset
52- try :
53- tm = time .mktime (tm ) - time .timezone - tzoffset
54- except (OverflowError , ValueError ):
55- tm = 0 # incorrect, but we avoid an ugly backtrace
51+ tm = time .mktime (tm ) - time .timezone - tzoffset
5652 else :
57- # try raw seconds since epoch in UTC
58- try :
59- tm = float (args )
60- except ValueError as err :
61- raise ValueError (f"Bad timestamp { args !r} : { err } " )
53+ tm = float (args )
6254 return tm
6355
6456
6557class Macro (MacroDateTimeBase ):
58+ """
59+ Return a date formatted per user settings or an error message if input is invalid.
60+ """
61+
6662 def macro (self , content , arguments , page_url , alternative ):
63+
6764 if arguments is None :
68- tm = None
65+ tm = None # show today's date
6966 else :
70- stamp = arguments [0 ]
71- tm = self .parse_time (stamp )
72- return show_time .format_date (tm )
67+ tm = arguments [0 ]
68+ try :
69+ if tm :
70+ tm = self .parse_time (tm )
71+ return show_time .format_date (tm )
72+ except (AttributeError , OSError , AssertionError , ValueError , OverflowError ):
73+ err_msg = _ ("Invalid input parameter: null, float, int, or ISO 8601 formats are accepted." )
74+ return fail_message (err_msg , alternative )
0 commit comments