datet 0.0.18 → 0.0.19
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/datet.gemspec +2 -2
- data/lib/datet.rb +49 -0
- data/spec/datet_spec.rb +13 -0
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.19
|
data/datet.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{datet}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.19"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kasper Johansen"]
|
12
|
-
s.date = %q{2012-08-
|
12
|
+
s.date = %q{2012-08-29}
|
13
13
|
s.description = %q{A framework for handeling date- and time-related stuff in Ruby.}
|
14
14
|
s.email = %q{k@spernj.org}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/datet.rb
CHANGED
@@ -1548,4 +1548,53 @@ class Datet
|
|
1548
1548
|
|
1549
1549
|
return self
|
1550
1550
|
end
|
1551
|
+
|
1552
|
+
#Returns a random Datet-object within given parameters.
|
1553
|
+
#===Examples
|
1554
|
+
# datet_rand = Datet.random(:years => 2003..2005) #=> Datet-object
|
1555
|
+
def self.random(args = {})
|
1556
|
+
#Test given arguments.
|
1557
|
+
raise "Given argument wasnt a hash: '#{args.class.name}'." if !args.is_a?(Hash)
|
1558
|
+
args.each do |key, val|
|
1559
|
+
raise "Invalid key: '#{key}'." if ![:years, :months, :days, :hours, :mins, :secs].include?(key)
|
1560
|
+
end
|
1561
|
+
|
1562
|
+
#Calculate random year.
|
1563
|
+
years = args[:years] || (1971..Time.now.year)
|
1564
|
+
year = years.to_a.sample
|
1565
|
+
|
1566
|
+
#Calculate random month.
|
1567
|
+
months = args[:months] || (1..12)
|
1568
|
+
month = months.to_a.sample
|
1569
|
+
month = 1 if month < 1
|
1570
|
+
month = 12 if month > 12
|
1571
|
+
|
1572
|
+
#Calculate random day.
|
1573
|
+
days_in_month = Datet.days_in_month(year, month)
|
1574
|
+
days = args[:days] || (1..days_in_month)
|
1575
|
+
day = days.to_a.sample
|
1576
|
+
day = days_in_month if day > days_in_month
|
1577
|
+
day = 1 if day < 1
|
1578
|
+
|
1579
|
+
#Calculate random hour.
|
1580
|
+
hours = args[:hours] || (0..23)
|
1581
|
+
hour = hours.to_a.sample
|
1582
|
+
hour = 0 if hour < 0
|
1583
|
+
hour = 23 if hour > 23
|
1584
|
+
|
1585
|
+
#Calculate random minute.
|
1586
|
+
mins = args[:mins] || (0..59)
|
1587
|
+
min = mins.to_a.sample
|
1588
|
+
min = 0 if min < 0
|
1589
|
+
min = 59 if min > 59
|
1590
|
+
|
1591
|
+
#Calculate random second.
|
1592
|
+
secs = args[:secs] || (0..59)
|
1593
|
+
sec = secs.to_a.sample
|
1594
|
+
sec = 0 if sec < 0
|
1595
|
+
sec = 59 if sec > 59
|
1596
|
+
|
1597
|
+
#Spawn object with the given random values.
|
1598
|
+
return Datet.new(year, month, day, hour, min, sec)
|
1599
|
+
end
|
1551
1600
|
end
|
data/spec/datet_spec.rb
CHANGED
@@ -523,4 +523,17 @@ describe "Datet" do
|
|
523
523
|
res = datet.dbstr
|
524
524
|
raise "Expected '2012-06-17 10:30:00' but got: '#{res}'." if res != "2012-06-17 10:30:00"
|
525
525
|
end
|
526
|
+
|
527
|
+
it "should be able to generate random dates" do
|
528
|
+
#Shouldnt be possible to generate random datet's with invalid dates.
|
529
|
+
datet = Datet.random(:days => 32..35)
|
530
|
+
raise "Invalid day: '#{datet.day}'." if datet.day > 31
|
531
|
+
|
532
|
+
#It should be within the given values.
|
533
|
+
years_r = 2001..2010
|
534
|
+
1.upto(100) do
|
535
|
+
datet = Datet.random(:years => years_r)
|
536
|
+
raise "Expected year to be within range: '#{datet.year}' '#{years_r}'." if !years_r.member?(datet.year)
|
537
|
+
end
|
538
|
+
end
|
526
539
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: datet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.19
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kasper Johansen
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-08-
|
13
|
+
date: 2012-08-29 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
hash: -
|
96
|
+
hash: -585524572600790953
|
97
97
|
segments:
|
98
98
|
- 0
|
99
99
|
version: "0"
|