Sha256: 3a53a55464b3fae6882853fd66ec0709ee062858838a2a4b3df784fc81389995
Contents?: true
Size: 1.94 KB
Versions: 10
Compression:
Stored size: 1.94 KB
Contents
require 'berkeley_library/util/times' require 'berkeley_library/tind/config' module BerkeleyLibrary module TIND module API class DateRange FORMAT = '%Y-%m-%d %H:%M:%S'.freeze attr_reader :from_time, :until_time def initialize(from_time:, until_time:, mtime: false) @from_time, @until_time = DateRange.ensure_valid_range(from_time, until_time) @mtime = mtime end def mtime? @mtime end def to_params { d1: format_param(from_time), d2: format_param(until_time) }.tap do |params| params[:dt] = 'm' if mtime? end end alias eql == def ==(other) return false unless other.class == self.class [from_time, until_time] == [other.from_time, other.until_time] end class << self def from_range(range) DateRange.new(from_time: range.first, until_time: range.last) end def ensure_date_range(date_range) return unless date_range return date_range if date_range.is_a?(DateRange) return DateRange.from_range(date_range) if date_range.respond_to?(:first) && date_range.respond_to?(:last) raise ArgumentError, "Can't convert #{date_range.inspect} to #{DateRange}" end def ensure_valid_range(from_time, until_time) ftime, utime = [from_time, until_time].map { |t| BerkeleyLibrary::Util::Times.ensure_utc(t) } return [ftime, utime] if ftime <= utime raise ArgumentError, "Not a valid range: #{from_time.inspect}..#{until_time.inspect}" end end private def format_param(t) tz = BerkeleyLibrary::TIND::Config.timezone t_utc = BerkeleyLibrary::Util::Times.ensure_utc(t) # just to be sure t_local = tz.utc_to_local(t_utc) t_local.strftime(FORMAT) end end end end end
Version data entries
10 entries across 10 versions & 1 rubygems