date_tools 0.0.2
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/examples/example_date_creator.rb +24 -0
- data/examples/example_date_locale.rb +35 -0
- data/examples/example_date_time_compare.rb +29 -0
- data/lib/date_tools.rb +31 -0
- data/lib/date_tools/date_creator.rb +38 -0
- data/lib/date_tools/date_locale.rb +300 -0
- data/lib/date_tools/date_time_compare.rb +55 -0
- data/readme.html +68 -0
- data/readme.txt +50 -0
- data/unittest/test_date_creator.rb +46 -0
- data/unittest/test_date_locale.rb +225 -0
- data/unittest/test_date_time_compare.rb +46 -0
- metadata +80 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
require '../lib/date_tools/date_creator'
|
2
|
+
|
3
|
+
puts "The first sunday in January 2010: #{Date.new_by_mday(2010,1,0,1)}"
|
4
|
+
puts "The second sunday in January 2010: #{Date.new_by_mday(2010,1,0,2)}"
|
5
|
+
puts "The third sunday in January 2010: #{Date.new_by_mday(2010,1,0,3)}"
|
6
|
+
puts "The fourth sunday in January 2010: #{Date.new_by_mday(2010,1,0,4)}"
|
7
|
+
puts "The fifth sunday in January 2010: #{Date.new_by_mday(2010,1,0,5)}"
|
8
|
+
|
9
|
+
puts
|
10
|
+
|
11
|
+
puts "The first monday in January 2010: #{Date.new_by_mday(2010,1,1,1)}"
|
12
|
+
puts "The second monday in January 2010: #{Date.new_by_mday(2010,1,1,2)}"
|
13
|
+
puts "The third monday in January 2010: #{Date.new_by_mday(2010,1,1,3)}"
|
14
|
+
puts "The fourth monday in January 2010: #{Date.new_by_mday(2010,1,1,4)}"
|
15
|
+
#~ puts "The fifth monday in January 2010: #{Date.new_by_mday(2010,1,1,5)}"
|
16
|
+
|
17
|
+
puts
|
18
|
+
|
19
|
+
puts "The first sunday in February 2010: #{Date.new_by_mday(2010,2,0,1)}"
|
20
|
+
puts "The second sunday in February 2010: #{Date.new_by_mday(2010,2,0,2)}"
|
21
|
+
puts "The third sunday in February 2010: #{Date.new_by_mday(2010,2,0,3)}"
|
22
|
+
puts "The fourth sunday in February 2010: #{Date.new_by_mday(2010,2,0,4)}"
|
23
|
+
puts "The fifth sunday in February 2010: #{Date.new_by_mday(2010,2,0,5)}"
|
24
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require '../lib/date_tools/date_locale'
|
2
|
+
|
3
|
+
#Here you can play with locales.
|
4
|
+
#Please run the tests with and without locale.
|
5
|
+
#~ require 'locale'
|
6
|
+
#~ Locale.current = 'en'
|
7
|
+
#~ Locale.current = 'fr'
|
8
|
+
|
9
|
+
#
|
10
|
+
#Test with encoding conversion
|
11
|
+
#
|
12
|
+
Date_locale.set_target_encoding( 'iso-8859-1')
|
13
|
+
[
|
14
|
+
Date.new(2009, 1,21),
|
15
|
+
Date.new(2009, 2,21),
|
16
|
+
Date.new(2009,10,21),
|
17
|
+
].each{|d|
|
18
|
+
puts d.strftime('%Y-%m-%d: %A (%a) %B (%b)')
|
19
|
+
puts d.strftime('%Y-%m-%d: %A (%a) %B (%b)', :de)
|
20
|
+
puts d.strftime('%Y-%m-%d: %A (%a) %B (%b)', :de_at)
|
21
|
+
puts d.strftime('%Y-%m-%d: %A (%a) %B (%b)', :fr)
|
22
|
+
}
|
23
|
+
|
24
|
+
#
|
25
|
+
#Test all languages (unicode nbeeded)
|
26
|
+
Date_locale.set_target_encoding( 'utf-8')
|
27
|
+
[
|
28
|
+
Date.new(2009, 1,21),
|
29
|
+
Date.new(2009, 2,21),
|
30
|
+
Date.new(2009,10,21),
|
31
|
+
].each{|d|
|
32
|
+
Date_locale::DATE_TEXTS.keys.each{|lang|
|
33
|
+
puts d.strftime("#{lang}: %Y-%m-%d: %A (%a) %B (%b)", lang)
|
34
|
+
}
|
35
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require '../lib/date_tools/date_time_compare.rb'
|
2
|
+
|
3
|
+
|
4
|
+
array = [
|
5
|
+
Date.new(2010,1,1),
|
6
|
+
Time.local(2010,1,2,12,0),
|
7
|
+
DateTime.new(2010,1,3,12,30),
|
8
|
+
]
|
9
|
+
#Add some random dates and times:
|
10
|
+
5.times{
|
11
|
+
array << Date.new( rand(10)+1995, rand(11)+1, rand(27)+1)
|
12
|
+
array << Time.local( rand(10)+1995, rand(11)+1, rand(27)+1, rand(22)+1,rand(58)+1)
|
13
|
+
array << DateTime.new(rand(10)+1995 ,rand(11)+1, rand(27)+1, rand(22)+1,rand(58)+1)
|
14
|
+
}
|
15
|
+
|
16
|
+
#~ array.sort #-> error
|
17
|
+
#~ puts array.map{|d| d.to_date}.sort
|
18
|
+
array.sort_by{|d| d.to_date}.each{|d|
|
19
|
+
puts d.strftime("%Y-%m-%d %H:%M (#{d.class})")
|
20
|
+
}
|
21
|
+
|
22
|
+
10.times{
|
23
|
+
sec = rand(200000)
|
24
|
+
puts "%6i seconds are %s" % [ sec, Time.sec2time(sec) ]
|
25
|
+
}
|
26
|
+
|
27
|
+
|
28
|
+
#Print time differences in days, hours...
|
29
|
+
puts Time.gm(2009,8,11,12,0).timediff( Time.gm(2009,8,12,12,0) )
|
data/lib/date_tools.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#Little helper for the classes Date, DateTime and Time:
|
2
|
+
#
|
3
|
+
#= date_time_compare.rb:
|
4
|
+
#Allows to compare Date and Time objects (allows to sort by mixed Date/Time-keys).
|
5
|
+
#
|
6
|
+
#Time gets also some methods like Date:
|
7
|
+
#* Time#cwday
|
8
|
+
#* Time#cweek
|
9
|
+
#* Time#cwyear
|
10
|
+
#
|
11
|
+
#= date_creator.rb:
|
12
|
+
#New date creations:
|
13
|
+
#
|
14
|
+
#Date#new_by_mday: Allows "Third sunday in june 2010"...
|
15
|
+
#
|
16
|
+
#
|
17
|
+
#= date_locale.rb:
|
18
|
+
#Localization of Date, DateTime and Time.
|
19
|
+
#
|
20
|
+
#strftime (Date#strftime, Time#strftime and DateTime#strftime)
|
21
|
+
#gets a optional parameter to determine the language.
|
22
|
+
#
|
23
|
+
#Default is en, or if locale is used the current locale.
|
24
|
+
|
25
|
+
|
26
|
+
#
|
27
|
+
#There are no dependicies between the
|
28
|
+
#
|
29
|
+
require 'date_tools/date_creator.rb'
|
30
|
+
require 'date_tools/date_locale.rb'
|
31
|
+
require 'date_tools/date_time_compare.rb'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
#
|
2
|
+
#Add some possibilities to create new dates.
|
3
|
+
#
|
4
|
+
#Similar packages:
|
5
|
+
#* expanded_date: http://gemcutter.org/gems/expanded_date
|
6
|
+
#
|
7
|
+
require 'date'
|
8
|
+
|
9
|
+
class Date
|
10
|
+
|
11
|
+
#~ class DateError < ArgumentError; end
|
12
|
+
|
13
|
+
#Get the third monday in march 2008: new_by_mday( 2008, 3, 1, 3)
|
14
|
+
#
|
15
|
+
#Based on http://forum.ruby-portal.de/viewtopic.php?f=1&t=6157
|
16
|
+
def self.new_by_mday(year, month, weekday, nr)
|
17
|
+
|
18
|
+
raise( ArgumentError, "No number for weekday/nr") unless weekday.respond_to?(:between?) and nr.respond_to?(:between?)
|
19
|
+
raise( ArgumentError, "Number not in Range 1..5: #{nr}") unless nr.between?(1,5)
|
20
|
+
raise( ArgumentError, "Weekday not between 0 (Sunday)and 6 (Saturday): #{nr}") unless weekday.between?(0,6)
|
21
|
+
|
22
|
+
day = (weekday-Date.new(year, month, 1).wday)%7 + (nr-1)*7 + 1
|
23
|
+
|
24
|
+
if nr == 5
|
25
|
+
lastday = (Date.new(year, (month)%12+1, 1)-1).day # each december has the same no. of days
|
26
|
+
raise "There are not 5 weekdays with number #{weekday} in month #{month}" if day > lastday
|
27
|
+
end
|
28
|
+
|
29
|
+
Date.new(year, month, day)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
#~ Date.new_by_mday(2010,1,:wrong,6)
|
34
|
+
#~ Date.new_by_mday(2010,1,2,:wrong)
|
35
|
+
|
36
|
+
#~ puts Date.new_by_mday(2010,1,8,1)
|
37
|
+
|
38
|
+
#~ Date.new_by_mday(2010,1,1,5)
|
@@ -0,0 +1,300 @@
|
|
1
|
+
#
|
2
|
+
#Make a 'localization' for Date, DateTime and Time.
|
3
|
+
#
|
4
|
+
#This is not using locale, but if you use locale, it is detected and locale sensitive.
|
5
|
+
#
|
6
|
+
#The output is in iso-8859-1, other encodings can be set with Date_locale.set_target_encoding.
|
7
|
+
#
|
8
|
+
|
9
|
+
|
10
|
+
require 'iconv'
|
11
|
+
#~ require 'date'
|
12
|
+
|
13
|
+
#
|
14
|
+
#Adaption for a localized Date-class
|
15
|
+
#
|
16
|
+
#Solution based on discussion at ruby-forum.de
|
17
|
+
#-http://forum.ruby-portal.de/viewtopic.php?f=1&t=10527&start=0
|
18
|
+
#
|
19
|
+
module Date_locale
|
20
|
+
|
21
|
+
#Constant/Hash with the supported languages.
|
22
|
+
#
|
23
|
+
#Initial definitions are taken from localization_simplified.
|
24
|
+
#
|
25
|
+
#Changes:
|
26
|
+
#* added de_at
|
27
|
+
#* adapted :pt to pt_br (original :pt was French).
|
28
|
+
DATE_TEXTS = {
|
29
|
+
:ca => {
|
30
|
+
:monthnames => [nil] + %w{gener febrer març abril maig juny juliol agost setembre octubre novembre desembre},
|
31
|
+
:abbr_monthnames => [nil] + %w{gen feb mar abr mai jun jul ago set oct nov des},
|
32
|
+
:daynames => %w{diumenge dilluns dimarts dimecres dijous divendres dissabte},
|
33
|
+
:abbr_daynames => %w{dmg dll dmt dmc djs dvn dsb},
|
34
|
+
},
|
35
|
+
:cf => {
|
36
|
+
:monthnames => [nil] + %w{Janvier Février Mars Avril Mai Juin Juillet Août Septembre Octobre Novembre Décembre},
|
37
|
+
:abbr_monthnames => [nil] + %w{Jan Fev Mar Avr Mai Jun Jui Aou Sep Oct Nov Dec},
|
38
|
+
:daynames => %w{Dimanche Lundi Mardi Mercredi Jeudi Vendredi Samedi},
|
39
|
+
:abbr_daynames => %w{Dim Lun Mar Mer Jeu Ven Sam},
|
40
|
+
},
|
41
|
+
:cs => {
|
42
|
+
:monthnames => [nil] + %w{Leden Únor Březen Duben Květen Červen Červenec Srpen Září Říjen Listopad Prosinec},
|
43
|
+
:abbr_monthnames => [nil] + %w{Led Úno Bře Dub Kvě Čvn Čvc Srp Zář Říj Lis Pro},
|
44
|
+
:daynames => %w{Neděle Pondělí Úterý Středa Čtvrtek Pátek Sobota},
|
45
|
+
:abbr_daynames => %w{Ne Po Út St Čt Pá So},
|
46
|
+
},
|
47
|
+
:da => {
|
48
|
+
:monthnames => [nil] + %w{januar februar marts april maj juni juli august september oktober november december},
|
49
|
+
:abbr_monthnames => [nil] + %w{jan feb mar apr maj jun jul aug sep okt nov dec},
|
50
|
+
:daynames => %w{søndag mandag tirsdag onsdag torsdag fredag lørdag},
|
51
|
+
:abbr_daynames => %w{søn man tir ons tors fre lør},
|
52
|
+
},
|
53
|
+
:de => {
|
54
|
+
:monthnames => [nil] + %w{Januar Februar März April Mai Juni Juli August September Oktober November Dezember},
|
55
|
+
:abbr_monthnames => [nil] + %w{Jan Feb Mrz Apr Mai Jun Jul Aug Sep Okt Nov Dez},
|
56
|
+
:daynames => %w{Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag},
|
57
|
+
:abbr_daynames => %w{So Mo Di Mi Do Fr Sa},
|
58
|
+
},
|
59
|
+
:de_at => {
|
60
|
+
:monthnames => [nil] + %w(Jänner Feber März April Mai Juni Juli August September Oktober November Dezember),
|
61
|
+
:abbr_monthnames => [nil] + %w(Jan Feb Mrz Apr Mai Jun Jul Aug Sep Okt Nov Dez),
|
62
|
+
:daynames => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
|
63
|
+
:abbr_daynames => %w(So Mo Di Mi Do Fr Sa),
|
64
|
+
},
|
65
|
+
:en => {
|
66
|
+
:monthnames => [nil] + %w{January February March April May June July August September October November December},
|
67
|
+
:abbr_monthnames => [nil] + %w{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec},
|
68
|
+
:daynames => %w{Sunday Monday Tuesday Wednesday Thursday Friday Saturday},
|
69
|
+
:abbr_daynames => %w{Sun Mon Tue Wed Thu Fri Sat},
|
70
|
+
},
|
71
|
+
:es => {
|
72
|
+
:monthnames => [nil] + %w{enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre},
|
73
|
+
:abbr_monthnames => [nil] + %w{ene feb mar abr may jun jul ago sep oct nov dic},
|
74
|
+
:daynames => %w{domingo lunes martes miércoles jueves viernes sábado},
|
75
|
+
:abbr_daynames => %w{dom lun mar mié jue vie sáb},
|
76
|
+
},
|
77
|
+
:es_ar => {
|
78
|
+
:monthnames => [nil] + %w{enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre},
|
79
|
+
:abbr_monthnames => [nil] + %w{ene feb mar abr may jun jul ago sep oct nov dic},
|
80
|
+
:daynames => %w{domingo lunes martes miércoles jueves viernes sábado},
|
81
|
+
:abbr_daynames => %w{dom lun mar mié jue vie sáb},
|
82
|
+
},
|
83
|
+
:fi => {
|
84
|
+
:monthnames => [nil] + %w{tammikuu helmikuu maaliskuu huhtikuu toukokuu kesäkuu heinäkuu elokuu syyskuu lokakuu marraskuu joulukuu},
|
85
|
+
:abbr_monthnames => [nil] + %w{tammi helmi maalis huhti touko kesä heinä elo syys loka marras joulu},
|
86
|
+
:daynames => %w{sunnuntai maanantai tiistai keskiviikko torstai perjantai lauantai},
|
87
|
+
:abbr_daynames => %w{su ma ti ke to pe la},
|
88
|
+
},
|
89
|
+
:fr => {
|
90
|
+
:monthnames => [nil] + %w{Janvier Février Mars Avril Mai Juin Juillet Août Septembre Octobre Novembre Decembre},
|
91
|
+
:abbr_monthnames => [nil] + %w{Jan Fév Mar Avr Mai Jui Jul Aoû Sep Oct Nov Déc},
|
92
|
+
:daynames => %w{Dimanche Lundi Mardi Mercredi Jeudi Vendredi Samedi},
|
93
|
+
:abbr_daynames => %w{Dim Lun Mar Mer Jeu Ven Sam},
|
94
|
+
},
|
95
|
+
:it => {
|
96
|
+
:monthnames => [nil] + %w{Gennaio Febbraio Marzo Aprile Maggio Giugno Luglio Agosto Settembre Ottobre Novembre Dicembre },
|
97
|
+
:daynames => %w{ Domenica Lunedì Martedì Mercoledì Giovedì Venerdì Sabato },
|
98
|
+
:abbr_monthnames => [nil] + %w{ Gen Feb Mar Apr Mag Giu Lug Ago Set Ott Nov Dic },
|
99
|
+
:abbr_daynames => %w{ Dom Lun Mar Mer Gio Ven Sab },
|
100
|
+
},
|
101
|
+
:ko => {
|
102
|
+
:monthnames => [nil] + %w{1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월},
|
103
|
+
:abbr_monthnames => [nil] + %w{1 2 3 4 5 6 7 8 9 10 11 12},
|
104
|
+
:daynames => %w{일요일 월요일 화요일 수요일 목요일 금요일 토요일},
|
105
|
+
:abbr_daynames => %w{일 월 화 수 목 금 토},
|
106
|
+
},
|
107
|
+
:nl => {
|
108
|
+
:monthnames => [nil] + %w{Januari Februari Maart April Mei Juni Juli Augustus September Oktober November December},
|
109
|
+
:abbr_monthnames => [nil] + %w{Jan Feb Maa Apr Mei Jun Jul Aug Sep Okt Nov Dec},
|
110
|
+
:daynames => %w{Zondag Maandag Dinsdag Woensdag Donderdag Vrijdag Zaterdag},
|
111
|
+
:abbr_daynames => %w{Zo Ma Di Wo Do Vr Za},
|
112
|
+
},
|
113
|
+
:no => {
|
114
|
+
:monthnames => [nil] + %w{januar februar mars april mai juni juli august september oktober november desember},
|
115
|
+
:abbr_monthnames => [nil] + %w{jan feb mar apr mai jun jul aug sep okt nov des},
|
116
|
+
:daynames => %w{søndag mandag tirsdag onsdag torsdag fredag lørdag},
|
117
|
+
:abbr_daynames => %w{søn man tir ons tors fre lør},
|
118
|
+
},
|
119
|
+
:pt => {
|
120
|
+
:monthnames => [nil] + %w{Janeiro Fevereiro Março Abril Maio Junho Julho Agosto Setembro Outubro Novembro Dezembro},
|
121
|
+
:abbr_monthnames => [nil] + %w{Jan Fev Mar Abr Mai Jun Jul Ago Set Out Nov Dez},
|
122
|
+
:daynames => %w{domingo segunda terça quarta quinta sexta sábado},
|
123
|
+
:abbr_daynames => %w{Dom Seg Ter Qua Qui Sex Sab},
|
124
|
+
},
|
125
|
+
:pt_br => {
|
126
|
+
:monthnames => [nil] + %w{janeiro fevereiro março abril maio junho julho agosto setembro outubro novembro dezembro},
|
127
|
+
:abbr_monthnames => [nil] + %w{jan fev mar abr mai jun jul ago set out nov dez},
|
128
|
+
:daynames => %w{domingo segunda terça quarta quinta sexta sábado},
|
129
|
+
:abbr_daynames => %w{dom seg ter qua qui sex sáb},
|
130
|
+
},
|
131
|
+
:ru => {
|
132
|
+
:monthnames => [nil] + %w{Январь Февраль Март Апрель Май Июнь Июль Август Сентябрь Октябрь Ноябрь Декабрь},
|
133
|
+
:abbr_monthnames => [nil] + %w{Янв Фев Мар Апр Май Июн Июл Авг Сен Окт Ноя Дек},
|
134
|
+
:daynames => %w{Воскресенье Понедельник Вторник Среда Четверг Пятница Суббота},
|
135
|
+
:abbr_daynames => %w{Вск Пнд Втр Сре Чет Пят Суб},
|
136
|
+
},
|
137
|
+
:se => {
|
138
|
+
:monthnames => [nil] + %w{januari februari mars april maj juni juli augusti september oktober november december},
|
139
|
+
:abbr_monthnames => [nil] + %w{jan feb mar apr maj jun jul aug sep okt nov dec},
|
140
|
+
:daynames => %w{söndag måndag tisdag onsdag torsdag fredag lördag},
|
141
|
+
:abbr_daynames => %w{sön mån tis ons tors fre lör},
|
142
|
+
},
|
143
|
+
:sr => {
|
144
|
+
:monthnames => [nil] + %w{Januar Februar Mart April Maj Jun Jul Avgust Septembar Oktobar Novembar Decembar},
|
145
|
+
:abbr_monthnames => [nil] + %w{Jan Feb Mar Apr Maj Jun Jul Aug Sep Okt Nov Dec},
|
146
|
+
:daynames => %w{Nedelja Ponedeljak Utorak Sreda Četvrtak Petak Subota},
|
147
|
+
:abbr_daynames => %w{Ned Pon Uto Sre Čet Pet Sub},
|
148
|
+
},
|
149
|
+
}
|
150
|
+
#~ puts DATE_TEXTS.to_yaml
|
151
|
+
|
152
|
+
#Not really necessary.
|
153
|
+
#But I want to avoid later changes.
|
154
|
+
DATE_TEXTS.freeze
|
155
|
+
|
156
|
+
#
|
157
|
+
#Test if the seleted language is available in Date_locale.
|
158
|
+
def self.locale?( lang )
|
159
|
+
return DATE_TEXTS[lang]
|
160
|
+
end
|
161
|
+
|
162
|
+
#Set default converter
|
163
|
+
#~ @@encoding_converter = Iconv.new( 'iso-8859-1', 'utf-8' )
|
164
|
+
|
165
|
+
#
|
166
|
+
#The daynames are encoded in UTF (I hope ;-) ).
|
167
|
+
#With this method you can define a global converter.
|
168
|
+
#
|
169
|
+
#Example:
|
170
|
+
# Date_locale.set_target_encoding( 'iso-8859-1')
|
171
|
+
#
|
172
|
+
def self.set_target_encoding( enc )
|
173
|
+
@@encoding_converter = Iconv.new( enc, 'utf-8' )
|
174
|
+
end
|
175
|
+
|
176
|
+
|
177
|
+
#
|
178
|
+
#Get the key for the wanted language.
|
179
|
+
#
|
180
|
+
#Allows the usage (or not to use) locale.
|
181
|
+
def self.get_language_key( lang = nil )
|
182
|
+
|
183
|
+
#
|
184
|
+
#What's the better solution? Check for locale, or check for the method :language?
|
185
|
+
#
|
186
|
+
if defined?( Locale ) and lang.is_a?(Locale::Object)
|
187
|
+
#~ if lang.respond_to?(:language)
|
188
|
+
Date_locale.set_target_encoding( lang.charset )
|
189
|
+
return lang.language.to_sym
|
190
|
+
end
|
191
|
+
|
192
|
+
case lang
|
193
|
+
when nil #Undefined default, take actual locale or en
|
194
|
+
return defined?( Locale ) ? Locale.current.language.to_sym : :en
|
195
|
+
#This code require locale (or you get an error "uninitialized constant Date_locale::Locale")
|
196
|
+
#when Locale::Object
|
197
|
+
# return lang.language.to_sym
|
198
|
+
else
|
199
|
+
return lang.to_sym
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
#
|
204
|
+
#strftime with the day- and month names in the selected language.
|
205
|
+
#
|
206
|
+
#Lang can be a language symbol or a locale.
|
207
|
+
def strftime_locale(format = '%F', lang = nil )
|
208
|
+
|
209
|
+
lang = Date_locale.get_language_key(lang)
|
210
|
+
|
211
|
+
#Get the texts
|
212
|
+
if DATE_TEXTS[lang]
|
213
|
+
daynames = DATE_TEXTS[lang][:daynames]
|
214
|
+
abbr_daynames = DATE_TEXTS[lang][:abbr_daynames]
|
215
|
+
monthnames = DATE_TEXTS[lang][:monthnames]
|
216
|
+
abbr_monthnames = DATE_TEXTS[lang][:abbr_monthnames]
|
217
|
+
else
|
218
|
+
raise "Missing Support for locale #{lang.inspect}"
|
219
|
+
end
|
220
|
+
|
221
|
+
#Make the original replacements, after....
|
222
|
+
result = self.strftime_orig(
|
223
|
+
#...you replaced the language dependent parts.
|
224
|
+
format.gsub(/%([aAbB])/){|m|
|
225
|
+
case $1
|
226
|
+
when 'a'; abbr_daynames[self.wday]
|
227
|
+
when 'A'; daynames[self.wday]
|
228
|
+
when 'b'; abbr_monthnames[self.mon]
|
229
|
+
when 'B'; monthnames[self.mon]
|
230
|
+
else
|
231
|
+
raise "Date#strftime: InputError"
|
232
|
+
end
|
233
|
+
}
|
234
|
+
)
|
235
|
+
if defined? @@encoding_converter
|
236
|
+
@@encoding_converter.iconv(result)
|
237
|
+
else
|
238
|
+
result
|
239
|
+
end
|
240
|
+
end #strftime_locale(format = '%F', lang = :en )
|
241
|
+
|
242
|
+
end #module Date_locale
|
243
|
+
|
244
|
+
class Date
|
245
|
+
|
246
|
+
include Date_locale
|
247
|
+
alias :strftime_orig :strftime
|
248
|
+
|
249
|
+
#Redefine strftime with flexible daynames.
|
250
|
+
#
|
251
|
+
def strftime(format = '%F', lang = nil )
|
252
|
+
return strftime_locale(format, lang )
|
253
|
+
end #strftime
|
254
|
+
end #class Date
|
255
|
+
|
256
|
+
|
257
|
+
#
|
258
|
+
#Redefine strftime for DateTime
|
259
|
+
#
|
260
|
+
class DateTime
|
261
|
+
#No alias! It is done already in class Date.
|
262
|
+
#alias :strftime_orig_date :strftime
|
263
|
+
|
264
|
+
#Redefine strftime.
|
265
|
+
#strftime_orig is already defined in Date.
|
266
|
+
def strftime( format='%F', lang = nil )
|
267
|
+
return strftime_locale(format, lang )
|
268
|
+
end #strftime
|
269
|
+
end
|
270
|
+
|
271
|
+
|
272
|
+
class Time
|
273
|
+
include Date_locale
|
274
|
+
alias :strftime_orig :strftime
|
275
|
+
#Redefine strftime for locale versions.
|
276
|
+
def strftime(format='%F', lang = nil )
|
277
|
+
return strftime_locale(format, lang )
|
278
|
+
end #strftime
|
279
|
+
|
280
|
+
end
|
281
|
+
|
282
|
+
#
|
283
|
+
#Make some quick tests
|
284
|
+
#
|
285
|
+
if __FILE__ == $0
|
286
|
+
#~ require 'date_locale'
|
287
|
+
|
288
|
+
d = Date.new(2009,10,21)
|
289
|
+
puts d.strftime("de: %A {%a} {%A} {%W} %w ", :de ) #=> de: Mittwoch {Mi} {Mittwoch} {42} 3 (loc: en)
|
290
|
+
puts d.strftime("en: %A {%a} {%A} {%W} %w ", :en ) #=> en: Wednesday {Wed} {Wednesday} {42} 3 (loc: en)
|
291
|
+
|
292
|
+
puts "=======Load locale"
|
293
|
+
require 'locale'
|
294
|
+
Locale.current = 'de'
|
295
|
+
puts d.strftime("#{Locale.current}: %A {%a} {%A} {%W} %w") #=> de: Mittwoch {Mi} {Mittwoch} {42} 3
|
296
|
+
Locale.current = 'en'
|
297
|
+
puts d.strftime("#{Locale.current}: %A {%a} {%A} {%W} %w") #=> en: Wednesday {Wed} {Wednesday} {42} 3
|
298
|
+
puts d.strftime("de: %A {%a} {%A} {%W} %w (loc: #{Locale.current})", :de ) #=> de: Mittwoch {Mi} {Mittwoch} {42} 3 (loc: en)
|
299
|
+
puts d.strftime("en: %A {%a} {%A} {%W} %w (loc: #{Locale.current})", :en ) #=> en: Wednesday {Wed} {Wednesday} {42} 3 (loc: en)
|
300
|
+
end #if __FILE__ == $0
|
@@ -0,0 +1,55 @@
|
|
1
|
+
#
|
2
|
+
#Some extensions for Time and Date to compare the two classes.
|
3
|
+
#
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
#Adaption to allow comparison of time and date
|
7
|
+
class Time
|
8
|
+
#Convert Time object to a Date object.
|
9
|
+
def to_date()
|
10
|
+
return Date.new( self.year, self.month, self.day )
|
11
|
+
end
|
12
|
+
#Simulate Date#cwday
|
13
|
+
def cwday()
|
14
|
+
return self.to_date.cwday
|
15
|
+
end
|
16
|
+
#Return the week.
|
17
|
+
#At the beginning of the year, 0 is possible.
|
18
|
+
#
|
19
|
+
#See also http://de.wikipedia.org/wiki/Woche#Kalenderwoche
|
20
|
+
def cweek()
|
21
|
+
#~ return [1,self.strftime_d('%W').to_i].max
|
22
|
+
#~ return self.strftime_d('%W').to_i #Wrong, not ISO 8601
|
23
|
+
return self.to_date.cweek
|
24
|
+
end
|
25
|
+
#Simulate Date#cwyear
|
26
|
+
def cwyear()
|
27
|
+
return self.to_date.cwyear
|
28
|
+
end
|
29
|
+
|
30
|
+
#Calculate the difference (no negative times) and build the diff-string.
|
31
|
+
def timediff( time2 )
|
32
|
+
return Time.sec2time( ( self - time2 ).abs )
|
33
|
+
end
|
34
|
+
|
35
|
+
#Build a string with number of days, hours ... for the given seconds.
|
36
|
+
#Only positive seconds allowed.
|
37
|
+
def self.sec2time( sec )
|
38
|
+
raise "No negative time" if sec < 0
|
39
|
+
days, sec = sec.divmod(86400) # 60*60*24
|
40
|
+
hours, sec = sec.divmod(3600) # 60*60
|
41
|
+
min, sec = sec.divmod(60) # 60*60
|
42
|
+
return "#{days}d #{hours}h #{min}m #{sec}s"
|
43
|
+
end #sec2time
|
44
|
+
end
|
45
|
+
|
46
|
+
#Adaption to allow comparison of time and date
|
47
|
+
class Date
|
48
|
+
#Returns 'me'.
|
49
|
+
#
|
50
|
+
#Needed like Time#to_date() if you don't know, if you have a date or a time-object.
|
51
|
+
def to_date()
|
52
|
+
self
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
data/readme.html
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
<!--
|
2
|
+
|
3
|
+
Build by C:/Program Files/ruby/lib/ruby/gems/1.8/gems/docgenerator-1.2.0/./lib/docgenerator/document.rb
|
4
|
+
Dir: C:/usr/Script/date_tools
|
5
|
+
Creator: rakefile.rb
|
6
|
+
Target: readme.html
|
7
|
+
2009/12/26 18:37:11
|
8
|
+
|
9
|
+
Generation-Info-End
|
10
|
+
-->
|
11
|
+
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
|
12
|
+
<html>
|
13
|
+
<head ></head>
|
14
|
+
<body ><h1 >date_tools-Gem</h1>
|
15
|
+
<p >
|
16
|
+
Some tools to handle dates:
|
17
|
+
</p>
|
18
|
+
<ul >
|
19
|
+
<li >compare to time (sort date and time-objects) </li>
|
20
|
+
<li >locales </li>
|
21
|
+
<li >new creators </li>
|
22
|
+
</ul>
|
23
|
+
<p >
|
24
|
+
Check the examples-folders to get an impression of the gem.
|
25
|
+
</p>
|
26
|
+
<h1 >Gem-Content</h1>
|
27
|
+
<h2 >date_tools/date_time_compare.rb:</h2>
|
28
|
+
<p >
|
29
|
+
Allows to compare Date and Time objects (allows to sort by mixed
|
30
|
+
Date/Time-keys).
|
31
|
+
</p>
|
32
|
+
<p >
|
33
|
+
→ please check the examples in <a href = "./examples/example_date_time_compare.rb" >./examples/example_date_time_compare.rb</a>
|
34
|
+
</p>
|
35
|
+
<h2 >date_tools/date_creator.rb:</h2>
|
36
|
+
<p >
|
37
|
+
New date creations: Date#new_by_mday: Allows "Third sunday in june 2010"...
|
38
|
+
</p>
|
39
|
+
<p >
|
40
|
+
→ please check the examples in <a href = "./examples/example_date_creator.rb" >./examples/example_date_creator.rb</a>
|
41
|
+
</p>
|
42
|
+
<h2 >date_tools/date_locale.rb:</h2>
|
43
|
+
<p >
|
44
|
+
Localization of Date, DateTime and Time.
|
45
|
+
</p>
|
46
|
+
<p >
|
47
|
+
strftime gets a optional parameter to determine the language. Default is en, or if locale is used the current locale.
|
48
|
+
</p>
|
49
|
+
<p >
|
50
|
+
→ please check the examples in <a href = "./examples/example_date_locale.rb" >./examples/example_date_locale.rb</a>
|
51
|
+
</p>
|
52
|
+
<h1 >Version history:</h1>
|
53
|
+
<p >
|
54
|
+
0.0.1 2009-11-13
|
55
|
+
</p>
|
56
|
+
<ul >
|
57
|
+
<li >Initial version, including date_locale.rb and date_time_compare.rb </li>
|
58
|
+
</ul>
|
59
|
+
<p >
|
60
|
+
0.0.2 2009-12-18
|
61
|
+
</p>
|
62
|
+
<ul >
|
63
|
+
<li >Add date_creator </li>
|
64
|
+
<li >folder reorg with date_tools see Gem-packaging question: <a href = "http://forum.ruby-portal.de/viewtopic.php?f=6&t=10654" >http://forum.ruby-portal.de/viewtopic.php?f=6&t=10654</a> </li>
|
65
|
+
<li >added examples (2009-12-28) </li>
|
66
|
+
</ul>
|
67
|
+
</body>
|
68
|
+
</html>
|
data/readme.txt
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
date_tools-Gem
|
4
|
+
------------------------------
|
5
|
+
|
6
|
+
Some tools to handle dates:
|
7
|
+
- compare to time (sort date and time-objects)
|
8
|
+
- locales
|
9
|
+
- new creators
|
10
|
+
|
11
|
+
Check the examples-folders to get an impression of the gem.
|
12
|
+
|
13
|
+
Gem-Content
|
14
|
+
------------------------------
|
15
|
+
|
16
|
+
date_tools/date_time_compare.rb:
|
17
|
+
------------------------------
|
18
|
+
|
19
|
+
Allows to compare Date and Time objects (allows to sort by mixed
|
20
|
+
Date/Time-keys).
|
21
|
+
|
22
|
+
-> please check the examples in ./examples/example_date_time_compare.rb (./examples/example_date_time_compare.rb)
|
23
|
+
|
24
|
+
date_tools/date_creator.rb:
|
25
|
+
------------------------------
|
26
|
+
|
27
|
+
New date creations: Date#new_by_mday: Allows "Third sunday in june 2010"...
|
28
|
+
|
29
|
+
-> please check the examples in ./examples/example_date_creator.rb (./examples/example_date_creator.rb)
|
30
|
+
|
31
|
+
date_tools/date_locale.rb:
|
32
|
+
------------------------------
|
33
|
+
|
34
|
+
Localization of Date, DateTime and Time.
|
35
|
+
|
36
|
+
strftime gets a optional parameter to determine the language. Default is en, or if locale is used the current locale.
|
37
|
+
|
38
|
+
-> please check the examples in ./examples/example_date_locale.rb (./examples/example_date_locale.rb)
|
39
|
+
|
40
|
+
Version history:
|
41
|
+
------------------------------
|
42
|
+
|
43
|
+
0.0.1 2009-11-13
|
44
|
+
- Initial version, including date_locale.rb and date_time_compare.rb
|
45
|
+
|
46
|
+
0.0.2 2009-12-18
|
47
|
+
- Add date_creator
|
48
|
+
- folder reorg with date_tools see Gem-packaging question: http://forum.ruby-portal.de/viewtopic.php?f=6&t=10654 (http://forum.ruby-portal.de/viewtopic.php?f=6&t=10654)
|
49
|
+
- added examples (2009-12-28)
|
50
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require '../lib/date_tools/date_creator'
|
3
|
+
|
4
|
+
class Test_Date_creator < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_exceptions()
|
7
|
+
#No number for weekday/nr
|
8
|
+
assert_raise( ArgumentError ){ Date.new_by_mday(2010,1,:wrong,6)}
|
9
|
+
assert_raise( ArgumentError ){ Date.new_by_mday(2010,1,1,:wrong)}
|
10
|
+
|
11
|
+
#Number not in Range...
|
12
|
+
assert_raise( ArgumentError ){ Date.new_by_mday(2010,1,1,6)}
|
13
|
+
assert_raise( ArgumentError ){ Date.new_by_mday(2010,1,8,1)}
|
14
|
+
|
15
|
+
#No fifth monday in jan 2010
|
16
|
+
assert_raise( RuntimeError ){ Date.new_by_mday(2010,1,1,5)}
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_sunday()
|
21
|
+
#Januar 2010
|
22
|
+
assert_equal( Date.new(2010,1,3) , Date.new_by_mday(2010,1,0,1))
|
23
|
+
assert_equal( Date.new(2010,1,10) , Date.new_by_mday(2010,1,0,2))
|
24
|
+
assert_equal( Date.new(2010,1,17) , Date.new_by_mday(2010,1,0,3))
|
25
|
+
assert_equal( Date.new(2010,1,24) , Date.new_by_mday(2010,1,0,4))
|
26
|
+
assert_equal( Date.new(2010,1,31) , Date.new_by_mday(2010,1,0,5))
|
27
|
+
end #test_first_sunday()
|
28
|
+
|
29
|
+
|
30
|
+
def test_monday()
|
31
|
+
#Januar 2010
|
32
|
+
assert_equal( Date.new(2010,1,4) , Date.new_by_mday(2010,1,1,1))
|
33
|
+
assert_equal( Date.new(2010,1,11) , Date.new_by_mday(2010,1,1,2))
|
34
|
+
assert_equal( Date.new(2010,1,18) , Date.new_by_mday(2010,1,1,3))
|
35
|
+
assert_equal( Date.new(2010,1,25) , Date.new_by_mday(2010,1,1,4))
|
36
|
+
|
37
|
+
#No fifth monday in jan 2010
|
38
|
+
assert_raise( RuntimeError ){ Date.new_by_mday(2010,1,1,5)}
|
39
|
+
|
40
|
+
end #test_first_monday()
|
41
|
+
def test_tuesday()
|
42
|
+
assert_equal( Date.new(2010,1,5) , Date.new_by_mday(2010,1,2,1))
|
43
|
+
assert_equal( Date.new(2010,1,12) , Date.new_by_mday(2010,1,2,2))
|
44
|
+
end #test_first_monday()
|
45
|
+
|
46
|
+
end #Test_Date_creator
|
@@ -0,0 +1,225 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require '../lib/date_tools/date_locale'
|
3
|
+
|
4
|
+
#Here you can play with locales.
|
5
|
+
#Please run the tests with and without locale.
|
6
|
+
#~ require 'locale'
|
7
|
+
#~ Locale.current = 'en'
|
8
|
+
#~ Locale.current = 'fr'
|
9
|
+
|
10
|
+
class Test_Date_locale < Test::Unit::TestCase
|
11
|
+
def test_locale_encoding
|
12
|
+
return unless defined? Locale
|
13
|
+
#~ assert_equal( 'de', Locale.current.language ) #changing during tests
|
14
|
+
assert_equal( 'CP1252', Locale.current.charset )
|
15
|
+
end
|
16
|
+
def test_DATE_TEXTS
|
17
|
+
|
18
|
+
assert_kind_of(Hash, Date_locale::DATE_TEXTS)
|
19
|
+
|
20
|
+
Date_locale::DATE_TEXTS.each{|lang, langdata |
|
21
|
+
assert_kind_of(Symbol, lang)
|
22
|
+
assert_kind_of(Hash, langdata)
|
23
|
+
assert_equal(4, langdata.keys.size)
|
24
|
+
#test not possibe, different orders in Hash key.
|
25
|
+
#~ assert_equal([:abbr_daynames, :monthnames, :abbr_monthnames, :daynames], langdata.keys)
|
26
|
+
|
27
|
+
assert_equal( 7, langdata[:abbr_daynames].size, "check language #{lang}")
|
28
|
+
assert_equal( 7, langdata[:daynames].size, "check language #{lang}")
|
29
|
+
assert_equal( 13, langdata[:abbr_monthnames].size, "check language #{lang}")
|
30
|
+
assert_equal( 13, langdata[:monthnames].size, "check language #{lang}")
|
31
|
+
assert_nil( langdata[:abbr_monthnames].first, "check language #{lang}")
|
32
|
+
assert_nil( langdata[:monthnames].first, "check language #{lang}")
|
33
|
+
|
34
|
+
}
|
35
|
+
|
36
|
+
end #test_DATE_TEXTS
|
37
|
+
end #Test_Date_locale < Test::Unit::TestCase
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
class Test_Date < Test::Unit::TestCase
|
42
|
+
|
43
|
+
TESTSTRING = '%A %a %B (%b)'
|
44
|
+
def test_default_en()
|
45
|
+
|
46
|
+
Locale.current = 'en' if defined? Locale
|
47
|
+
if defined? Locale
|
48
|
+
assert_equal( 'en', Locale.current.language )
|
49
|
+
end
|
50
|
+
|
51
|
+
d = Date.new(2009,10,21)
|
52
|
+
assert_equal( 'Wednesday {Wed} {Wednesday} {42} 3', d.strftime('%A {%a} {%A} {%W} %w'))
|
53
|
+
assert_equal( 'Wednesday (Wed)', d.strftime('%A (%a)'))
|
54
|
+
assert_equal( 'October (Oct)', d.strftime('%B (%b)'))
|
55
|
+
end
|
56
|
+
|
57
|
+
#Check the defined lanuages.
|
58
|
+
def test_defined()
|
59
|
+
assert_not_nil( Date_locale.locale?(:de))
|
60
|
+
assert_not_nil( Date_locale.locale?(:de_at))
|
61
|
+
assert_not_nil( Date_locale.locale?(:en))
|
62
|
+
assert_not_nil( Date_locale.locale?(:fr))
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_undefined()
|
66
|
+
assert_nil( Date_locale.locale?(:xx))
|
67
|
+
|
68
|
+
d = Date.new(2009,10,21)
|
69
|
+
assert_raise( RuntimeError ){ d.strftime('%A {%a} {%A} {%W} %w', :xx) }
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_en()
|
73
|
+
d = Date.new(2009,10,21)
|
74
|
+
|
75
|
+
assert_equal( 'Wednesday (Wed)', d.strftime('%A (%a)', :en))
|
76
|
+
assert_equal( 'October (Oct)', d.strftime('%B (%b)', :en))
|
77
|
+
assert_equal( 'Wednesday {Wed} {Wednesday} {42} 3', d.strftime('%A {%a} {%A} {%W} %w', :en))
|
78
|
+
|
79
|
+
Locale.current = 'en' if defined? Locale
|
80
|
+
if defined? Locale and Locale.current.language == 'en'
|
81
|
+
assert_equal( 'en', Locale.current.language )
|
82
|
+
assert_equal( d.strftime('%A (%a)'), d.strftime('%A (%a)', :en))
|
83
|
+
assert_equal( d.strftime('%B (%b)'), d.strftime('%B (%b)', :en))
|
84
|
+
assert_equal( d.strftime('%A {%a} {%A} {%W} %w'), d.strftime('%A {%a} {%A} {%W} %w', :en))
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_de()
|
90
|
+
d = Date.new(2009,10,21)
|
91
|
+
|
92
|
+
assert_equal( 'Mittwoch (Mi)', d.strftime('%A (%a)', :de))
|
93
|
+
assert_equal( 'Oktober (Okt)', d.strftime('%B (%b)', :de))
|
94
|
+
|
95
|
+
Locale.current = 'de' if defined? Locale
|
96
|
+
if defined? Locale and Locale.current.language == 'de'
|
97
|
+
assert_equal( 'de', Locale.current.language )
|
98
|
+
assert_equal( d.strftime('%A (%a)'), d.strftime('%A (%a)', :de))
|
99
|
+
assert_equal( d.strftime('%B (%b)'), d.strftime('%B (%b)', :de))
|
100
|
+
assert_equal( d.strftime('%A {%a} {%A} {%W} %w'), d.strftime('%A {%a} {%A} {%W} %w', :de))
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_de_umlaut()
|
106
|
+
d = Date.new(2009,3,21)
|
107
|
+
#Set automatic.
|
108
|
+
#~ Date_locale.set_target_encoding( 'iso-8859-1')
|
109
|
+
|
110
|
+
assert_equal( 'Samstag (Sa)', d.strftime('%A (%a)', :de))
|
111
|
+
if defined? Locale
|
112
|
+
assert_equal( 'M�rz (Mrz)', d.strftime('%B (%b)', :de))
|
113
|
+
else
|
114
|
+
assert_equal( 'März (Mrz)', d.strftime('%B (%b)', :de))
|
115
|
+
end
|
116
|
+
|
117
|
+
Locale.current = 'de' if defined? Locale
|
118
|
+
if defined? Locale and Locale.current.language == 'de'
|
119
|
+
assert_equal( 'de', Locale.current.language )
|
120
|
+
assert_equal( d.strftime('%A (%a)'), d.strftime('%A (%a)', :de))
|
121
|
+
assert_equal( d.strftime('%B (%b)'), d.strftime('%B (%b)', :de))
|
122
|
+
assert_equal( d.strftime('%A {%a} {%A} {%W} %w'), d.strftime('%A {%a} {%A} {%W} %w', :de))
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_de_locale()
|
128
|
+
return unless defined? Locale
|
129
|
+
|
130
|
+
#Get a German locale
|
131
|
+
#?How to define it??
|
132
|
+
Locale.current = 'de'
|
133
|
+
loc_de = Locale.current
|
134
|
+
Locale.current = 'en' #set back english
|
135
|
+
loc_en = Locale.current
|
136
|
+
|
137
|
+
d = Date.new(2009,10,21)
|
138
|
+
assert_equal( 'Mittwoch (Mi)', d.strftime('%A (%a)', loc_de ))
|
139
|
+
assert_equal( 'Wednesday (Wed)', d.strftime('%A (%a)', loc_en ))
|
140
|
+
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
end #Test_Date
|
145
|
+
|
146
|
+
class Test_DateTime < Test::Unit::TestCase
|
147
|
+
|
148
|
+
TESTSTRING = '%A %a %B (%b)'
|
149
|
+
def test_default_en()
|
150
|
+
Locale.current = 'en' if defined? Locale
|
151
|
+
if defined? Locale
|
152
|
+
assert_equal( 'en', Locale.current.language )
|
153
|
+
end
|
154
|
+
|
155
|
+
1.upto(12){|m|
|
156
|
+
1.upto(7){|day| #first week (=all day names)
|
157
|
+
d = Date.new(2009,m,day)
|
158
|
+
dt = DateTime.new(2009,m, day)
|
159
|
+
assert_equal( dt.strftime(TESTSTRING), d.strftime(TESTSTRING))
|
160
|
+
}}
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_en()
|
164
|
+
1.upto(12){|m|
|
165
|
+
1.upto(7){|day| #first week (=all day names)
|
166
|
+
d = Date.new(2009,m,day)
|
167
|
+
dt = DateTime.new(2009,m, day)
|
168
|
+
assert_equal( dt.strftime(TESTSTRING, :en), d.strftime(TESTSTRING, :en))
|
169
|
+
}}
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_de()
|
173
|
+
1.upto(12){|m|
|
174
|
+
1.upto(7){|day| #first week (=all day names)
|
175
|
+
d = Date.new(2009,m,day)
|
176
|
+
dt = DateTime.new(2009,m, day)
|
177
|
+
assert_equal( dt.strftime(TESTSTRING, :de), d.strftime(TESTSTRING, :de))
|
178
|
+
}}
|
179
|
+
end
|
180
|
+
end #Test_DateTime
|
181
|
+
|
182
|
+
class Test_Time < Test::Unit::TestCase
|
183
|
+
|
184
|
+
def test_default()
|
185
|
+
t = Time.local(2009,10,21)
|
186
|
+
|
187
|
+
Locale.current = 'en' if defined? Locale
|
188
|
+
if defined? Locale and Locale.current.language == 'en'
|
189
|
+
assert_equal( 'en', Locale.current.language )
|
190
|
+
assert_equal( 'Wednesday {Wed} {Wednesday} {42} 3', t.strftime('%A {%a} {%A} {%W} %w'))
|
191
|
+
assert_equal( 'Wednesday (Wed)', t.strftime('%A (%a)'))
|
192
|
+
assert_equal( 'October (Oct)', t.strftime('%B (%b)'))
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_en()
|
197
|
+
t = Time.local(2009,10,21)
|
198
|
+
assert_equal( 'Wednesday (Wed)', t.strftime('%A (%a)', :en))
|
199
|
+
assert_equal( 'October (Oct)', t.strftime('%B (%b)', :en))
|
200
|
+
assert_equal( 'Wednesday {Wed} {Wednesday} {42} 3', t.strftime('%A {%a} {%A} {%W} %w', :en))
|
201
|
+
|
202
|
+
Locale.current = 'en' if defined? Locale
|
203
|
+
if defined? Locale and Locale.current.language == 'en'
|
204
|
+
assert_equal( 'en', Locale.current.language )
|
205
|
+
assert_equal( t.strftime('%A (%a)'), t.strftime('%A (%a)', :en))
|
206
|
+
assert_equal( t.strftime('%B (%b)'), t.strftime('%B (%b)', :en))
|
207
|
+
assert_equal( t.strftime('%A {%a} {%A} {%W} %w'), t.strftime('%A {%a} {%A} {%W} %w', :en))
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_de()
|
212
|
+
t = Time.local(2009,10,21)
|
213
|
+
assert_equal( 'Mittwoch (Mi)', t.strftime('%A (%a)', :de))
|
214
|
+
assert_equal( 'Oktober (Okt)', t.strftime('%B (%b)', :de))
|
215
|
+
|
216
|
+
Locale.current = 'de' if defined? Locale
|
217
|
+
if defined? Locale and Locale.current.language == 'de'
|
218
|
+
assert_equal( 'de', Locale.current.language )
|
219
|
+
assert_equal( t.strftime('%A (%a)'), t.strftime('%A (%a)', :de))
|
220
|
+
assert_equal( t.strftime('%B (%b)'), t.strftime('%B (%b)', :de))
|
221
|
+
assert_equal( t.strftime('%A {%a} {%A} {%W} %w'), t.strftime('%A {%a} {%A} {%W} %w', :de))
|
222
|
+
end
|
223
|
+
|
224
|
+
end
|
225
|
+
end #Test_Time
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require '../lib/date_tools/date_time_compare.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class Test_TimeDate < Test::Unit::TestCase
|
5
|
+
|
6
|
+
@@testdata = []
|
7
|
+
1990.upto(2037){|year| #ab 2038 �berlauf
|
8
|
+
@@testdata << [year,1,1]
|
9
|
+
@@testdata << [year,3,1]
|
10
|
+
@@testdata << [year,6,15]
|
11
|
+
@@testdata << [year,12,31]
|
12
|
+
}
|
13
|
+
def test_conversion()
|
14
|
+
@@testdata.each{|testdata|
|
15
|
+
assert_equal( Date.new(*testdata), Time.local(*testdata).to_date)
|
16
|
+
}
|
17
|
+
end
|
18
|
+
def test_cwday()
|
19
|
+
@@testdata.each{|testdata|
|
20
|
+
assert_equal( Date.new(*testdata).cwday, Time.local(*testdata).cwday)
|
21
|
+
}
|
22
|
+
end
|
23
|
+
def test_cweek()
|
24
|
+
@@testdata.each{|testdata|
|
25
|
+
assert_equal( Date.new(*testdata).cweek, Time.local(*testdata).cweek)
|
26
|
+
}
|
27
|
+
end
|
28
|
+
def test_cwyear()
|
29
|
+
@@testdata.each{|testdata|
|
30
|
+
assert_equal( Date.new(*testdata).cwyear, Time.local(*testdata).cwyear)
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end #Test_TimeDate
|
34
|
+
|
35
|
+
class Test_Timediff < Test::Unit::TestCase
|
36
|
+
def test_datediff()
|
37
|
+
assert_equal( '1d 0h 0m 0.0s', Time.gm(2009,8,11,12,0).timediff( Time.gm(2009,8,12,12,0) ))
|
38
|
+
assert_equal( '1d 0h 0m 0.0s', Time.gm(2009,8,12,12,0).timediff( Time.gm(2009,8,11,12,0) ))
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_sec2time()
|
42
|
+
assert_equal( '1d 1h 0m 0s', Time.sec2time(90000))
|
43
|
+
assert_raise( RuntimeError) { Time.sec2time(-90000) }
|
44
|
+
end
|
45
|
+
end #Test_Timediff
|
46
|
+
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_tools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Knut Lickert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-26 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: |
|
17
|
+
Little helper for the classes Date, DateTime and Time.
|
18
|
+
//
|
19
|
+
date_time_compare.rb:
|
20
|
+
Allows to compare Date and Time objects (allows to sort by mixed Date/Time-keys).
|
21
|
+
//
|
22
|
+
date_creator.rb:
|
23
|
+
New date creations:
|
24
|
+
Date#new_by_mday: Allows "Third sunday in june 2010"...
|
25
|
+
//
|
26
|
+
date_locale.rb:
|
27
|
+
Localization of Date, DateTime and Time.
|
28
|
+
strftime gets a optional parameter to determine the language.
|
29
|
+
Default is en, or if locale is used the current locale.
|
30
|
+
|
31
|
+
email: knut@lickert.net
|
32
|
+
executables: []
|
33
|
+
|
34
|
+
extensions: []
|
35
|
+
|
36
|
+
extra_rdoc_files: []
|
37
|
+
|
38
|
+
files:
|
39
|
+
- lib/date_tools.rb
|
40
|
+
- lib/date_tools/date_creator.rb
|
41
|
+
- lib/date_tools/date_locale.rb
|
42
|
+
- lib/date_tools/date_time_compare.rb
|
43
|
+
- readme.txt
|
44
|
+
- readme.html
|
45
|
+
- examples/example_date_creator.rb
|
46
|
+
- examples/example_date_locale.rb
|
47
|
+
- examples/example_date_time_compare.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://www.rubypla.net/
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --main
|
55
|
+
- lib/date_tools.rb
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.5
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Some tools to handle dates (compare to time, locales, new creators)
|
77
|
+
test_files:
|
78
|
+
- unittest/test_date_creator.rb
|
79
|
+
- unittest/test_date_locale.rb
|
80
|
+
- unittest/test_date_time_compare.rb
|