datet 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/datet.gemspec +1 -1
  3. data/lib/datet.rb +33 -39
  4. data/spec/datet_spec.rb +39 -2
  5. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{datet}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
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"]
@@ -48,13 +48,23 @@ class Datet
48
48
  @@days_in_months = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
49
49
 
50
50
  #Initializes the object. Default is the current time. A time-object can be given.
51
+ #=Examples
52
+ # datet = Datet.new #=> Datet-object with the current date and time.
53
+ #
54
+ # time = Time.new
55
+ # datet = Datet.new(time) #=> Datet-object with the date and time from the given Time-object.
56
+ #
57
+ # datet = Datet.new(1985, 06, 17) #=> Datet-object with the date 1985-06-17.
58
+ # datet = Datet.new(1985, 06, 17, 10) #=> Datet-object with the date 1985-06-17 10:00:00
59
+ #
60
+ # datet = Datet.new(1985, 06, 35) #=> Datet-object with the date 1985-07-05 00:00:00. Notice the invalid day of 35 was automatically converted to the right date.
51
61
  def initialize(*args)
52
62
  if args.length == 1 and args.first.is_a?(Time)
53
63
  self.update_from_time(args.first)
54
64
  return nil
55
65
  elsif args.empty?
56
- tnow = Time.now
57
- args = [tnow.year, tnow.month, tnow.day, tnow.hour, tnow.min, tnow.sec, tnow.usec]
66
+ self.update_from_time(Time.now)
67
+ return nil
58
68
  end
59
69
 
60
70
  days_left = 0
@@ -170,6 +180,8 @@ class Datet
170
180
  end
171
181
 
172
182
  #Returns a new 'Time'-object based on the data of the 'Datet'-object.
183
+ #=Examples
184
+ # Datet.new.time #=> 2012-07-13 16:14:27 +0200
173
185
  def time
174
186
  return Time.new(@t_year, @t_month, @t_day, @t_hour, @t_min, @t_sec)
175
187
  end
@@ -212,18 +224,12 @@ class Datet
212
224
  cur_usecs = @t_usec
213
225
  next_usec = cur_usecs + usecs
214
226
 
215
- if next_usec >= 1000000
216
- @t_usec = 0
217
- self.add_secs(1)
218
- usecs_left = (usecs - 1) - (1000000 - cur_usecs)
219
- self.add_usecs(usecs_left) if usecs_left > 0
220
- elsif next_usec < 0
221
- @t_usec = 99999
222
- self.add_secs(-1)
223
- usecs_left = usecs + cur_usecs + 1
224
- self.add_usecs(usecs_left) if usecs_left > 0
227
+ if next_usec >= 1000000 or next_usec <= -1000000
228
+ secs = (next_usec.to_f / 1000000.0).to_f.floor
229
+ @t_usec = next_usec - (secs * 1000000)
230
+ self.add_secs(secs)
225
231
  else
226
- time = self.stamp(:datet => false, :usec => next_usec)
232
+ @t_usec = next_usec
227
233
  end
228
234
 
229
235
  return self
@@ -233,18 +239,12 @@ class Datet
233
239
  def add_secs(secs = 1)
234
240
  secs = secs.to_i
235
241
  cur_secs = @t_sec
236
- next_sec = cur_secs + secs
242
+ next_sec = cur_secs + secs
237
243
 
238
- if next_sec >= 60
239
- @t_sec = 0
240
- self.add_mins(1)
241
- secs_left = (secs - 1) - (60 - cur_secs)
242
- self.add_secs(secs_left) if secs_left > 0
243
- elsif next_sec < 0
244
- @t_sec = 59
245
- self.add_mins(-1)
246
- secs_left = secs + cur_secs + 1
247
- self.add_secs(secs_left) if secs_left > 0
244
+ if next_sec >= 60 or next_sec <= -60
245
+ mins = (next_sec.to_f / 60.0).floor
246
+ @t_sec = next_sec - (mins * 60)
247
+ self.add_mins(mins)
248
248
  else
249
249
  @t_sec = next_sec
250
250
  end
@@ -295,7 +295,7 @@ class Datet
295
295
  self.add_hours(hours_left) if hours_left > 0
296
296
  elsif next_hour < 0
297
297
  @t_hour = 23
298
- .add_days(-1)
298
+ self.add_days(-1)
299
299
  hours_left = hours + cur_hour + 1
300
300
  self.add_hours(hours_left) if hours_left < 0
301
301
  else
@@ -312,7 +312,6 @@ class Datet
312
312
  # datet.time #=> 2012-06-01 17:42:27 +0200
313
313
  def add_days(days = 1)
314
314
  days = days.to_i
315
- return self if days == 0
316
315
  dim = self.days_in_month
317
316
  cur_day = @t_day
318
317
  next_day = cur_day + days
@@ -321,13 +320,11 @@ class Datet
321
320
  @t_day = 1
322
321
  self.add_months(1)
323
322
  days_left = (days - 1) - (dim - cur_day)
