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

Instance Method Details

#a_week_agoObject



89
90
91
# File 'lib/test-factory/date_factory.rb', line 89

def a_week_ago
  date_factory(Time.now - (3600*24*7))
end

#an_hour_agoObject Also known as: last_hour



6
7
8
# File 'lib/test-factory/date_factory.rb', line 6

def an_hour_ago
  date_factory(Time.now - 3600)
end

#current_monthObject

Returns the current month as an upper-case 3-letter string. example: “JUL”



59
60
61
# File 'lib/test-factory/date_factory.rb', line 59

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.

Parameters:

  • time_object (Time)

    the moment you want to convert



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/test-factory/date_factory.rb', line 115

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"
  }
end

#hours_ago(hours) ⇒ Object



36
37
38
# File 'lib/test-factory/date_factory.rb', line 36

def hours_ago(hours)
  date_factory(Time.now - hours*3600)
end

#hours_from_now(hours) ⇒ Object



40
41
42
# File 'lib/test-factory/date_factory.rb', line 40

def hours_from_now(hours)
  date_factory(Time.now + hours*3600)
end

#in_a_weekObject Also known as: next_week



84
85
86
# File 'lib/test-factory/date_factory.rb', line 84

def in_a_week
  date_factory(Time.now + (3600*24*7))
end

#in_a_yearObject



72
73
74
# File 'lib/test-factory/date_factory.rb', line 72

def in_a_year
  date_factory(Time.now + (3600*24*365))
end

#in_an_hourObject Also known as: next_hour



15
16
17
# File 'lib/test-factory/date_factory.rb', line 15

def in_an_hour
  date_factory(Time.now + 3600)
end

#in_the_last_yearObject

Returns a randomly selected date/time from within the last year.



27
28
29
# File 'lib/test-factory/date_factory.rb', line 27

def in_the_last_year
  date_factory(Time.random(:year_range=>1))
end

#last_monthObject



31
32
33
34
# File 'lib/test-factory/date_factory.rb', line 31

def last_month
  index = MONTHS.index(current_month)
  return MONTHS[index-1]
end

#last_yearObject Also known as: a_year_ago



20
21
22
# File 'lib/test-factory/date_factory.rb', line 20

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.

Parameters:

  • time_object (Time)

    the moment that you want converted to the string



102
103
104
105
106
107
108
109
# File 'lib/test-factory/date_factory.rb', line 102

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.



48
49
50
# File 'lib/test-factory/date_factory.rb', line 48

def minutes_ago(mins)
  date_factory(Time.now - mins*60)
end

#minutes_from_now(mins) ⇒ Object



52
53
54
# File 'lib/test-factory/date_factory.rb', line 52

def minutes_from_now(mins)
  date_factory(Time.now + mins*60)
end

#next_mondayObject



93
94
95
# File 'lib/test-factory/date_factory.rb', line 93

def next_monday
  date_factory(Time.at(Time.now+(8-Time.now.wday)*24*3600))
end

#next_monthObject



63
64
65
66
67
68
69
70
# File 'lib/test-factory/date_factory.rb', line 63

def next_month
  index = MONTHS.index(current_month)
  if index < 11
    return MONTHS[index+1]
  else
    return MONTHS[0]
  end
end

#right_nowObject



11
12
13
# File 'lib/test-factory/date_factory.rb', line 11

def right_now
  date_factory(Time.now)
end

#tomorrowObject



80
81
82
# File 'lib/test-factory/date_factory.rb', line 80

def tomorrow
  date_factory(Time.now + (3600*24))
end

#yesterdayObject



76
77
78
# File 'lib/test-factory/date_factory.rb', line 76

def yesterday
  date_factory(Time.now - (3600*24))
end