1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MoinDateSupport.py Sun Jan 22 00:04:16 2012 +0100
1.3 @@ -0,0 +1,163 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - MoinDateSupport library (derived from EventAggregatorSupport)
1.7 +
1.8 + @copyright: 2008, 2009, 2010, 2011, 2012 by Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinSupport import *
1.13 +
1.14 +__version__ = "0.1"
1.15 +
1.16 +# User interface functions.
1.17 +
1.18 +def getParameterDate(arg):
1.19 +
1.20 + "Interpret 'arg', recognising keywords and simple arithmetic operations."
1.21 +
1.22 + n = None
1.23 +
1.24 + if arg is None:
1.25 + return None
1.26 +
1.27 + elif arg.startswith("current"):
1.28 + date = getCurrentDate()
1.29 + if len(arg) > 8:
1.30 + n = int(arg[7:])
1.31 +
1.32 + elif arg.startswith("yearstart"):
1.33 + date = Date((getCurrentYear(), 1, 1))
1.34 + if len(arg) > 10:
1.35 + n = int(arg[9:])
1.36 +
1.37 + elif arg.startswith("yearend"):
1.38 + date = Date((getCurrentYear(), 12, 31))
1.39 + if len(arg) > 8:
1.40 + n = int(arg[7:])
1.41 +
1.42 + else:
1.43 + date = getDate(arg)
1.44 +
1.45 + if n is not None:
1.46 + date = date.day_update(n)
1.47 +
1.48 + return date
1.49 +
1.50 +def getParameterMonth(arg):
1.51 +
1.52 + "Interpret 'arg', recognising keywords and simple arithmetic operations."
1.53 +
1.54 + n = None
1.55 +
1.56 + if arg is None:
1.57 + return None
1.58 +
1.59 + elif arg.startswith("current"):
1.60 + date = getCurrentMonth()
1.61 + if len(arg) > 8:
1.62 + n = int(arg[7:])
1.63 +
1.64 + elif arg.startswith("yearstart"):
1.65 + date = Month((getCurrentYear(), 1))
1.66 + if len(arg) > 10:
1.67 + n = int(arg[9:])
1.68 +
1.69 + elif arg.startswith("yearend"):
1.70 + date = Month((getCurrentYear(), 12))
1.71 + if len(arg) > 8:
1.72 + n = int(arg[7:])
1.73 +
1.74 + else:
1.75 + date = getMonth(arg)
1.76 +
1.77 + if n is not None:
1.78 + date = date.month_update(n)
1.79 +
1.80 + return date
1.81 +
1.82 +def getFormDate(request, calendar_name, argname):
1.83 +
1.84 + """
1.85 + Return the date from the 'request' for the calendar with the given
1.86 + 'calendar_name' using the parameter having the given 'argname'.
1.87 + """
1.88 +
1.89 + arg = getQualifiedParameter(request, calendar_name, argname)
1.90 + return getParameterDate(arg)
1.91 +
1.92 +def getFormMonth(request, calendar_name, argname):
1.93 +
1.94 + """
1.95 + Return the month from the 'request' for the calendar with the given
1.96 + 'calendar_name' using the parameter having the given 'argname'.
1.97 + """
1.98 +
1.99 + arg = getQualifiedParameter(request, calendar_name, argname)
1.100 + return getParameterMonth(arg)
1.101 +
1.102 +def getFormDateTriple(request, yeararg, montharg, dayarg):
1.103 +
1.104 + """
1.105 + Return the date from the 'request' for the calendar with the given
1.106 + 'calendar_name' using the parameters having the given 'yeararg', 'montharg'
1.107 + and 'dayarg' names.
1.108 + """
1.109 +
1.110 + year = getParameter(request, yeararg)
1.111 + month = getParameter(request, montharg)
1.112 + day = getParameter(request, dayarg)
1.113 + if year and month and day:
1.114 + return Date((int(year), int(month), int(day)))
1.115 + else:
1.116 + return None
1.117 +
1.118 +def getFormMonthPair(request, yeararg, montharg):
1.119 +
1.120 + """
1.121 + Return the month from the 'request' for the calendar with the given
1.122 + 'calendar_name' using the parameters having the given 'yeararg' and
1.123 + 'montharg' names.
1.124 + """
1.125 +
1.126 + year = getParameter(request, yeararg)
1.127 + month = getParameter(request, montharg)
1.128 + if year and month:
1.129 + return Month((int(year), int(month)))
1.130 + else:
1.131 + return None
1.132 +
1.133 +def getFullDateLabel(request, date):
1.134 +
1.135 + """
1.136 + Return the full month plus year label using the given 'request' and
1.137 + 'year_month'.
1.138 + """
1.139 +
1.140 + if not date:
1.141 + return ""
1.142 +
1.143 + _ = request.getText
1.144 + year, month, day = date.as_tuple()[:3]
1.145 + start_weekday, number_of_days = date.month_properties()
1.146 + weekday = (start_weekday + day - 1) % 7
1.147 + day_label = _(getDayLabel(weekday))
1.148 + month_label = _(getMonthLabel(month))
1.149 + return "%s %s %s %s" % (day_label, day, month_label, year)
1.150 +
1.151 +def getFullMonthLabel(request, year_month):
1.152 +
1.153 + """
1.154 + Return the full month plus year label using the given 'request' and
1.155 + 'year_month'.
1.156 + """
1.157 +
1.158 + if not year_month:
1.159 + return ""
1.160 +
1.161 + _ = request.getText
1.162 + year, month = year_month.as_tuple()[:2]
1.163 + month_label = _(getMonthLabel(month))
1.164 + return "%s %s" % (month_label, year)
1.165 +
1.166 +# vim: tabstop=4 expandtab shiftwidth=4