Class: Synthra::Types::DateTime::Timestamp

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/types/date_time/dates.rb

Overview

Timestamp type

Instance Method Summary collapse

Instance Method Details

#generate_edge(rng, context, args) ⇒ Object



145
146
147
148
# File 'lib/synthra/types/date_time/dates.rb', line 145

def generate_edge(rng, context, args)
  reference = TimeTravel.current_time&.current_time || Time.now
  [Time.at(0), reference].sample(random: rng.instance_variable_get(:@random))
end

#generate_invalid(rng, context, args) ⇒ Object



150
151
152
# File 'lib/synthra/types/date_time/dates.rb', line 150

def generate_invalid(rng, context, args)
  [nil, "not a timestamp", 123, [], {}].sample(random: rng.instance_variable_get(:@random))
end

#generate_random(rng, context, args) ⇒ Time

Generate a random timestamp

Generates a Time value in the past (up to 365 days ago).

Parameters:

Returns:

  • (Time)

    time value in the past



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/synthra/types/date_time/dates.rb', line 126

def generate_random(rng, context, args)
  # "timestamp" anchors exactly to the TimeTravel point-in-time (or a random
  # point within it, if a range context is active) rather than a random past
  # offset — the caller asked for data as it existed AT that moment.
  return TimeTravel.current_time.current_time if TimeTravel.current_time

  adapter = faker_adapter(context)
  if adapter
    adapter.time_backward(days: 365)

  else
    Faker::Time.backward(days: 365)
  end

rescue StandardError
  Time.now - rng.int(0, 365 * 86400) # fallback
end