Sha256: b49abbcfd642d5aca5603e6bd74ac37594a838a8390b0d47c9a8d2eba34572a9

Contents?: true

Size: 654 Bytes

Versions: 1

Compression:

Stored size: 654 Bytes

Contents

# frozen_string_literal: true

module ChronoModel
  module Conversions
    module_function

    ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(?:\.(\d+))?\z/

    # rubocop:disable Style/PerlBackrefs
    def string_to_utc_time(string)
      return string if string.is_a?(Time)

      return unless string =~ ISO_DATETIME

      # .1 is .100000, not .000001
      usec = $7.ljust(6, '0') unless $7.nil?

      Time.utc $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, usec.to_i
    end
    # rubocop:enable Style/PerlBackrefs

    def time_to_utc_string(time)
      time.to_fs(:db) << '.' << format('%06d', time.usec)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chrono_model-3.0.1 lib/chrono_model/conversions.rb