class DateTime

Constants

SYM_360_day

@private

SYM_365_day

@private

SYM_366_day

@private

Public Class Methods

parse_grads_time(time_string) click to toggle source
# 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) click to toggle source

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

@param spec [String]

@return [DateTimeFixedDPY]

# 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

Public Instance Methods

timeperiod(interval_spec, tz: nil, ends: "[]") click to toggle source
# 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) click to toggle source
# 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