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_ago ⇒ Object



135
136
137
# File 'lib/test-factory/date_factory.rb', line 135

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

#an_hour_ago ⇒ Object Also known as: last_hour



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

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"



104
105
106
# File 'lib/test-factory/date_factory.rb', line 104

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/test-factory/date_factory.rb', line 25

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



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

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

#hours_from_now(hours) ⇒ Object



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

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

#in_a_week ⇒ Object Also known as: next_week



130
131
132
# File 'lib/test-factory/date_factory.rb', line 130

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

#in_a_year ⇒ Object Also known as: next_year



117
118
119
# File 'lib/test-factory/date_factory.rb', line 117

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

#in_an_hour ⇒ Object Also known as: next_hour



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

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.



70
71
72
# File 'lib/test-factory/date_factory.rb', line 70

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

#last_month ⇒ Object



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

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

#last_year ⇒ Object Also known as: a_year_ago



63
64
65
# File 'lib/test-factory/date_factory.rb', line 63

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



149
150
151
152
153
154
155
156
# File 'lib/test-factory/date_factory.rb', line 149

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.



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

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

#minutes_from_now(mins) ⇒ Object



96
97
98
# File 'lib/test-factory/date_factory.rb', line 96

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

#next_monday ⇒ Object



139
140
141
# File 'lib/test-factory/date_factory.rb', line 139

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

#next_month ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/test-factory/date_factory.rb', line 108

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

#right_now ⇒ Object



54
55
56
# File 'lib/test-factory/date_factory.rb', line 54

def right_now
  date_factory(Time.now)
end

#tomorrow ⇒ Object



126
127
128
# File 'lib/test-factory/date_factory.rb', line 126

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

#yesterday ⇒ Object



122
123
124
# File 'lib/test-factory/date_factory.rb', line 122

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