Skip to content

Commit

Permalink
Some Faker::Time fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrazo committed Jul 29, 2019
1 parent ffeded0 commit 1977088
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 22 deletions.
4 changes: 2 additions & 2 deletions doc/books/lovecraft.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ Faker::Books::Lovecraft.sentence #=> "Furtive antiquarian squamous dank cat loat
Faker::Books::Lovecraft.sentence(word_count: 3) #=> "Daemoniac antediluvian fainted squamous comprehension gambrel nameless singular."
Faker::Books::Lovecraft.sentence(word_count: 3, random_words_to_add: 1) #=> "Amorphous indescribable tenebrous."

# Optional arguments: num=3, spaces_allowed = false
# Optional arguments: number=3, spaces_allowed = false
Faker::Books::Lovecraft.words #=> ["manuscript", "abnormal", "singular"]
Faker::Books::Lovecraft.words(number: 2) #=> ["daemoniac", "cat"]
Faker::Books::Lovecraft.words(number: 2, spaces_allowed: true) #=> ["lurk", "charnel"]

# Optional arguments: sentence_count=3
# Optional arguments: number=3
Faker::Books::Lovecraft.sentences #=> ["Nameless loathsome decadent gambrel.", "Ululate swarthy immemorial cat madness gibbous unmentionable unnamable.", "Decadent antediluvian non-euclidean tentacles amorphous tenebrous."]
Faker::Books::Lovecraft.sentences(number: 2) #=> ["Antediluvian amorphous unmentionable singular accursed squamous immemorial.", "Gambrel daemoniac gibbous stygian shunned ululate iridescence abnormal."]

Expand Down
1 change: 0 additions & 1 deletion doc/default/address.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ Faker::Address.latitude #=> "-58.17256227443719"
Faker::Address.longitude #=> "-156.65548382095133"

Faker::Address.full_address #=> "282 Kevin Brook, Imogeneborough, CA 58517"

```
33 changes: 16 additions & 17 deletions doc/default/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@
```ruby
# Random Time between two times
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now) #=> "2014-09-18 12:30:59 -0700"
# Random Stringified time between two times, formatted to the specified I18n format
# (Examples are from a Rails console with rails-i18n 5.1.1 defaults loaded)
I18n.locale = 'en-US'

# Random Stringified time between two times, formatted to the specified I18n format
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :default) #=> "Tue, 16 Oct 2018 10:48:27 AM -05:00"
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :short) #=> "15 Oct 10:48 AM"
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :long) #=> "October 15, 2018 10:48 AM"
I18n.locale = 'ja'
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :default) #=> "2018/10/15 10:48:27"
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :short) #=> "18/10/15 10:48"
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :long) #=> "2018年10月16日(火) 10時48分27秒 -0500"
# Random Time between two dates, within specified part of the day

# Random Time between two dates, within specified part of the day
# You can install the as-duration gem to facilitate time manipulation like 45.minutes + 2.hours
# (not needed if you already have activesupport, which is included with Rails)
# (not needed if you already have ActiveSupport, which is included with Rails)
require 'as-duration'
Faker::Time.between_dates(from: 2.days.ago, to: Date.today, period: :all) #=> "2014-09-19 07:03:30 -0700"
Faker::Time.between_dates(from: 2.days.ago, to: Date.today, period: :day) #=> "2014-09-18 16:28:13 -0700"
Faker::Time.between_dates(from: 2.days.ago, to: Date.today, period: :night) #=> "2014-09-20 19:39:38 -0700"
Faker::Time.between_dates(from: 2.days.ago, to: Date.today, period: :morning) #=> "2014-09-19 08:07:52 -0700"
Faker::Time.between_dates(from: 2.days.ago, to: Date.today, period: :afternoon) #=> "2014-09-18 12:10:34 -0700"
Faker::Time.between_dates(from: 2.days.ago, to: Date.today, period: :evening) #=> "2014-09-19 20:21:03 -0700"
Faker::Time.between_dates(from: 2.days.ago, to: Date.today, period: :midnight) #=> "2014-09-20 00:40:14 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :all) #=> "2014-09-19 07:03:30 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :day) #=> "2014-09-18 16:28:13 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :night) #=> "2014-09-20 19:39:38 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :morning) #=> "2014-09-19 08:07:52 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :afternoon) #=> "2014-09-18 12:10:34 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :evening) #=> "2014-09-19 20:21:03 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :midnight) #=> "2014-09-20 00:40:14 -0700"

# Random Time in the future (up to maximum of N days)
Faker::Time.forward(days: 23, period: :morning) # => "2014-09-26 06:54:47 -0700"

# Random Time in the past (up to maximum of N days)
Faker::Time.backward(days: 14, period: :evening) #=> "2014-09-17 19:56:33 -0700"

# Random Stringified time in the past, future, or between dates, formatted to the specified I18n format
Faker::Time.backward(days: 5, period: :morning, format: :short) #=> "14 Oct 07:44"
Faker::Time.forward(days: 5, period: :evening, format: :long) #=> "October 21, 2018 20:47"
Faker::Time.between_dates(from: 5.days.ago, to: 5.days.from_now, period: :afternoon, format: :default) #=> "Fri, 19 Oct 2018 15:17:46 -0500"
Faker::Time.between_dates(from: Date.today - 5, to: Date.today + 5, period: :afternoon, format: :default) #=> "Fri, 19 Oct 2018 15:17:46 -0500"
```
2 changes: 1 addition & 1 deletion lib/faker/default/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def date_with_random_time(date, period)
end

def time_with_format(time, format)
format.nil? ? time : I18n.l(time, format: format)
format.nil? ? time : I18n.localize(time, format: format)
end

def hours(period)
Expand Down
52 changes: 51 additions & 1 deletion lib/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,57 @@ en:
time:
formats:
us: "%m/%d/%Y %I:%M %p"
default: '%a, %d %b %Y %H:%M:%S %z'
long: '%B %d, %Y %H:%M'
short: '%d %b %H:%M'
am: "AM"
pm: "PM"
faker:
separator: ' & '
separator: ' & '
date:
abbr_day_names:
- Sun
- Mon
- Tue
- Wed
- Thu
- Fri
- Sat
abbr_month_names:
- Jan
- Feb
- Mar
- Apr
- May
- Jun
- Jul
- Aug
- Sep
- Oct
- Nov
- Dec
day_names:
- Sunday
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
formats:
default: "%m-%d-%Y"

This comment has been minimized.

Copy link
@tindron

tindron Aug 9, 2019

Was this change necessary? After updating to faker 2.1.0 I'm getting failures because I'm expecting the date format from activesupport's en.yml.

This comment has been minimized.

Copy link
@vbrazo

vbrazo Aug 9, 2019

Author Member

@tindron We should probably rollback this and do a minor release. Here is the PR #1683. Please review it.

long: "%B %d, %Y"
short: "%b %d"
month_names:
- January
- February
- March
- April
- May
- June
- July
- August
- September
- October
- November
- December

0 comments on commit 1977088

Please sign in to comment.