antoniop
committed
on 27 Jan 23
Rework api to avoid extra checks
/tcl/acs-datetime-procs.tcl (+17 -54)
177 177
178 178     return [clock format [clock scan "last day" -base [clock scan $year-$month-01]] -format %d]
179 179 }
180 180
181 181 ad_proc -public dt_first_day_of_month {
182 182     year
183 183     month
184 184 } {
185 185     @return the weekday number of the first day for the given month/year
186 186 } {
187 187     # calendar widgets are expecting integers 1-7, so we must adjust
188 188     return [expr {[clock format [clock scan $year-$month-01] -format %w] + 1}]
189 189 }
190 190
191 191 ad_proc -public dt_next_month {
192 192     year
193 193     month
194 194 } {
195 195     @return the ANSI date for the next month
196 196 } {
197       if {$month == 12} {
198           incr year
199           set month 01
200       } else {
201           scan $month %d month
202           incr month
203       }
204  
205       # jarkko: added this check to avoid calendars bombing when prev month goes
206       # beyond borders
207       if {[catch {set next_month [clock format [clock scan $year-$month-01] -format %Y-%m-%d]} err]} {
  197     try {
  198         return [clock format [clock add [clock scan $year-$month-01] 1 month] -format %Y-%m-%d]
  199     } on error {errmsg} {
  200         ad_log warning "Cannot get next month date for $year-$month"
208 201         return ""
209 202     }
210       return $next_month
211 203 }
212 204
213 205 ad_proc -public dt_prev_month {
214 206     year
215 207     month
216 208 } {
217 209     @return the ANSI date for the previous month
218 210 } {
219       if {$month == 1} {
220           set year [expr {$year - 1}]
221           set month 12
222       } else {
223           scan $month %d month
224           set month [expr {$month - 1}]
225       }
226  
227       # jarkko: added this check to avoid calendars bombing when prev month goes
228       # beyond borders
229       if {[catch {set prev_month [clock format [clock scan $year-$month-01] -format %Y-%m-%d]} err]} {
  211     try {
  212         return [clock format [clock add [clock scan $year-$month-01] -1 month] -format %Y-%m-%d]
  213     } on error {errmsg} {
  214         ad_log warning "Cannot get previous month date for $year-$month"
230 215         return ""
231 216     }
232  
233       return $prev_month
234 217 }
235 218
236 219 ad_proc -public dt_next_month_name {
237 220     year
238 221     month
239 222 } {
240 223     @return Localized name of the next month
241 224 } {
242       if {$month == 12} {
243           incr year
244           set month 01
245       } else {
246           scan $month %d month
247           incr month
248       }
249  
250       # jarkko: added this check to avoid calendars bombing when next month goes
251       # beyond borders
252       if {[catch {set next_name [clock format [clock scan $year-$month-01] -format %B]} err]} {
  225     try {
  226         return [lc_time_fmt [lc_clock_to_ansi [clock add [clock scan $year-$month-01] 1 month]] "%B"]
  227     } on error {errmsg} {
  228         ad_log warning "Cannot get name of previous month for $year-$month"
253 229         return ""
254 230     }
255  
256       return [lc_time_fmt [lc_clock_to_ansi [clock scan $year-$month-01]] "%B"]
257  
258 231 }
259 232
260 233 ad_proc -public dt_prev_month_name {
261 234     year
262 235     month
263 236 } {
264 237     @return Localized name of the previous month
265 238 } {
266       if {$month == 1} {
267           set year [expr {$year - 1}]
268           set month 12
269       } else {
270           scan $month %d month
271           set month [expr {$month - 1}]
272       }
273  
274       # jarkko: added this check to avoid calendars bombing when prev month goes
275       # beyond borders
276  
277       if {[catch {set prev_name [clock format [clock scan $year-$month-01] -format %B]} err]} {
  239     try {
  240         return [lc_time_fmt [lc_clock_to_ansi [clock add [clock scan $year-$month-01] -1 month]] "%B"]
  241     } on error {errmsg} {
  242         ad_log warning "Cannot get name of previous month for $year-$month"
278 243         return ""
279 244     }
280  
281       return [lc_time_fmt [lc_clock_to_ansi [clock scan $year-$month-01]] "%B"]
282 245 }
283 246
284 247 ad_proc -public dt_widget_datetime {
285 248     {-show_date 1}
286 249     {-date_time_sep " "}
287 250     {-use_am_pm 0}
288 251     {-default none}
289 252     name
290 253     {granularity days}
291 254 } {
292 255
293 256     @return an HTML form fragment for collecting date-time
294 257     information with names "$name.year", "$name.month", "$name.day",
295 258     "$name.hours", "$name.minutes", "$name.seconds", and "$name.ampm".
296 259     These will be numeric ("ampm" is 0 for am, 1 for pm)
297 260
298 261     Default specifies what should be set as the current time in the
299 262     form. Valid defaults are "none", "now", or any valid date string
300 263     that can be converted with clock scan.
301 264