Module: TimeHelper

Defined in:
lib/monkey_patch_happy/time_helper.rb

Overview

计算相隔的时间。 初始化年月日的hash等等。

Instance Method Summary collapse

Instance Method Details

#apart_days(begin_time, end_time) ⇒ Object

两个日期之间多少天



36
37
38
# File 'lib/monkey_patch_happy/time_helper.rb', line 36

def apart_days(begin_time, end_time)
  days = [(end_time.to_date - begin_time.to_date).to_i + 1, 0].max
end

#apart_hours(begin_date, end_date) ⇒ Object

date.day之前总共多少小时



31
32
33
# File 'lib/monkey_patch_happy/time_helper.rb', line 31

def apart_hours(begin_date, end_date)
  hours = [(end_date - bbegin_date).to_i * 24 , 0].max
end

#apart_months(begin_time, end_time) ⇒ Object

两个日期之间多少个月



41
42
43
# File 'lib/monkey_patch_happy/time_helper.rb', line 41

def apart_months(begin_time, end_time)
  months = [((end_time.year * 12 + end_time.month) - (begin_time.year * 12 + begin_time.month) + 1), 0].max
end

#apart_years(begin_time, end_time) ⇒ Object

两个日期之间多少年



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

def apart_years(begin_time, end_time) 
  years = [(end_time.year - begin_time.year + 1), 0].max
end

#days_number(begin_time, end_time) ⇒ Object

两个日期之间各天的天数。比如1号有4天,2号有3天等。 result like : …



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/monkey_patch_happy/time_helper.rb', line 85

def days_number(begin_time, end_time)
  number_hash = get_31_days
  today = Date.today

  index = 0
  remaining_date = today
  while remaining_date.strftime('%Y-%m') >= begin_time.strftime('%Y-%m')
    Time.days_in_month(remaining_date.month).times do |index|
      number_hash[("d" + (index + 1).to_s).to_sym] += 1
    end
    index += 1
    remaining_date = end_time - index.month
  end
  
  #比如当前日期是2015-12-25号,那么应该减去26,27,28,29,30,31
  if end_time.strftime('%Y-%m') == today.strftime('%Y-%m')
    (Time.days_in_month(today.month) - today.day).times do  |index|
      number_hash[("d" + (today.day + index + 1).to_s).to_sym] -= 1
    end
  end

  #比如开始日期是2015-11-5 ,那么1,2,3,4号需要减掉
  (begin_time.day - 1).times do |index|
    number_hash[("d" + (index + 1).to_s).to_sym] -= 1
  end 
  
  number_hash
end

#get_12_months(default_num = 0) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/monkey_patch_happy/time_helper.rb', line 22

def get_12_months(default_num = 0)
  hash = {}
  12.times do |i|
    hash[("m" + (i+1).to_s).to_sym] = default_num
  end
  hash
end

#get_24_hours(default_num = 0) ⇒ Object

初始化hash



6
7
8
9
10
11
12
# File 'lib/monkey_patch_happy/time_helper.rb', line 6

def get_24_hours(default_num = 0)
  hash = {}
  24.times do |i|
    hash[("h" + (i+1).to_s).to_sym ] = default_num
  end
  hash
end

#get_31_days(default_num = 0) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/monkey_patch_happy/time_helper.rb', line 14

def get_31_days(default_num = 0)
  hash = {}
  31.times do |i|
    hash[("d" + (i+1).to_s).to_sym] = default_num
  end
  hash
end

#hours_number(begin_time, end_time) ⇒ Object

两个时间之间每个小时的个数。#1点3次,2点3次,3点3次,4点2次。 result like : …



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/monkey_patch_happy/time_helper.rb', line 58

def hours_number(begin_time, end_time)
  days = apart_days(begin_time, end_time) #相隔多少天
  number_hash = get_24_hours(days)  #因为每天都是24小时,所以直接用“天数 x 24”
  time_now = Time.now #用来判断所选日期是否是今天。

  #比如结束时间是当天的2015-11-5 20:00:00 ,那么21,22,23小时需要减掉
  if end_time.strftime('%Y-%m-%d') == time_now.strftime('%Y-%m-%d')
    #hour is 1..24h
    (24 - time_now.hour - 1).times do  |index|
      number_hash[("h" + (time_now.hour + index + 1 + 1 ).to_s).to_sym] -= 1
    end

    #hour is 0..23h
    # (24 - time_now.hour).times do  |index|
    #   number_hash[("h" + (time_now.hour + index + 1).to_s).to_sym] -= 1
    # end
  end

  #比如开始时间是2015-11-5 4:00:00 ,那么1,2,3小时需要减掉
  begin_time.hour.times do |index|
    number_hash[("h" + (index + 1).to_s).to_sym] -= 1
  end
  number_hash
end

#months_number(begin_time, end_time) ⇒ Object

两个日期之间,每个月的个数 result like : :m2=>2, :m3=>2, :m4=>2, :m5=>2, :m6=>2, :m7=>2, :m8=>3, :m9=>3, :m10=>3, :m11=>3, :m12=>3



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/monkey_patch_happy/time_helper.rb', line 116

def months_number(begin_time, end_time)
  number_hash = get_12_months
  today = Date.today

  index = 0
  remaining_date = end_time
  while remaining_date.strftime('%Y') >= begin_time.strftime('%Y')
    12.times do |index|
      number_hash[("m" + (index + 1).to_s).to_sym] += 1
    end
    
    index += 1
    remaining_date = end_time - index.year
  end

  #今年是2016,目前是1月份。那么2月..12月就需要去掉
  if end_time.strftime('%Y') == today.strftime('%Y')
    (12 - today.month).times do  |index|
      number_hash[("m" + (today.month + index + 1).to_s).to_sym] -= 1
    end
  end

  #比如开始日期是2015-5 ,那么1,2,3,4月需要减掉
  (begin_time.month - 1).times do |index|
    number_hash[("m" + (index + 1).to_s).to_sym] -= 1
  end

  number_hash
end

#time_change_to_date(begin_time, end_time) {|begin_date, end_date| ... } ⇒ Object

Yields:

  • (begin_date, end_date)


50
51
52
53
54
# File 'lib/monkey_patch_happy/time_helper.rb', line 50

def time_change_to_date(begin_time, end_time)
  begin_date = begin_time.to_date
  end_date = end_time.to_date
  yield begin_date, end_date
end