Class: ReeDatetime::Change

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/change.rb

Instance Method Summary collapse

Instance Method Details

#call(date_time, **opts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/change.rb', line 27

def call(date_time, **opts)
  if opts[:nsec]
    raise ArgumentError, "Can't change both :nsec and :usec at the same time" if opts[:usec]
    new_fraction = Rational(opts[:nsec], 1000000000).to_f
    raise ArgumentError, "argument out of range" if new_fraction >= 1
  elsif opts[:usec]
    new_fraction = Rational(opts[:usec], 1000000).to_f
    raise ArgumentError, "argument out of range" if new_fraction >= 1
  end

  DateTime.new(
    opts[:year] || date_time.year,
    opts[:month] || date_time.month,
    opts[:day] || date_time.day,
    opts[:hour] || date_time.hour,
    opts[:min] || date_time.min,
    (opts[:sec] || date_time.sec).to_f + (new_fraction || 0),
    opts[:offset] || date_time.offset
  )
end