Module: DataMagic::DateTranslation

Included in:
DataMagic, Translation
Defined in:
lib/data_magic/date_translation.rb

Instance Method Summary collapse

Instance Method Details

#date_between(from = nil, to = nil, format = '%D') ⇒ Object Also known as: dm_date_between

return random date within the range

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
# File 'lib/data_magic/date_translation.rb', line 84

def date_between(from = nil, to = nil, format = '%D')
  raise ArgumentError, 'Invalid date format' if from.to_s.empty? || to.to_s.empty?

  start_date = from.nil? ? Date.today.strftime(format) : Date.strptime(from, format)
  end_date = to.nil? ? Date.today.strftime(format) : Date.strptime(to, format)

  Faker::Date.between(from: start_date, to: end_date).strftime(format)
end

#day_of_weekObject Also known as: dm_day_of_week

return a day of the week



65
66
67
# File 'lib/data_magic/date_translation.rb', line 65

def day_of_week
  randomize(Date::DAYNAMES)
end

#day_of_week_abbrObject Also known as: dm_day_of_week_abbr



70
71
72
# File 'lib/data_magic/date_translation.rb', line 70

def day_of_week_abbr
  randomize(Date::ABBR_DAYNAMES)
end

#monthObject Also known as: dm_month

return a month



49
50
51
# File 'lib/data_magic/date_translation.rb', line 49

def month
  randomize(Date::MONTHNAMES[1..-1])
end

#month_abbrObject Also known as: dm_month_abbr

return a month abbreviation



57
58
59
# File 'lib/data_magic/date_translation.rb', line 57

def month_abbr
  randomize(Date::ABBR_MONTHNAMES[1..-1])
end

#today(format = '%D') ⇒ Object Also known as: dm_today

return today’s date

See ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime for details of the formats

Parameters:

  • String

    the format to use for the date. Default is %D



13
14
15
# File 'lib/data_magic/date_translation.rb', line 13

def today(format = '%D')
  Date.today.strftime(format)
end

#tomorrow(format = '%D') ⇒ Object Also known as: dm_tomorrow

return tomorrow’s date

See ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime for details of the formats

Parameters:

  • String

    the format to use for the date. Default is %D



26
27
28
29
# File 'lib/data_magic/date_translation.rb', line 26

def tomorrow(format = '%D')
  tomorrow = Date.today + 1
  tomorrow.strftime(format)
end

#yesterday(format = '%D') ⇒ Object Also known as: dm_yesterday

return yesterday’s date

See ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime for details of the formats

Parameters:

  • String

    the format to use for the date. Default is %D



40
41
42
43
# File 'lib/data_magic/date_translation.rb', line 40

def yesterday(format = '%D')
  yesterday = Date.today - 1
  yesterday.strftime(format)
end