324
- self.add_days(days_left) if days_left > 0
325
- elsif next_day <= 0
326
- self.date = 1
323
+ self.add_days(days_left) if days_left != 0
324
+ elsif next_day < 0
327
325
  self.add_months(-1)
328
-
329
326
  @t_day = self.days_in_month
330
- days_left = days + 1
327
+ days_left = days + cur_day
331
328
  self.add_days(days_left) if days_left != 0
332
329
  else
333
330
  @t_day = next_day
@@ -347,12 +344,10 @@ class Datet
347
344
  cur_day = @t_day
348
345
  next_month = cur_month + months.to_i
349
346
 
350
- if next_month > 12
351
- @t_month = 1
352
- @t_day = 1
353
- self.add_years(1)
354
- months_left = (months - 1) - (12 - cur_month)
355
- self.add_months(months_left) if months_left > 0
347
+ if next_month > 12 or next_month < 0
348
+ years = (next_month.to_f / 12.0).floor
349
+ @t_month = next_month - (years * 12)
350
+ self.add_years(years)
356
351
  elsif next_month < 1
357
352
  @t_month = 12
358
353
  self.add_years(-1)
@@ -378,8 +373,7 @@ class Datet
378
373
  # datet.add_years(3)
379
374
  # datet.time #> 2014-08-01 17:42:27 +0200
380
375
  def add_years(years = 1)
381
- next_year = @t_year + years.to_i
382
- @t_year = next_year
376
+ @t_year = @t_year + years.to_i
383
377
  return self
384
378
  end
385
379
 
@@ -90,6 +90,43 @@ describe "Datet" do
90
90
  end
91
91
 
92
92
  it "should be able to handle invalid timestamps" do
93
+ datet = Datet.new(2012, 7, 13, 16, 15, 04)
94
+ raise "Expected dbstr to be '2012-07-13 16:15:04' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2012-07-13 16:15:04"
95
+
96
+ #Test 'add_secs'.
97
+ datet.add_secs(120)
98
+ raise "Expected dbstr to be '2012-07-13 16:17:04' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2012-07-13 16:17:04"
99
+
100
+ #Test 'add_days'.
101
+ datet.add_days(60)
102
+ raise "Expected dbstr to be '2012-09-11 16:17:04' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2012-09-11 16:17:04"
103
+
104
+ #Test 'add_usecs'.
105
+ datet.add_usecs(10000000)
106
+ raise "Expected dbstr to be '2012-09-11 16:17:14' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2012-09-11 16:17:14"
107
+ datet.add_usecs(-10000000)
108
+ raise "Expected dbstr to be '2012-09-11 16:17:04' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2012-09-11 16:17:04"
109
+
110
+ #Test negative 'add_secs'.
111
+ datet.add_secs(-125)
112
+ raise "Expected dbstr to be '2012-09-11 16:14:59' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2012-09-11 16:14:59"
113
+
114
+ #Test negative 'add_days'.
115
+ datet.add_days(-62)
116
+ raise "Expected dbstr to be '2012-07-11 16:14:59' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2012-07-11 16:14:59"
117
+
118
+ #Test 'add_months'.
119
+ datet.add_months(25)
120
+ raise "Expected dbstr to be '2014-08-11 16:14:59' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2014-08-11 16:14:59"
121
+ datet.add_months(-25)
122
+ raise "Expected dbstr to be '2012-07-11 16:14:59' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2012-07-11 16:14:59"
123
+
124
+ #Test 'add_years'.
125
+ datet.add_years(12)
126
+ raise "Expected dbstr to be '2024-07-11 16:14:59' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2024-07-11 16:14:59"
127
+ datet.add_years(-12)
128
+ raise "Expected dbstr to be '2012-07-11 16:14:59' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "2012-07-11 16:14:59"
129
+
93
130
  datet = Datet.new(2012, 3, 40)
94
131
  raise "Expected dbstr to be '2012-04-09' but it wasnt: '#{datet.dbstr(:time => false)}'." if datet.dbstr(:time => false) != "2012-04-09"
95
132
 
@@ -103,10 +140,10 @@ describe "Datet" do
103
140
  raise "Expected dbstr to be '1985-06-18 05:08:00' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:08:00"
104
141
 
105
142
  datet = Datet.new(1985, 6, 17, 28, 68, 68)
106
- raise "Expected dbstr to be '1985-06-18 05:09:08' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:09:08"
143
+ raise "Expected dbstr to be '1985-06-18 05:09:09' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:09:09"
107
144
 
108
145
  datet = Datet.new(1985, 6, 17, 28, 68, 68, 1000008)
109
- raise "Expected dbstr to be '1985-06-18 05:09:09' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:09:09"
146
+ raise "Expected dbstr to be '1985-06-18 05:09:10' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:09:10"
110
147
  end
111
148
 
112
149
  it "should be able to convert day-strings into numbers" do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: datet
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -104,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
104
  requirements:
105
105
  - - ">="
106
106
  - !ruby/object:Gem::Version
107
- hash: 2787627911254732399
107
+ hash: -1455855206107876040
108
108
  segments:
109
109
  - 0
110
110
  version: "0"