datet 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +0 -1
- data/Gemfile.lock +2 -2
- data/VERSION +1 -1
- data/datet.gemspec +1 -4
- data/lib/datet.rb +193 -97
- data/spec/datet_spec.rb +66 -3
- metadata +2 -13
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -9,8 +9,8 @@ GEM
|
|
9
9
|
rake
|
10
10
|
rdoc
|
11
11
|
json (1.7.3)
|
12
|
+
json (1.7.3-java)
|
12
13
|
rake (0.9.2.2)
|
13
|
-
rcov (0.9.11)
|
14
14
|
rdoc (3.12)
|
15
15
|
json (~> 1.4)
|
16
16
|
rspec (2.8.0)
|
@@ -23,11 +23,11 @@ GEM
|
|
23
23
|
rspec-mocks (2.8.0)
|
24
24
|
|
25
25
|
PLATFORMS
|
26
|
+
java
|
26
27
|
ruby
|
27
28
|
|
28
29
|
DEPENDENCIES
|
29
30
|
bundler (>= 1.0.0)
|
30
31
|
jeweler (~> 1.8.4)
|
31
|
-
rcov
|
32
32
|
rdoc (~> 3.12)
|
33
33
|
rspec (~> 2.8.0)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/datet.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
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.7"
|
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"]
|
@@ -44,20 +44,17 @@ Gem::Specification.new do |s|
|
|
44
44
|
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
45
45
|
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
46
46
|
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
47
|
-
s.add_development_dependency(%q<rcov>, [">= 0"])
|
48
47
|
else
|
49
48
|
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
50
49
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
51
50
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
52
51
|
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
53
|
-
s.add_dependency(%q<rcov>, [">= 0"])
|
54
52
|
end
|
55
53
|
else
|
56
54
|
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
57
55
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
58
56
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
59
57
|
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
60
|
-
s.add_dependency(%q<rcov>, [">= 0"])
|
61
58
|
end
|
62
59
|
end
|
63
60
|
|
data/lib/datet.rb
CHANGED
@@ -32,24 +32,46 @@ class Datet
|
|
32
32
|
}
|
33
33
|
|
34
34
|
@@days_lcase = {
|
35
|
+
"sunday" => 0,
|
35
36
|
"monday" => 1,
|
36
37
|
"tuesday" => 2,
|
37
38
|
"wednesday" => 3,
|
38
39
|
"thursday" => 4,
|
39
40
|
"friday" => 5,
|
40
|
-
"saturday" => 6
|
41
|
-
"sunday" => 0
|
41
|
+
"saturday" => 6
|
42
42
|
}
|
43
43
|
@@days_lcase.clone.each do |key, val|
|
44
44
|
@@days_lcase[key[0, 3]] = val
|
45
45
|
end
|
46
46
|
|
47
|
+
@@day_names = {
|
48
|
+
0 => "Sunday",
|
49
|
+
1 => "Monday",
|
50
|
+
2 => "Tuesday",
|
51
|
+
3 => "Wednesday",
|
52
|
+
4 => "Thursday",
|
53
|
+
5 => "Friday",
|
54
|
+
6 => "Saturday"
|
55
|
+
}
|
56
|
+
|
57
|
+
@@month_names = {
|
58
|
+
1 => "January",
|
59
|
+
2 => "February",
|
60
|
+
3 => "March",
|
61
|
+
4 => "April",
|
62
|
+
5 => "May",
|
63
|
+
6 => "June",
|
64
|
+
7 => "July",
|
65
|
+
8 => "August",
|
66
|
+
9 => "September",
|
67
|
+
10 => "October",
|
68
|
+
11 => "November",
|
69
|
+
12 => "December"
|
70
|
+
}
|
71
|
+
|
47
72
|
#Thanks to ActiveSupport: http://rubydoc.info/docs/rails/2.3.8/ActiveSupport/CoreExtensions/Time/Calculations
|
48
73
|
@@days_in_months = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
49
74
|
|
50
|
-
#This date is a monday. It is used to calculate up against, when we calculate 'day_in_week'.
|
51
|
-
@@def_date = Datet.new(1970, 1, 4)
|
52
|
-
|
53
75
|
#Initializes the object. Default is the current time. A time-object can be given.
|
54
76
|
#=Examples
|
55
77
|
# datet = Datet.new #=> Datet-object with the current date and time.
|
@@ -199,21 +221,33 @@ class Datet
|
|
199
221
|
#
|
200
222
|
#Try to find next wednesday by Time's wday-method.
|
201
223
|
# datet.find(:day, :wday => 3) #=> 2012-05-09 19:36:08 +0200
|
202
|
-
def find(
|
224
|
+
def find(args)
|
225
|
+
#Test arguments.
|
226
|
+
raise ArgumentError, "Too little arguments given: #{args.length}" if args.length < 2
|
227
|
+
|
228
|
+
args.each do |key, val|
|
229
|
+
case key
|
230
|
+
when :wday_mfirst, :wday, :incr
|
231
|
+
else
|
232
|
+
raise ArgumentError, "Invalid key in arguments: '#{key}'."
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
#Find the date.
|
203
237
|
count = 0
|
204
238
|
while true
|
205
|
-
if args[:
|
239
|
+
if args[:wday_mfirst] and self.day_in_week(:mfirst => true) == args[:wday_mfirst]
|
206
240
|
return self
|
207
|
-
elsif args[:wday] and self.
|
241
|
+
elsif args[:wday] and self.day_in_week == args[:wday]
|
208
242
|
return self
|
209
243
|
end
|
210
244
|
|
211
|
-
if incr == :day
|
245
|
+
if args[:incr] == :day
|
212
246
|
self.add_days(1)
|
213
|
-
elsif incr == :month
|
247
|
+
elsif args[:incr] == :month
|
214
248
|
self.add_months(1)
|
215
249
|
else
|
216
|
-
raise "Invalid increment: #{incr}."
|
250
|
+
raise ArgumentError, "Invalid increment: #{incr}."
|
217
251
|
end
|
218
252
|
|
219
253
|
count += 1
|
@@ -414,7 +448,7 @@ class Datet
|
|
414
448
|
|
415
449
|
#Class-method for days in month.
|
416
450
|
def self.days_in_month(year, month)
|
417
|
-
raise "Invalid month: '#{month}'." if month.to_i <= 0
|
451
|
+
raise ArgumentError, "Invalid month: '#{month}'." if month.to_i <= 0
|
418
452
|
return 29 if month == 2 and Datet.gregorian_leap?(year)
|
419
453
|
return @@days_in_months[month]
|
420
454
|
end
|
@@ -430,8 +464,23 @@ class Datet
|
|
430
464
|
return 365
|
431
465
|
end
|
432
466
|
|
433
|
-
#Returns the day in the week.
|
434
|
-
|
467
|
+
#Returns the day in the week.
|
468
|
+
#===Examples
|
469
|
+
#Get a monday:
|
470
|
+
# datet = Datet.new(1970, 1, 4)
|
471
|
+
# datet.day_in_week #=> 1
|
472
|
+
# datet.day_in_week(:mfirst = true) #=> 0
|
473
|
+
def day_in_week(args = nil)
|
474
|
+
if args
|
475
|
+
args.each do |key, val|
|
476
|
+
case key
|
477
|
+
when :mfirst
|
478
|
+
else
|
479
|
+
raise ArgumentError, "Invalid key in arguments: '#{key}'."
|
480
|
+
end
|
481
|
+
end
|
482
|
+
end
|
483
|
+
|
435
484
|
#This is a monday - 0. Use this date to calculate up against.
|
436
485
|
def_date = Datet.new(1970, 1, 4)
|
437
486
|
|
@@ -447,10 +496,13 @@ class Datet
|
|
447
496
|
diw = 0 if diw == 7
|
448
497
|
end
|
449
498
|
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
499
|
+
#Monday should be the first day in the week.
|
500
|
+
if args and args[:mfirst]
|
501
|
+
if diw == 0
|
502
|
+
diw = 6
|
503
|
+
else
|
504
|
+
diw -= 1
|
505
|
+
end
|
454
506
|
end
|
455
507
|
|
456
508
|
return diw
|
@@ -458,12 +510,37 @@ class Datet
|
|
458
510
|
|
459
511
|
#Returns the days name as a string.
|
460
512
|
def day_name
|
461
|
-
return self.
|
513
|
+
return @@day_names[self.day_in_week]
|
514
|
+
end
|
515
|
+
|
516
|
+
#Returns the day as a localized string.
|
517
|
+
#===Examples
|
518
|
+
# Datet.new.day_str #=> "Monday"
|
519
|
+
# Datet.new.day_str(:short => true) #=> "Mon"
|
520
|
+
def day_str(args = nil)
|
521
|
+
ret = Datet.days(:trans => true)[self.day_in_week]
|
522
|
+
if args and args[:short]
|
523
|
+
ret = ret.slice(0, 3)
|
524
|
+
end
|
525
|
+
|
526
|
+
return ret
|
527
|
+
end
|
528
|
+
|
529
|
+
#Returns the month as a localized string.
|
530
|
+
#===Examples
|
531
|
+
# Datet.new.month_str #=> "January"
|
532
|
+
def month_str
|
533
|
+
ret = Datet.months(:trans => true)[@t_month]
|
534
|
+
if args and args[:short]
|
535
|
+
ret = ret.slice(0, 3)
|
536
|
+
end
|
537
|
+
|
538
|
+
return ret
|
462
539
|
end
|
463
540
|
|
464
541
|
#Returns the months name as a string.
|
465
542
|
def month_name
|
466
|
-
return
|
543
|
+
return @@month_names[@t_month]
|
467
544
|
end
|
468
545
|
|
469
546
|
#Returns the year as an integer.
|
@@ -511,13 +588,6 @@ class Datet
|
|
511
588
|
return @t_day
|
512
589
|
end
|
513
590
|
|
514
|
-
#Returns the weekday of the week as an integer. Monday being the first and sunday being the last.
|
515
|
-
def wday_mon
|
516
|
-
wday = self.time.wday
|
517
|
-
return 0 if wday == 6
|
518
|
-
return wday - 1
|
519
|
-
end
|
520
|
-
|
521
591
|
#Changes the date to a given date.
|
522
592
|
#===Examples
|
523
593
|
# datet.time #=> 2005-05-03 17:46:11 +0200
|
@@ -589,7 +659,7 @@ class Datet
|
|
589
659
|
# datet.time #=> 2012-07-09 05:35:20 +0200
|
590
660
|
def month=(newmonth)
|
591
661
|
newmonth = newmonth.to_i
|
592
|
-
raise "Invalid month: '#{newmonth}'." if newmonth <= 0
|
662
|
+
raise ArgumentError, "Invalid month: '#{newmonth}'." if newmonth <= 0
|
593
663
|
@t_month = newmonth
|
594
664
|
end
|
595
665
|
|
@@ -603,7 +673,7 @@ class Datet
|
|
603
673
|
elsif datet.is_a?(Time)
|
604
674
|
return datet
|
605
675
|
else
|
606
|
-
raise "Could not handle object of class: '#{datet.class.name}'."
|
676
|
+
raise ArgumentError, "Could not handle object of class: '#{datet.class.name}'."
|
607
677
|
end
|
608
678
|
end
|
609
679
|
|
@@ -617,7 +687,7 @@ class Datet
|
|
617
687
|
elsif datet.is_a?(Time)
|
618
688
|
return Datet.new(datet)
|
619
689
|
else
|
620
|
-
raise "Could not handle object of class: '#{datet.class.name}'."
|
690
|
+
raise ArgumentError, "Could not handle object of class: '#{datet.class.name}'."
|
621
691
|
end
|
622
692
|
end
|
623
693
|
|
@@ -787,20 +857,19 @@ class Datet
|
|
787
857
|
#===Examples
|
788
858
|
# Datet.new.day_of_year #=> 123
|
789
859
|
def day_of_year
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
ret = ret.slice(0, 3)
|
860
|
+
count = @t_day
|
861
|
+
@@days_in_months.each_index do |key|
|
862
|
+
break if key >= @t_month
|
863
|
+
val = @@days_in_months[key]
|
864
|
+
|
865
|
+
if key == 2 and Datet.gregorian_leap?(@t_year)
|
866
|
+
count += 29
|
867
|
+
else
|
868
|
+
count += val.to_i
|
869
|
+
end
|
801
870
|
end
|
802
871
|
|
803
|
-
return
|
872
|
+
return count
|
804
873
|
end
|
805
874
|
|
806
875
|
#Returns how many days there is between the two timestamps given as an integer.
|
@@ -810,7 +879,7 @@ class Datet
|
|
810
879
|
# d2.months + 5 #=> 2012-10-03 18:04:16 +0200
|
811
880
|
# Datet.days_between(d1, d2) #=> 153
|
812
881
|
def self.days_between(t1, t2)
|
813
|
-
raise "Timestamp 2 should be larger than timestamp 1." if t2 < t1
|
882
|
+
raise ArgumentError, "Timestamp 2 should be larger than timestamp 1." if t2 < t1
|
814
883
|
|
815
884
|
doy1 = t1.day_of_year
|
816
885
|
doy2 = t2.day_of_year
|
@@ -948,53 +1017,81 @@ class Datet
|
|
948
1017
|
end
|
949
1018
|
|
950
1019
|
#Returns a hash with the month-no as key and month-name as value. It uses the method "_" to translate the months names. So GetText or another method has to be defined.
|
951
|
-
def self.
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
9 => _("September"),
|
962
|
-
10 => _("October"),
|
963
|
-
11 => _("November"),
|
964
|
-
12 => _("December")
|
965
|
-
}
|
1020
|
+
def self.months(args = nil)
|
1021
|
+
if args
|
1022
|
+
args.each do |key, val|
|
1023
|
+
case key
|
1024
|
+
when :short, :trans
|
1025
|
+
else
|
1026
|
+
raise ArgumentError, "Invalid key in arguments: '#{key}'."
|
1027
|
+
end
|
1028
|
+
end
|
1029
|
+
end
|
966
1030
|
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
1031
|
+
ret = @@month_names.clone
|
1032
|
+
|
1033
|
+
if args
|
1034
|
+
if args[:trans]
|
1035
|
+
ret.each do |key, val|
|
1036
|
+
ret[key] = _(val)
|
1037
|
+
end
|
971
1038
|
end
|
972
1039
|
|
973
|
-
|
1040
|
+
if args[:short]
|
1041
|
+
ret_short = {}
|
1042
|
+
ret.each do |key, val|
|
1043
|
+
ret_short[key] = val[0..2]
|
1044
|
+
end
|
1045
|
+
|
1046
|
+
return ret_short
|
1047
|
+
end
|
974
1048
|
end
|
975
1049
|
|
976
1050
|
return ret
|
977
1051
|
end
|
978
1052
|
|
979
1053
|
#Returns a hash with the day-number as value (starting with 1 for monday). It uses the method "_" to translate the months names.
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
1054
|
+
#===Examples
|
1055
|
+
# Datet.days #=> {0=>"Sunday", 1=>"Monday", 2=>"Tuesday", 3=>"Wednesday", 4=>"Thursday", 5=>"Friday", 6=>"Saturday"}
|
1056
|
+
# Datet.days(:mfirst => true) #=> {0=>"Monday", 1=>"Tuesday", 2=>"Wednesday", 3=>"Thursday", 4=>"Friday", 5=>"Saturday", 6=>"Sunday"}
|
1057
|
+
def self.days(args = nil)
|
1058
|
+
if args
|
1059
|
+
args.each do |key, val|
|
1060
|
+
case key
|
1061
|
+
when :mfirst, :short, :trans
|
1062
|
+
else
|
1063
|
+
raise ArgumentError, "Unknown key in arguments: '#{key}'."
|
1064
|
+
end
|
1065
|
+
end
|
1066
|
+
end
|
1067
|
+
|
1068
|
+
ret = @@day_names.clone
|
990
1069
|
|
991
|
-
if args
|
992
|
-
|
993
|
-
|
994
|
-
|
1070
|
+
if args
|
1071
|
+
if args[:trans]
|
1072
|
+
ret.each do |key, val|
|
1073
|
+
ret[key] = _(val)
|
1074
|
+
end
|
995
1075
|
end
|
996
1076
|
|
997
|
-
|
1077
|
+
if args[:mfirst]
|
1078
|
+
newret = {}
|
1079
|
+
ret.each do |key, val|
|
1080
|
+
next if key == 0
|
1081
|
+
newret[key - 1] = val
|
1082
|
+
end
|
1083
|
+
newret[6] = ret[0]
|
1084
|
+
ret = newret
|
1085
|
+
end
|
1086
|
+
|
1087
|
+
if args[:short]
|
1088
|
+
ret_short = {}
|
1089
|
+
ret.each do |key, val|
|
1090
|
+
ret_short[key] = val[0..2]
|
1091
|
+
end
|
1092
|
+
|
1093
|
+
ret = ret_short
|
1094
|
+
end
|
998
1095
|
end
|
999
1096
|
|
1000
1097
|
return ret
|
@@ -1003,10 +1100,18 @@ class Datet
|
|
1003
1100
|
#Converts a given day-name to the right day number.
|
1004
1101
|
#===Examples
|
1005
1102
|
# Datet.day_str_to_no('wed') #=> 3
|
1006
|
-
def self.day_str_to_no(day_str)
|
1103
|
+
def self.day_str_to_no(day_str, args = nil)
|
1007
1104
|
day_str = day_str.to_s.strip[0, 3]
|
1008
1105
|
|
1009
1106
|
if no = @@days_lcase[day_str]
|
1107
|
+
if args and args[:mfirst]
|
1108
|
+
if no == 0
|
1109
|
+
no = 6
|
1110
|
+
else
|
1111
|
+
no -= 1
|
1112
|
+
end
|
1113
|
+
end
|
1114
|
+
|
1010
1115
|
return no
|
1011
1116
|
end
|
1012
1117
|
|
@@ -1021,19 +1126,7 @@ class Datet
|
|
1021
1126
|
def self.month_str_to_no(str)
|
1022
1127
|
str = str.to_s.downcase.strip
|
1023
1128
|
return @@months_lcase[str] if @@months_lcase.key?(str)
|
1024
|
-
raise "No month to return from that string: '#{str}'."
|
1025
|
-
end
|
1026
|
-
|
1027
|
-
def loc_wday
|
1028
|
-
return _(self.time.strftime("%A"))
|
1029
|
-
end
|
1030
|
-
|
1031
|
-
def loc_wday_small
|
1032
|
-
return _(self.time.strftime("%a"))
|
1033
|
-
end
|
1034
|
-
|
1035
|
-
def loc_month
|
1036
|
-
return _(self.time.strftime("%B"))
|
1129
|
+
raise ArgumentError, "No month to return from that string: '#{str}'."
|
1037
1130
|
end
|
1038
1131
|
|
1039
1132
|
#This returns a code-string that can be used to recreate the Datet-object.
|
@@ -1044,19 +1137,23 @@ class Datet
|
|
1044
1137
|
return "#{"%04d" % @t_year}#{"%02d" % @t_month}#{"%02d" % @t_day}#{"%02d" % @t_hour}#{"%02d" % @t_min}#{"%02d" % @t_sec}#{"%05d" % @t_usec}"
|
1045
1138
|
end
|
1046
1139
|
|
1047
|
-
#Returns the unix timestamp for this object.
|
1140
|
+
#Returns the unix timestamp for this object (uses 'Time' to calculate).
|
1048
1141
|
#===Examples
|
1049
1142
|
# datet.to_i #=> 487843200
|
1050
1143
|
def to_i
|
1051
1144
|
return self.time.to_i
|
1052
1145
|
end
|
1053
1146
|
|
1147
|
+
#Returns the unix timestamp for this object as a float (uses 'Time' to calculate).
|
1148
|
+
#===Examples
|
1149
|
+
# Datet.new.to_f #=> 1342381158.0
|
1054
1150
|
def to_f
|
1055
|
-
return self.
|
1151
|
+
return self.time.to_f
|
1056
1152
|
end
|
1057
1153
|
|
1154
|
+
#Returns a string that describes the object.
|
1058
1155
|
def to_s
|
1059
|
-
return
|
1156
|
+
return "#{"%04d" % @t_year}-#{"%02d" % @t_month}-#{"%02d" % @t_day} #{"%02d" % @t_hour}:#{"%02d" % @t_min}:#{"%02d" % @t_sec}"
|
1060
1157
|
end
|
1061
1158
|
|
1062
1159
|
#Returns arguments in an array.
|
@@ -1068,8 +1165,7 @@ class Datet
|
|
1068
1165
|
#===Examples
|
1069
1166
|
# datet.httpdate #=> "Mon, 17 Jun 1985 08:00:00 GMT"
|
1070
1167
|
def httpdate
|
1071
|
-
|
1072
|
-
return self.time.httpdate
|
1168
|
+
return "#{self.day_name[0, 3]}, #{@t_day} #{self.month_name[0, 3]} #{@t_year} #{"%02d" % @t_hour}:#{"%02d" % @t_min}:#{"%02d" % @t_sec} GMT"
|
1073
1169
|
end
|
1074
1170
|
|
1075
1171
|
#Returns various information about the offset as a hash.
|
data/spec/datet_spec.rb
CHANGED
@@ -176,6 +176,16 @@ describe "Datet" do
|
|
176
176
|
tests.each do |test_str, right_res|
|
177
177
|
res = Datet.day_str_to_no(test_str)
|
178
178
|
raise "Expected result: '#{right_res}' but got: '#{res}'." if res != right_res
|
179
|
+
|
180
|
+
#Check that the ':mfirst' is working as well (monday first day of week).
|
181
|
+
res2 = Datet.day_str_to_no(test_str, :mfirst => true)
|
182
|
+
if res == 0
|
183
|
+
exp = 6
|
184
|
+
else
|
185
|
+
exp = res - 1
|
186
|
+
end
|
187
|
+
|
188
|
+
raise "Expected '#{exp}' but got: '#{res2}'." if res2 != exp
|
179
189
|
end
|
180
190
|
end
|
181
191
|
|
@@ -229,8 +239,8 @@ describe "Datet" do
|
|
229
239
|
[Datet.new(2012, 7, 17), 1]
|
230
240
|
]
|
231
241
|
tests.each do |data|
|
232
|
-
day_in_week = data[0].day_in_week
|
233
|
-
raise "Expected #{data[1]} but got #{day_in_week}" if day_in_week != data[1]
|
242
|
+
day_in_week = data[0].day_in_week(:mfirst => true)
|
243
|
+
raise "Expected '#{data[1]}' but got '#{day_in_week}'." if day_in_week != data[1]
|
234
244
|
|
235
245
|
diw = data[0].time.strftime("%w").to_i
|
236
246
|
if diw == 0
|
@@ -269,9 +279,62 @@ describe "Datet" do
|
|
269
279
|
datet = Datet.new(1970, 1, 4)
|
270
280
|
6.times do |i|
|
271
281
|
day_str = datet.day_str
|
272
|
-
exp = Datet.
|
282
|
+
exp = Datet.days[i]
|
273
283
|
raise "Expected '#{exp}' but got '#{day_str}' for day-no: '#{i}'." if day_str != exp
|
274
284
|
datet.days + 1
|
275
285
|
end
|
286
|
+
|
287
|
+
#Check date 'mfirst' works (monday first day of week).
|
288
|
+
days_str_sf = Datet.days
|
289
|
+
days_str_mf = Datet.days(:mfirst => true)
|
290
|
+
|
291
|
+
days_str_mf.each do |key, val|
|
292
|
+
if key == 6
|
293
|
+
use_key = 0
|
294
|
+
else
|
295
|
+
use_key = key + 1
|
296
|
+
end
|
297
|
+
|
298
|
+
res = days_str_sf[use_key]
|
299
|
+
raise "Expected '#{val}' but got: '#{res}' for key: '#{use_key}'." if res != val
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
it "should be able to calculate the day of the year" do
|
304
|
+
tests = [
|
305
|
+
[Datet.new(2005, 1, 1), 1],
|
306
|
+
[Datet.new(2005, 12, 31), 365],
|
307
|
+
[Datet.new(2005, 3, 1), 60],
|
308
|
+
[Datet.new(2008, 3, 1), 61],
|
309
|
+
[Datet.new(2005, 2, 27), 58],
|
310
|
+
[Datet.new(2008, 2, 27), 58]
|
311
|
+
]
|
312
|
+
|
313
|
+
tests.each do |data|
|
314
|
+
res = data[0].day_of_year
|
315
|
+
raise "Expected '#{data[1]}' but got: '#{res}' for #{data[0]}." if res != data[1]
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
it "should return http-dates" do
|
320
|
+
tests = [
|
321
|
+
[Datet.new(1985, 6, 17, 8), "Mon, 17 Jun 1985 08:00:00 GMT"]
|
322
|
+
]
|
323
|
+
|
324
|
+
tests.each do |data|
|
325
|
+
res = data[0].httpdate
|
326
|
+
raise "Expected: '#{data[1]}' but got: '#{res}'." if res != data[1]
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
it "should be able to do finds" do
|
331
|
+
#This is a monday.
|
332
|
+
datet = Datet.new(1970, 1, 4)
|
333
|
+
|
334
|
+
datet.find(:incr => :day, :wday => 4)
|
335
|
+
raise "Expected 'day_name' to be 'Thursday' but it wasnt: '#{datet.day_name}'." if datet.day_name != "Thursday"
|
336
|
+
|
337
|
+
datet.find(:incr => :month, :wday => 5)
|
338
|
+
raise "Expected dbstr to be '1970-05-08' but it wasnt: '#{datet.dbstr(:time => false)}'." if datet.dbstr(:time => false) != "1970-05-08"
|
276
339
|
end
|
277
340
|
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.7
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kasper Johansen
|
@@ -57,17 +57,6 @@ dependencies:
|
|
57
57
|
type: :development
|
58
58
|
prerelease: false
|
59
59
|
version_requirements: *id004
|
60
|
-
- !ruby/object:Gem::Dependency
|
61
|
-
name: rcov
|
62
|
-
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
-
none: false
|
64
|
-
requirements:
|
65
|
-
- - ">="
|
66
|
-
- !ruby/object:Gem::Version
|
67
|
-
version: "0"
|
68
|
-
type: :development
|
69
|
-
prerelease: false
|
70
|
-
version_requirements: *id005
|
71
60
|
description: A framework for handeling date- and time-related stuff in Ruby.
|
72
61
|
email: k@spernj.org
|
73
62
|
executables: []
|
@@ -104,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
93
|
requirements:
|
105
94
|
- - ">="
|
106
95
|
- !ruby/object:Gem::Version
|
107
|
-
hash:
|
96
|
+
hash: 3008034930327995715
|
108
97
|
segments:
|
109
98
|
- 0
|
110
99
|
version: "0"
|