lib/vpim/date.rb in vpim-0.360 vs lib/vpim/date.rb in vpim-0.597

- old
+ new

@@ -1,7 +1,7 @@ =begin - Copyright (C) 2006 Sam Roberts + Copyright (C) 2008 Sam Roberts This library is free software; you can redistribute it and/or modify it under the same terms as the ruby language itself, see the file COPYING for details. =end @@ -75,12 +75,12 @@ # - Date.bywday(2004, 12, 1) => the first sunday in the 12th month of 2004 # - Date.bywday(2004, 2, 2, -1) => last Tuesday in the 2nd month in 2004 # - Date.bywday(2004, -2, 3, -2) => second last Wednesday in the second last month of 2004 # # Compare this to Date.new, which allows a Date to be created by - # day-of-the-month, mday, to Date.new2, which allows a Date to be created by - # day-of-the-year, yday, and to Date.neww, which allows a Date to be created + # day-of-the-month, mday, to Date.ordinal, which allows a Date to be created by + # day-of-the-year, yday, and to Date.commercial, which allows a Date to be created # by day-of-the-week, but within a specific week. def Date.bywday(year, mon, wday, n = 1, sg=Date::ITALY) # Normalize mon to 1-12. if mon if mon > 12 || mon == 0 || mon < -12 @@ -118,13 +118,39 @@ if mon && d.mon != mon raise ArgumentError, 'n is out of bounds of month' end d end + + # Return the first day of the week for the specified date. Commercial weeks + # start on Monday, but the weekstart can be specified (as 0-6, where 0 is + # sunday, or in formate of Date.str2day). + def Date.weekstart(year, mon, day, weekstart="MO") + wkst = Date.str2wday(weekstart) + d = Date.new(year, mon, day) + until d.wday == wkst + d = d - 1 + end + d + end end # DateGen generates arrays of dates matching simple criteria. class DateGen + + # Generate an array of a week's dates, where week is specified by year, mon, + # day, and the weekstart (the day-of-week that is considered the "first" day + # of that week, 0-6, where 0 is sunday). + def DateGen.weekofdate(year, mon, day, weekstart) + d = Date.weekstart(year, mon, day, weekstart) + week = [] + 7.times do + week << d + d = d + 1 + end + week + end + # Generate an array of dates on +wday+ (the day-of-week, # 0-6, where 0 is Sunday). # # If +n+ is specified, only the nth occurrence of +wday+ within the period # will be generated. If +n+ is positive, the nth occurence from the