module GoTime

formats Time like Golang

Constants

ANSIC
BASIC_CONVERT_REGEXP
BASIC_CONVERT_TABLE
Kitchen
Layout
RFC1123
RFC1123Z
RFC3339
RFC3339Nano
RFC822
RFC822Z
RFC850
RubyDate
Stamp
StampMicro
StampMilli
StampNano
UnixDate
VERSION

Public Class Methods

convert(fmt, exception: false) click to toggle source

Converts Go-like format string to strftime format string

@param fmt [String] Go-like format string @param exception [Boolean] If true, raise ArgumentError when there is a syntax that does not support Time#strftime. @return [String] strftime format string

# File lib/go_time.rb, line 51
def self.convert(fmt, exception: false)
  ret = fmt.gsub(BASIC_CONVERT_REGEXP, BASIC_CONVERT_TABLE)
  if exception
    matched = ret.match(Regexp.union(@convert_table.keys - BASIC_CONVERT_TABLE.keys))
    raise ArgumentError, %(unsupported syntax "#{matched}") if matched
  end
  ret
end
format(time, fmt) click to toggle source

Formats time according to Go-like format string

@param time [Time] @param fmt [String] Go-like format string @return [String] formatted string

# File lib/go_time.rb, line 15
def self.format(time, fmt)
  fmt.gsub(@convert_regexp) do |matched|
    conv = @convert_table[matched]
    case conv
    when String
      time.respond_to?(:_orig_strftime) ? time._orig_strftime(conv) : time.strftime(conv)
    when Proc
      conv.call(time, matched)
    end
  end
end
strftime(time, fmt) click to toggle source

Formats time according to Go-like format or strftime format string

@param time [Time] @param fmt [String] Go-like format or strftime format string @return [String] formatted string

# File lib/go_time.rb, line 32
def self.strftime(time, fmt)
  converted_fmt = fmt.gsub(@convert_regexp) do |matched|
    conv = @convert_table[matched]
    case conv
    when String
      conv
    when Proc
      conv.call(time, matched)
    end
  end

  time.respond_to?(:_orig_strftime) ? time._orig_strftime(converted_fmt) : time.strftime(converted_fmt)
end

Private Class Methods

update_convert_regexp() click to toggle source
# File lib/go_time.rb, line 60
def self.update_convert_regexp
  @convert_regexp = Regexp.union(@convert_table.keys)
end

Public Instance Methods

format(fmt) click to toggle source
# File lib/go_time/refine.rb, line 3
def format(fmt)
  GoTime.format(self, fmt)
end