Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/weather_fetcher/utils/time.rb

Class Method Summary collapse

Class Method Details

.create_time_from_string(date, time) ⇒ Object

Create Time from YYYY-MM-DD HH:mm string format

:call-seq:

Time.create_time_from_string( String date, String time ) => Time


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/weather_fetcher/utils/time.rb', line 39

def self.create_time_from_string(date, time)
  date =~ /(\d{4})-(\d{1,2})-(\d{1,2})/
  y = $1.to_i
  m = $2.to_i
  d = $3.to_i

  if time =~ /(\d{1,2}):(\d{1,2})/
    h = $1.to_i
    min = $2.to_i
  else
    h = 0
    min = 0
  end

  return Time.mktime(y, m, d, h, min, 0, 0)
end

.create_time_from_string_12_utc(date, time) ⇒ Object

Create Time from YYYY-MM-DD HH:mm A/PM string format

:call-seq:

Time.create_time_from_string( String date, String time ) => Time


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/weather_fetcher/utils/time.rb', line 6

def self.create_time_from_string_12_utc(date, time)
  if date.nil?
    y = Time.now.year
    m = Time.now.month
    d = Time.now.day
  else
    date =~ /(\d{4})-(\d{1,2})-(\d{1,2})/
    y = $1.to_i
    mo = $2.to_i
    d = $3.to_i
  end

  # hour
  time =~ /(\d{1,2}):(\d{1,2}) ([PM])M/
  h = $1.to_i
  m = $2.to_i

  if $3 == "P"
    h_plus = 12
  else
    h_plus = 0
  end
  h += h_plus
  h = h % 24

  return Time.utc(y, mo, d, h, m).localtime

end