Class: DateTime

Inherits:
Object
  • Object
show all
Defined in:
lib/timesteps/grads.rb,
lib/timesteps/datetime_360day.rb,
lib/timesteps/datetime_noleap.rb,
lib/timesteps/datetime_allleap.rb,
lib/timesteps/datetime_timestep.rb,
lib/timesteps/datetime_parse_timestamp.rb

Defined Under Namespace

Classes: AllLeap, Fixed360Day, NoLeap

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_grads_time(time_string) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/timesteps/grads.rb', line 5

def self.parse_grads_time (time_string)
  if time_string.strip =~ /\A(((\d{2})?(:(\d{2}))?Z)?(\d{2}))?(\w{3})(\d{4})\z/i
    hour = $3 || "00"
    min  = $5 || "00"
    day  = $6 || "01"
    mon  = $7
    year = $8
    time = DateTime.parse("#{year}#{mon}#{day} #{hour}:#{min}:00")
  else
    raise "invalid time format"
  end
  return time
end

.parse_timestamp(spec, format: nil, offset: nil, calendar: "standard", tz: nil) ⇒ DateTimeFixedDPY

Parses the given datetime expression and creates an instance. ‘DateTime._parse()` is called internally.

Parameters:

  • spec (String)

Returns:

  • (DateTimeFixedDPY)


17
18
19
20
21
22
23
24
25
26
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/timesteps/datetime_parse_timestamp.rb', line 17

def self.parse_timestamp (spec, format: nil, offset: nil, calendar: "standard", tz: nil)
  case calendar.downcase.intern
  when :standard, :gregorian
    klass = DateTime
    start = Date::ITALY
  when :proleptic_gregorian
    klass = DateTime
    start = Date::GREGORIAN
  when :proleptic_julian, :julian
    klass = DateTime
    start = Date::JULIAN
  when :noleap, SYM_365_day
    klass = DateTime::NoLeap
    start = nil
  when :allleap, SYM_366_day
    klass = DateTime::AllLeap
    start = nil
  when SYM_360_day
    klass = DateTime::Fixed360Day
    start = nil
  end
  if format
    hash = DateTime._strptime(spec, format)
    raise "date-time string '#{spec}' doesn't match with the given format '#{format}'" unless hash
  else
    if spec =~ /\A([+\-]?\d{1,4})(\-(\d{1,2}))?(\s+(\w{3}|[+\-]\d{1,2}(:?\d{1,2})))?\z/
      year  = $1.to_i
      month = $3 ? $3.to_i : 1
      mday  = 1
      rest  = $4
      hash = DateTime._parse("#{year}-#{month}-#{mday} 00:00:00 #{rest}")
    else
      hash = DateTime._parse(spec)
    end
  end
  year, month, day, hour, minute, second, sec_fraction, offset_ = 
         hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset)
  hour   ||= 0
  minute ||= 0
  second ||= 0.0
  sec_fraction ||= 0.0
  if offset_ 
    offset = offset_.quo(86400)
  else
    offset ||= 0
  end
  if tz
    time = tz.local_datetime(year, month, day, hour, minute, second.to_i, sec_fraction.to_r)
  else
    if hour == 24 && minute == 0 && second == 0.0
      time = klass.new(year, month, day, 23, minute, second + sec_fraction, offset, start) + 1.quo(24)
    else
      time = klass.new(year, month, day, hour, minute, second + sec_fraction, offset, start)
    end    
  end
  return time
end

Instance Method Details

#next(*args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/timesteps/datetime_timestep.rb', line 28

def next (*args)
  case args.size
  when 1
    num = 1
    unit = args[0]
  when 2
    num = args[0]
    unit = args[1]
  else
    raise "invalid number of argumets"
  end
  return TimeStep.new(unit, since: self).time_at(num)
end

#prev(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/timesteps/datetime_timestep.rb', line 42

def prev (*args)
  case args.size
  when 1
    num = 1
    unit = args[0]
  when 2
    num = args[0]
    unit = args[1]
  else
    raise "invalid number of argumets"
  end
  return TimeStep.new(unit, since: self).time_at(-num)
end

#timeperiod(interval_spec, tz: nil, ends: "[]") ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/timesteps/datetime_timestep.rb', line 16

def timeperiod (interval_spec, tz: nil, ends: "[]")
  case start
  when Date::ITALY
    calendar = "standard"
  when Date::GREGORIAN
    calendar = "proleptic_gregorian"
  when Date::JULIAN
    calendar = "proleptic_julian"
  end
  return TimePeriod.new(interval_spec, since: self, calendar: calendar, ends: ends, tz: tz)
end

#timestep(interval_spec, tz: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/timesteps/datetime_timestep.rb', line 4

def timestep (interval_spec, tz: nil)
  case start
  when Date::ITALY
    calendar = "standard"
  when Date::GREGORIAN
    calendar = "proleptic_gregorian"
  when Date::JULIAN
    calendar = "proleptic_julian"
  end
  return TimeStep.new(interval_spec, since: self, calendar: calendar, tz: tz)
end