1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - MoinDateSupport library (derived from EventAggregatorSupport) 4 5 @copyright: 2008, 2009, 2010, 2011, 2012 by Paul Boddie <paul@boddie.org.uk> 6 @license: GNU GPL (v2 or later), see COPYING.txt for details. 7 """ 8 9 from MoinSupport import * 10 11 __version__ = "0.1" 12 13 # User interface functions. 14 15 def getParameterDate(arg): 16 17 "Interpret 'arg', recognising keywords and simple arithmetic operations." 18 19 n = None 20 21 if not arg: 22 return None 23 24 elif arg.startswith("current"): 25 date = getCurrentDate() 26 if len(arg) > 8: 27 n = int(arg[7:]) 28 29 elif arg.startswith("yearstart"): 30 date = Date((getCurrentYear(), 1, 1)) 31 if len(arg) > 10: 32 n = int(arg[9:]) 33 34 elif arg.startswith("yearend"): 35 date = Date((getCurrentYear(), 12, 31)) 36 if len(arg) > 8: 37 n = int(arg[7:]) 38 39 else: 40 date = getDate(arg) 41 42 if n is not None: 43 date = date.day_update(n) 44 45 return date 46 47 def getParameterMonth(arg): 48 49 "Interpret 'arg', recognising keywords and simple arithmetic operations." 50 51 n = None 52 53 if arg is None: 54 return None 55 56 elif arg.startswith("current"): 57 date = getCurrentMonth() 58 if len(arg) > 8: 59 n = int(arg[7:]) 60 61 elif arg.startswith("yearstart"): 62 date = Month((getCurrentYear(), 1)) 63 if len(arg) > 10: 64 n = int(arg[9:]) 65 66 elif arg.startswith("yearend"): 67 date = Month((getCurrentYear(), 12)) 68 if len(arg) > 8: 69 n = int(arg[7:]) 70 71 else: 72 date = getMonth(arg) 73 74 if n is not None: 75 date = date.month_update(n) 76 77 return date 78 79 def getFormDate(request, calendar_name, argname): 80 81 """ 82 Return the date from the 'request' for the calendar with the given 83 'calendar_name' using the parameter having the given 'argname'. 84 """ 85 86 arg = getQualifiedParameter(request, calendar_name, argname) 87 return getParameterDate(arg) 88 89 def getFormMonth(request, calendar_name, argname): 90 91 """ 92 Return the month from the 'request' for the calendar with the given 93 'calendar_name' using the parameter having the given 'argname'. 94 """ 95 96 arg = getQualifiedParameter(request, calendar_name, argname) 97 return getParameterMonth(arg) 98 99 def getFormDateTriple(request, yeararg, montharg, dayarg): 100 101 """ 102 Return the date from the 'request' for the calendar with the given 103 'calendar_name' using the parameters having the given 'yeararg', 'montharg' 104 and 'dayarg' names. 105 """ 106 107 year = getParameter(request, yeararg) 108 month = getParameter(request, montharg) 109 day = getParameter(request, dayarg) 110 if year and month and day: 111 return Date((int(year), int(month), int(day))) 112 else: 113 return None 114 115 def getFormMonthPair(request, yeararg, montharg): 116 117 """ 118 Return the month from the 'request' for the calendar with the given 119 'calendar_name' using the parameters having the given 'yeararg' and 120 'montharg' names. 121 """ 122 123 year = getParameter(request, yeararg) 124 month = getParameter(request, montharg) 125 if year and month: 126 return Month((int(year), int(month))) 127 else: 128 return None 129 130 def getFullDateLabel(request, date): 131 132 """ 133 Return the full month plus year label using the given 'request' and 134 'year_month'. 135 """ 136 137 if not date: 138 return "" 139 140 _ = request.getText 141 year, month, day = date.as_tuple()[:3] 142 start_weekday, number_of_days = date.month_properties() 143 weekday = (start_weekday + day - 1) % 7 144 day_label = _(getDayLabel(weekday)) 145 month_label = _(getMonthLabel(month)) 146 return "%s %s %s %s" % (day_label, day, month_label, year) 147 148 def getFullMonthLabel(request, year_month): 149 150 """ 151 Return the full month plus year label using the given 'request' and 152 'year_month'. 153 """ 154 155 if not year_month: 156 return "" 157 158 _ = request.getText 159 year, month = year_month.as_tuple()[:2] 160 month_label = _(getMonthLabel(month)) 161 return "%s %s" % (month_label, year) 162 163 # vim: tabstop=4 expandtab shiftwidth=4