Class: Tareek::Dates

Inherits:
Object
  • Object
show all
Defined in:
lib/tareek.rb

Constant Summary collapse

DAYS_IN_A_MONTH =
[nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
MONTHS =
[nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct","Nov", "Dec"]

Class Method Summary collapse

Class Method Details

.convert_to_date(date) ⇒ Object



66
67
68
# File 'lib/tareek.rb', line 66

def convert_to_date(date)
  (date.kind_of? Date) ? date : Date.parse(date)
end

.date_of_next(day) ⇒ Object



36
37
38
39
40
# File 'lib/tareek.rb', line 36

def date_of_next(day)
  date  = convert_to_date(day)
  delta = date > Date.today ? 0 : 7
  date + delta
end

.day_middle_of_next_month_date(year, month) ⇒ Object



24
25
26
# File 'lib/tareek.rb', line 24

def day_middle_of_next_month_date(year, month)
  day_on(get_middle_of_next_month_date(year, month))
end

.day_middle_of_past_month_date(year, month) ⇒ Object



28
29
30
# File 'lib/tareek.rb', line 28

def day_middle_of_past_month_date(year, month)
  day_on(get_middle_of_past_month_date(year, month))
end

.day_on(date) ⇒ Object



42
43
44
# File 'lib/tareek.rb', line 42

def day_on(date)
  convert_to_date(date).strftime("%A")
end

.days_in_a_month_of(month, year) ⇒ Object



86
87
88
89
90
# File 'lib/tareek.rb', line 86

def days_in_a_month_of(month, year)
 month = MONTHS.index(month)
 return 29 if(month == 2 && Date.leap?(year.to_i))
 DAYS_IN_A_MONTH[month] 
end

.feb_month_of_28_days_for?(year) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/tareek.rb', line 92

def feb_month_of_28_days_for?(year)
 days_in_a_month_of("Feb", year.to_i) == 28
end

.get_middle_of_next_month_date(year, month) ⇒ Object



16
17
18
# File 'lib/tareek.rb', line 16

def get_middle_of_next_month_date(year, month)
  (parse_date(month,year) + 45 * 24 * 60 * 60).to_date
end

.get_middle_of_past_month_date(year, month) ⇒ Object



20
21
22
# File 'lib/tareek.rb', line 20

def get_middle_of_past_month_date(year, month)
  (parse_date(month,year) - 15 * 24 * 60 * 60).to_date
end

.humanize_with_day(date) ⇒ Object



50
51
52
# File 'lib/tareek.rb', line 50

def humanize_with_day(date)
  convert_to_date(date).strftime('%d-%b-%y')
end

.humanize_with_day_and_full_year(date) ⇒ Object



54
55
56
# File 'lib/tareek.rb', line 54

def humanize_with_day_and_full_year(date)
  convert_to_date(date).strftime('%d %b %Y')
end

.humanize_without_day(date) ⇒ Object



46
47
48
# File 'lib/tareek.rb', line 46

def humanize_without_day(date)
  convert_to_date(date).strftime('%b %y')
end

.omniture_formatted_with_time(date) ⇒ Object



62
63
64
# File 'lib/tareek.rb', line 62

def omniture_formatted_with_time(date)
  convert_to_date(date).strftime('%m/%d/%Y %I:%M:%S %p')
end

.parse_date(month, year) ⇒ Object



32
33
34
# File 'lib/tareek.rb', line 32

def parse_date(month,year)
  Time.parse("1-#{month}-#{year} 0:1 UTC")
end

.time_at_day(date) ⇒ Object



58
59
60
# File 'lib/tareek.rb', line 58

def time_at_day(date)
  convert_to_date(date).strftime('%I:%M %p')
end

.today_date_in_dd_mm_yy_formatObject



70
71
72
# File 'lib/tareek.rb', line 70

def today_date_in_dd_mm_yy_format
 Time.now.strftime('%d-%m-%Y')
end

.today_date_in_mm_dd_yy_formatObject



74
75
76
# File 'lib/tareek.rb', line 74

def today_date_in_mm_dd_yy_format
 Time.now.strftime('%m-%d-%Y')
end

.weekday?(day) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/tareek.rb', line 82

def weekday?(day)
 !(weekend?(day))
end

.weekend?(day) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/tareek.rb', line 78

def weekend?(day)
 [0,6].include?(convert_to_date(day).wday) 
end