Module: Windows::TimeCalcHelper

Included in:
Win32::TaskScheduler
Defined in:
lib/win32/windows/time_calc_helper.rb

Constant Summary collapse

DAYS_IN_A_MONTH =

Returns actual no of days for given month; Array with a 0 is defined to give actual result without any manipulation. eg, DAYS_IN_A_MONTH = 31 0(NUMBER) is kept to avoid exceptions during calculations

[0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Instance Method Summary collapse

Instance Method Details

#days_in_month(month, year) ⇒ Object

Returns no of days in a given month of a year



11
12
13
# File 'lib/win32/windows/time_calc_helper.rb', line 11

def days_in_month(month, year)
  (month == 2 && is_leap_year?(year)) ? 29 : DAYS_IN_A_MONTH[month]
end

#dt_tm_array_to_hash(arr, tm_detail) ⇒ Object

Method to convert date/time array to hash



126
127
128
# File 'lib/win32/windows/time_calc_helper.rb', line 126

def dt_tm_array_to_hash(arr, tm_detail)
  arr.split(/(\d+)/)[1..-1].each_slice(2).inject(tm_detail) { |h, i| h[i.last.to_sym] = i.first; h }
end

#extra_days(days_count, month_count, year_count, init_month, init_year) ⇒ Object

Returns no of actual days with all overloaded months & Years



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/win32/windows/time_calc_helper.rb', line 77

def extra_days(days_count, month_count, year_count, init_month, init_year)
  # Will keep increamenting them with surplus days

  days = days_count
  mth = init_month
  yr = init_year

  loop do
   days -= days_in_month(mth, yr)
   break if days <= 0
   mth += 1
   if mth > 12
     mth = 1; yr += 1
   end
   days_count = days
  end

  # Setting actual incremented values

  month_count += (mth - init_month)
  year_count += (yr - init_year)

  [days_count, month_count, year_count]
end

#extra_months(month_count, year_count, init_month, init_year) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/win32/windows/time_calc_helper.rb', line 66

def extra_months(month_count, year_count, init_month, init_year)
  year, month_count = month_count.divmod(12)
  if year.positive? && month_count.zero?
    month_count = 12
    year -= 1
  end
  year_count += year
  [month_count, year_count]
end

#extra_time(low_rank, high_rank, div_val) ⇒ Object

a will contain extra value of low_rank (in high_rank(eg min)); b will hold actual low_rank value(ie sec) Example: low_rank = 65, high_rank = 2, div_val = 60 Hence a = 1; b = 5



60
61
62
63
64
# File 'lib/win32/windows/time_calc_helper.rb', line 60

def extra_time(low_rank, high_rank, div_val)
  a, b = low_rank.divmod(div_val)
  high_rank += a; low_rank = b
  [low_rank, high_rank]
end

#is_leap_year?(year) ⇒ Boolean

Year is leap when it is a multiple of 4 and not a multiple of 100. But it can be a multiple of 400

Returns:

  • (Boolean)


17
18
19
# File 'lib/win32/windows/time_calc_helper.rb', line 17

def is_leap_year?(year)
  (((year % 4).zero? && !(year % 100).zero?) || (year % 400).zero?)
end

#time_details(time_str) ⇒ Object

Extracts “P_Y_M_DT_H_M_S” format and Returns a hash with applicable values of (keys =>) [:year, :month, :day, :hour, :min, :sec] Example: “PT3S” => 3



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/win32/windows/time_calc_helper.rb', line 104

def time_details(time_str)
  tm_detail = {}
  if time_str.to_s != ''
    # time_str will be like "PxxYxxMxxDTxxHxxMxxS"

    # Ignoring 'P' and extracting date and time

    dt, tm = time_str[1..-1].split('T')

    # Replacing strings

    if dt.to_s != ''
      dt['Y'] = 'year' if dt['Y']; dt['M'] = 'month' if dt['M']; dt['D'] = 'day' if dt['D']
      dt_tm_array_to_hash(dt, tm_detail)
    end

    if tm.to_s != ''
      tm['H'] = 'hour' if tm['H']; tm['M'] = 'min' if tm['M']; tm['S'] = 'sec' if tm['S']
      dt_tm_array_to_hash(tm, tm_detail)
    end
  end
  tm_detail
end

#time_in_minutes(time_str) ⇒ Object

Returns total time duration in minutes



22
23
24
# File 'lib/win32/windows/time_calc_helper.rb', line 22

def time_in_minutes(time_str)
  time_in_seconds(time_str) / 60
end

#time_in_seconds(time_str) ⇒ Object

Calculates total time duration in seconds



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/win32/windows/time_calc_helper.rb', line 27

def time_in_seconds(time_str)
  dt_tm_hash = time_details(time_str)
  curr_time = Time.now

  # Basic time variables

  future_year = curr_time.year + dt_tm_hash[:year].to_i
  future_month = curr_time.month + dt_tm_hash[:month].to_i
  future_day = curr_time.day + dt_tm_hash[:day].to_i
  future_hr = curr_time.hour + dt_tm_hash[:hour].to_i
  future_min = curr_time.min + dt_tm_hash[:min].to_i
  future_sec = curr_time.sec + dt_tm_hash[:sec].to_i

  # 'extra value' calculations for these time variables

  future_sec, future_min = extra_time(future_sec, future_min, 60)
  future_min, future_hr = extra_time(future_min, future_hr, 60)
  future_hr, future_day = extra_time(future_hr, future_day, 24)

  # explicit method to calculate overloaded days;

  # They may stretch upto years; heance leap year & months are into consideration

  future_day, future_month, future_year = extra_days(future_day, future_month, future_year, curr_time.month, curr_time.year)

  future_month, future_year = extra_months(future_month, future_year, curr_time.month, curr_time.year)

  future_time = Time.new(future_year, future_month, future_day, future_hr, future_min, future_sec)

  # Difference in time will return seconds

  future_time.to_i - curr_time.to_i
end