Module: DateFactory
- Defined in:
- lib/test-factory/date_factory.rb
Overview
Some date and time helper functions.…
Constant Summary collapse
- MONTHS =
%w{JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC}
Instance Method Summary collapse
- #a_week_ago ⇒ Object
- #an_hour_ago ⇒ Object (also: #last_hour)
-
#current_month ⇒ Object
Returns the current month as an upper-case 3-letter string.
-
#date_factory(time_object) ⇒ Object
Takes a time object and returns a hash containing various parts of the relevant date.
- #hours_ago(hours) ⇒ Object
- #hours_from_now(hours) ⇒ Object
- #in_a_week ⇒ Object (also: #next_week)
- #in_a_year ⇒ Object (also: #next_year)
- #in_an_hour ⇒ Object (also: #next_hour)
-
#in_the_last_year ⇒ Object
Returns a randomly selected date/time from within the last year.
- #last_month ⇒ Object
- #last_year ⇒ Object (also: #a_year_ago)
-
#make_date(time_object) ⇒ Object
Formats a date string Sakai-style.
-
#minutes_ago(mins) ⇒ Object
Takes an integer representing the count of minutes as the parameter, and returns the date_factory hash for the resulting Time value.
- #minutes_from_now(mins) ⇒ Object
- #next_monday ⇒ Object
- #next_month ⇒ Object
- #right_now ⇒ Object
- #tomorrow ⇒ Object
- #yesterday ⇒ Object
Instance Method Details
#a_week_ago ⇒ Object
121 122 123 |
# File 'lib/test-factory/date_factory.rb', line 121 def a_week_ago date_factory(Time.now - (3600*24*7)) end |
#an_hour_ago ⇒ Object Also known as: last_hour
35 36 37 |
# File 'lib/test-factory/date_factory.rb', line 35 def an_hour_ago date_factory(Time.now - 3600) end |
#current_month ⇒ Object
Returns the current month as an upper-case 3-letter string. example: “JUL”
90 91 92 |
# File 'lib/test-factory/date_factory.rb', line 90 def current_month Time.now.strftime("%^b") end |
#date_factory(time_object) ⇒ Object
Takes a time object and returns a hash containing various parts of the relevant date.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/test-factory/date_factory.rb', line 11 def date_factory(time_object) { sakai: make_date(time_object), sakai_rounded: make_date(time_object).gsub!(/:\d+/, ":#{Time.at(time_object.to_i/(5*60)*(5*60)).strftime("%M")}"), # Date with time rounded to nearest 5-minute mark. short_date: time_object.strftime("%b %-d, %Y"), # => "Oct 18, 2013" samigo: time_object.strftime("%m/%d/%Y %I:%M:%S %p"), # => "10/30/2012 07:02:05 AM" MON: time_object.strftime("%^b"), # => "DEC" Mon: time_object.strftime("%b"), # => "Jan" Month: time_object.strftime("%B"), # => "February" month_int: time_object.month, # => 3 day_of_month: time_object.day, # => 17 Note this is not zero-padded weekday: time_object.strftime("%A"), # => "Monday" wkdy: time_object.strftime("%a"), # => "Tue" year: time_object.year, # => 2013 hour: time_object.strftime("%I").to_i, # => "07" Zero-padded, 12-hour clock minute: time_object.strftime("%M"), # => "02" Zero-padded minute_rounded: (Time.at(time_object.to_i/(5*60)*(5*60))).strftime("%M"), # => "05" Zero-padded, rounded to 5-minute increments meridian: time_object.strftime("%P"), # => "pm" MERIDIAN: time_object.strftime("%p"), # => "AM" date_w_slashes: time_object.strftime("%m/%d/%Y"), # => 02/08/2013 custom: time_object # => Allows creation of a custom date string using the passed time value. } end |
#hours_ago(hours) ⇒ Object
65 66 67 |
# File 'lib/test-factory/date_factory.rb', line 65 def hours_ago(hours) date_factory(Time.now - hours*3600) end |
#hours_from_now(hours) ⇒ Object
69 70 71 |
# File 'lib/test-factory/date_factory.rb', line 69 def hours_from_now(hours) date_factory(Time.now + hours*3600) end |
#in_a_week ⇒ Object Also known as: next_week
116 117 118 |
# File 'lib/test-factory/date_factory.rb', line 116 def in_a_week date_factory(Time.now + (3600*24*7)) end |
#in_a_year ⇒ Object Also known as: next_year
103 104 105 |
# File 'lib/test-factory/date_factory.rb', line 103 def in_a_year date_factory(Time.now + (3600*24*365)) end |
#in_an_hour ⇒ Object Also known as: next_hour
44 45 46 |
# File 'lib/test-factory/date_factory.rb', line 44 def in_an_hour date_factory(Time.now + 3600) end |
#in_the_last_year ⇒ Object
Returns a randomly selected date/time from within the last year.
56 57 58 |
# File 'lib/test-factory/date_factory.rb', line 56 def in_the_last_year date_factory(Time.random(:year_range=>1)) end |
#last_month ⇒ Object
60 61 62 63 |
# File 'lib/test-factory/date_factory.rb', line 60 def last_month index = MONTHS.index(current_month) return MONTHS[index-1] end |
#last_year ⇒ Object Also known as: a_year_ago
49 50 51 |
# File 'lib/test-factory/date_factory.rb', line 49 def last_year date_factory(Time.now - (3600*24*365)) end |
#make_date(time_object) ⇒ Object
Formats a date string Sakai-style. Useful for verifying creation dates and such.
135 136 137 138 139 140 141 142 |
# File 'lib/test-factory/date_factory.rb', line 135 def make_date(time_object) month = time_object.strftime("%b ") day = time_object.strftime("%-d") year = time_object.strftime(", %Y ") mins = time_object.strftime(":%M %P") hour = time_object.strftime("%l").to_i return month + day + year + hour.to_s + mins end |
#minutes_ago(mins) ⇒ Object
Takes an integer representing the count of minutes as the parameter, and returns the date_factory hash for the resulting Time value.
78 79 80 |
# File 'lib/test-factory/date_factory.rb', line 78 def minutes_ago(mins) date_factory(Time.now - mins*60) end |
#minutes_from_now(mins) ⇒ Object
82 83 84 |
# File 'lib/test-factory/date_factory.rb', line 82 def minutes_from_now(mins) date_factory(Time.now + mins*60) end |
#next_monday ⇒ Object
125 126 127 |
# File 'lib/test-factory/date_factory.rb', line 125 def next_monday date_factory(Time.at(Time.now+(8-Time.now.wday)*24*3600)) end |
#next_month ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/test-factory/date_factory.rb', line 94 def next_month index = MONTHS.index(current_month) if index < 11 return MONTHS[index+1] else return MONTHS[0] end end |
#right_now ⇒ Object
40 41 42 |
# File 'lib/test-factory/date_factory.rb', line 40 def right_now date_factory(Time.now) end |
#tomorrow ⇒ Object
112 113 114 |
# File 'lib/test-factory/date_factory.rb', line 112 def tomorrow date_factory(Time.now + (3600*24)) end |
#yesterday ⇒ Object
108 109 110 |
# File 'lib/test-factory/date_factory.rb', line 108 def yesterday date_factory(Time.now - (3600*24)) end |