Sha256: f1f795c04b6e1ed6d29cbd15bfd508a9619e9b687871d104c17c773b5b446082

Contents?: true

Size: 1.02 KB

Versions: 22

Compression:

Stored size: 1.02 KB

Contents

require 'tzinfo'

class BigqueryMigration
  class TimeWithZone
    # [+-]HH:MM, [+-]HHMM, [+-]HH
    NUMERIC_PATTERN = %r{\A[+-]\d\d(:?\d\d)?\z}

    # Region/Zone, Region/Zone/Zone
    NAME_PATTERN = %r{\A[^/]+/[^/]+(/[^/]+)?\z}

    class << self
      def time_with_zone(time, timezone)
        time.localtime(zone_offset(timezone))
      end

      def strptime_with_zone(date, format, timezone)
        time = Time.strptime(date, format)
        _utc_offset = time.utc_offset
        _zone_offset = zone_offset(timezone)
        time.localtime(_zone_offset) + _utc_offset - _zone_offset
      end

      private
      def zone_offset(timezone)
        if NUMERIC_PATTERN === timezone
          Time.zone_offset(timezone)
        elsif NAME_PATTERN === timezone
          tz = TZInfo::Timezone.get(timezone)
          tz.current_period.utc_total_offset
        elsif "UTC" == timezone # special treatment
          0
        else
          raise ArgumentError, "timezone format is invalid: #{timezone}"
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
bigquery_migration-0.3.2 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.3.1 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.3.0 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.2.2 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.2.1 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.2.0 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.2.0.pre2 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.2.0.pre1 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.7 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.6 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.5 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.4 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.3 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.2 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.1 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.0 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.0.pre6 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.0.pre5 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.0.pre4 lib/bigquery_migration/time_with_zone.rb
bigquery_migration-0.1.0.pre3 lib/bigquery_migration/time_with_zone.rb