File: datetime_parse_to_time.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (41 lines) | stat: -rw-r--r-- 1,372 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen-string-literal: true
#
# This switches the default parsing of strings into Time values
# from using Time.parse to using DateTime.parse.to_time.  This
# fixes issues when the times being parsed have no timezone
# information, the implicit timezone for the Database instance
# is set to +:utc+, and the timestamps being used include values
# not valid in the local timezone, such as during a daylight
# savings time switch.  
#
# To load the extension:
#
#   Sequel.extension :datetime_parse_to_time

#
module Sequel::DateTimeParseToTime
  private

  # Use DateTime.parse.to_time to do the conversion if the input a string and is assumed to
  # be in UTC and there is no offset information in the string.
  def convert_input_timestamp(v, input_timezone)
    if v.is_a?(String) && datetime_class == Time && input_timezone == :utc && !_date_parse(v).has_key?(:offset)
      # :nocov:
      # Whether this is fully branch covered depends on the order in which the specs are run.
      v = handle_date_parse_input(v) if respond_to?(:handle_date_parse_input, true)
      # :nocov:
      t = DateTime.parse(v).to_time
      case application_timezone
      when nil, :local
        t = t.localtime
      end
      t
    else
      super
    end
  rescue => e
    raise convert_exception_class(e, Sequel::InvalidValue)
  end
end

Sequel.extend(Sequel::DateTimeParseToTime)