knjrbfw 0.0.29 → 0.0.30

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.29
1
+ 0.0.30
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{knjrbfw}
8
- s.version = "0.0.29"
8
+ s.version = "0.0.30"
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"]
@@ -258,6 +258,7 @@ Gem::Specification.new do |s|
258
258
  "spec/process_spec.rb",
259
259
  "spec/spec_helper.rb",
260
260
  "spec/strings_spec.rb",
261
+ "spec/threadsafe_spec.rb",
261
262
  "spec/web_spec.rb",
262
263
  "testfiles/image.jpg"
263
264
  ]
@@ -1,5 +1,11 @@
1
1
  require "time"
2
2
 
3
+ #This class handels various time- and date-specific behaviour in a friendly way.
4
+ #===Examples
5
+ # datet = Knj::Datet.new #=> 2012-05-03 20:35:16 +0200
6
+ # datet = Knj::Datet.new(Time.now) #=> 2012-05-03 20:35:16 +0200
7
+ # datet.months + 5 #=> 2012-10-03 20:35:16 +0200
8
+ # datet.days + 64 #=> 2012-12-06 20:35:16 +010
3
9
  class Knj::Datet
4
10
  attr_accessor :time
5
11
 
@@ -7,6 +13,16 @@ class Knj::Datet
7
13
  @time = time
8
14
  end
9
15
 
16
+ #Goes forward day-by-day and stops at a date matching the criteria given.
17
+ #
18
+ #===Examples
19
+ # datet.time #=> 2012-05-03 19:36:08 +0200
20
+ #
21
+ #Try to find next saturday.
22
+ # datet.find(:day, :day_in_week => 5) #=> 2012-05-05 19:36:08 +0200
23
+ #
24
+ #Try to find next wednesday by Time's wday-method.
25
+ # datet.find(:day, :wday => 3) #=> 2012-05-09 19:36:08 +0200
10
26
  def find(incr, args)
11
27
  count = 0
12
28
  while true
@@ -29,6 +45,11 @@ class Knj::Datet
29
45
  end
30
46
  end
31
47
 
48
+ #Add a given amount of minutes to the object.
49
+ #===Examples
50
+ # datet = Knj::Datet.new #=> 2012-05-03 17:39:45 +0200
51
+ # datet.add_mins(30)
52
+ # datet.time #=> 2012-05-03 18:08:45 +0200
32
53
  def add_mins(mins = 1)
33
54
  mins = mins.to_i
34
55
  cur_mins = @time.min
@@ -49,6 +70,10 @@ class Knj::Datet
49
70
  return self
50
71
  end
51
72
 
73
+ #Adds a given amount of hours to the object.
74
+ #===Examples
75
+ # datet = Knj::Datet.new
76
+ # datet.add_hours(2)
52
77
  def add_hours(hours = 1)
53
78
  hours = hours.to_i
54
79
  cur_hour = @time.hour
@@ -69,6 +94,11 @@ class Knj::Datet
69
94
  return self
70
95
  end
71
96
 
97
+ #Adds a given amount of days to the object.
98
+ #===Examples
99
+ # datet = Knj::Datet.new #=> 2012-05-03 17:42:27 +0200
100
+ # datet.add_days(29)
101
+ # datet.time #=> 2012-06-01 17:42:27 +0200
72
102
  def add_days(days = 1)
73
103
  days = days.to_i
74
104
  return self if days == 0
@@ -93,6 +123,11 @@ class Knj::Datet
93
123
  return self
94
124
  end
95
125
 
126
+ #Adds a given amount of months to the object.
127
+ #===Examples
128
+ # datet.time #=> 2012-06-01 17:42:27 +0200
129
+ # datet.add_months(2)
130
+ # datet.time #=> 2012-08-01 17:42:27 +0200
96
131
  def add_months(months = 1)
97
132
  months = months.to_i
98
133
  cur_month = @time.month
@@ -120,6 +155,11 @@ class Knj::Datet
120
155
  return self
121
156
  end
122
157
 
158
+ #Adds a given amount of years to the object.
159
+ #===Examples
160
+ # datet.time #=> 2012-08-01 17:42:27 +0200
161
+ # datet.add_years(3)
162
+ # datet.time #> 2014-08-01 17:42:27 +0200
123
163
  def add_years(years = 1)
124
164
  next_year = @time.year + years.to_i
125
165
  @time = self.stamp(:datet => false, :year => next_year)
@@ -127,6 +167,12 @@ class Knj::Datet
127
167
  end
128
168
 
129
169
  #Is a year a leap year in the Gregorian calendar? Copied from Date-class.
170
+ #===Examples
171
+ # if Knj::Datet.gregorian_leap?(2005)
172
+ # print "2005 is a gregorian-leap year."
173
+ # else
174
+ # print "2005 is not a gregorian-leap year."
175
+ # end
130
176
  def self.gregorian_leap?(y)
131
177
  if Date.respond_to?("gregorian_leap?")
132
178
  return Date.gregorian_leap?(y)
@@ -139,7 +185,10 @@ class Knj::Datet
139
185
  end
140
186
  end
141
187
 
142
- #Returns the number of days in the current month.
188
+ #Returns the number of days in the month.
189
+ #===Examples
190
+ # datet = Knj::Datet.new
191
+ # print "There are #{datet.days_in_month} days in the current month."
143
192
  def days_in_month
144
193
  return 29 if month == 2 and Knj::Datet.gregorian_leap?(self.year)
145
194
 
@@ -148,6 +197,7 @@ class Knj::Datet
148
197
  return days_in_months[@time.month]
149
198
  end
150
199
 
200
+ #Returns the day in the week. Monday being 1 and sunday being 6.
151
201
  def day_in_week
152
202
  diw = @time.strftime("%w").to_i
153
203
  if diw == 0
@@ -159,46 +209,63 @@ class Knj::Datet
159
209
  return diw
160
210
  end
161
211
 
212
+ #Returns the days name as a string.
162
213
  def day_name
163
214
  return @time.strftime("%A")
164
215
  end
165
216
 
217
+ #Returns the months name as a string.
166
218
  def month_name
167
219
  return @time.strftime("%B")
168
220
  end
169
221
 
222
+ #Returns the year as an integer.
170
223
  def year
171
224
  return @time.year
172
225
  end
173
226
 
227
+ #Returns the hour as an integer.
174
228
  def hour
175
229
  return @time.hour
176
230
  end
177
231
 
232
+ #Returns the minute as an integer.
178
233
  def min
179
234
  return @time.min
180
235
  end
181
236
 
237
+ #Changes the year to the given year.
238
+ # datet = Knj::Datet.now #=> 2014-05-03 17:46:11 +0200
239
+ # datet.year = 2005
240
+ # datet.time #=> 2005-05-03 17:46:11 +0200
182
241
  def year=(newyear)
183
242
  @time = self.stamp(:datet => false, :year => newyear)
184
243
  end
185
244
 
245
+ #Returns the month as an integer.
186
246
  def month
187
247
  @mode = :months
188
248
  return @time.month
189
249
  end
190
250
 
251
+ #Returns the day in month as an integer.
191
252
  def date
192
253
  @mode = :days
193
254
  return @time.day
194
255
  end
195
256
 
257
+ #Returns the weekday of the week as an integer. Monday being the first and sunday being the last.
196
258
  def wday_mon
197
259
  wday = @time.wday
198
260
  return 0 if wday == 6
199
261
  return wday - 1
200
262
  end
201
263
 
264
+ #Changes the date to a given date.
265
+ #===Examples
266
+ # datet.time #=> 2005-05-03 17:46:11 +0200
267
+ # datet.date = 8
268
+ # datet.time #=> 2005-05-08 17:46:11 +0200
202
269
  def date=(newday)
203
270
  newday = newday.to_i
204
271
 
@@ -211,6 +278,11 @@ class Knj::Datet
211
278
  return self
212
279
  end
213
280
 
281
+ #Changes the hour to a given new hour.
282
+ #===Examples
283
+ # datet.time #=> 2012-05-09 19:36:08 +0200
284
+ # datet.hour = 5
285
+ # datet.time #=> 2012-05-09 05:36:08 +0200
214
286
  def hour=(newhour)
215
287
  newhour = newhour.to_i
216
288
  day = @time.day
@@ -233,21 +305,40 @@ class Knj::Datet
233
305
  return self
234
306
  end
235
307
 
308
+ #Changes the minute to a given new minute.
309
+ #===Examples
310
+ # datet.time #=> 2012-05-09 05:36:08 +0200
311
+ # datet.min = 35
312
+ # datet.time #=> 2012-05-09 05:35:08 +0200
236
313
  def min=(newmin)
237
314
  @time = self.stamp(:datet => false, :min => newmin.to_i)
238
315
  end
239
316
 
317
+ #Changes the second to a given new second.
318
+ #===Examples
319
+ # datet.time #=> 2012-05-09 05:35:08 +0200
320
+ # datet.sec = 20
321
+ # datet.time #=> 2012-05-09 05:35:20 +0200
240
322
  def sec=(newsec)
241
323
  @time = self.stamp(:datet => false, :sec => newsec.to_i)
242
324
  end
243
325
 
244
326
  alias :day :date
245
327
 
328
+ #Changes the month to a given new month.
329
+ #===Examples
330
+ # datet.time #=> 2012-05-09 05:35:20 +0200
331
+ # datet.month = 7
332
+ # datet.time #=> 2012-07-09 05:35:20 +0200
246
333
  def month=(newmonth)
247
334
  @time = self.stamp(:datet => false, :month => newmonth)
248
335
  end
249
336
 
250
- def arg_to_time(datet)
337
+ #Turns the given argument into a new Time-object.
338
+ #===Examples
339
+ # time = Knj::Datet.arg_to_time(datet) #=> <Time>-object
340
+ # time = Knj::Datet.arg_to_time(Time.now) #=> <Time>-object
341
+ def self.arg_to_time(datet)
251
342
  if datet.is_a?(Knj::Datet)
252
343
  return datet.time
253
344
  elsif datet.is_a?(Time)
@@ -259,7 +350,7 @@ class Knj::Datet
259
350
 
260
351
  include Comparable
261
352
  def <=>(timeobj)
262
- secs = arg_to_time(timeobj).to_i
353
+ secs = Knj::Datet.arg_to_time(timeobj).to_i
263
354
 
264
355
  if secs > @time.to_i
265
356
  return -1
@@ -270,6 +361,11 @@ class Knj::Datet
270
361
  end
271
362
  end
272
363
 
364
+ #This method is used for adding values to the object based on the current set mode.
365
+ #===Examples
366
+ #Add two months to the datet.
367
+ # datet.months
368
+ # datet.add_something(2)
273
369
  def add_something(val)
274
370
  val = -val if @addmode == "-"
275
371
  return self.add_years(val) if @mode == :years
@@ -280,41 +376,77 @@ class Knj::Datet
280
376
  raise "No such mode: #{@mode}"
281
377
  end
282
378
 
379
+ #Minus something.
380
+ #===Examples
381
+ # datet.months - 5
382
+ # datet.years - 2
283
383
  def -(val)
284
384
  @addmode = "-"
285
385
  self.add_something(val)
286
386
  end
287
387
 
388
+ #Add something.
389
+ #===Examples
390
+ # datet.months + 5
391
+ # datet.months + 2
288
392
  def +(val)
289
393
  @addmode = "+"
290
394
  self.add_something(val)
291
395
  end
292
396
 
397
+ #Sets the mode to hours and gets ready to plus or minus.
398
+ #===Examples
399
+ # datet.time #=> 2005-05-08 17:46:11 +0200
400
+ # datet.hours + 5
401
+ # datet.time #=> 2005-05-08 22:46:11 +0200
293
402
  def hours
294
403
  @mode = :hours
295
404
  return self
296
405
  end
297
406
 
407
+ #Sets the mode to minutes and gets ready to plus or minus.
408
+ #===Examples
409
+ # datet.time #=> 2005-05-08 22:46:11 +0200
410
+ # datet.mins + 5
411
+ # datet.mins #=> 2005-05-08 22:51:11 +0200
298
412
  def mins
299
413
  @mode = :mins
300
414
  return self
301
415
  end
302
416
 
417
+ #Sets the mode to days and gets ready to plus or minus.
418
+ #===Examples
419
+ # datet.time #=> 2005-05-08 22:51:11 +0200
420
+ # datet.days + 26
421
+ # datet.time #=> 2005-06-03 22:51:11 +0200
303
422
  def days
304
423
  @mode = :days
305
424
  return self
306
425
  end
307
426
 
427
+ #Sets the mode to months and gets ready to plus or minus.
428
+ #===Examples
429
+ # datet.time #=> 2005-06-03 22:51:11 +0200
430
+ # datet.months + 14
431
+ # datet.time #=> 2006-08-01 22:51:11 +0200
308
432
  def months
309
433
  @mode = :months
310
434
  return self
311
435
  end
312
436
 
437
+ #Sets the mode to years and gets ready to plus or minus.
438
+ #===Examples
439
+ # datet.time #=> 2006-08-01 22:51:11 +0200
440
+ # datet.years + 5
441
+ # datet.time #=> 2011-08-01 22:51:11 +0200
313
442
  def years
314
443
  @mode = :years
315
444
  return self
316
445
  end
317
446
 
447
+ #Returns a new Knj::Datet- or Time-object based on the arguments.
448
+ #===Examples
449
+ # time = datet.stamp(:datet => false, :min => 15, :day => 5) #=> 2012-07-05 05:15:20 +0200
318
450
  def stamp(args)
319
451
  vars = {:year => @time.year, :month => @time.month, :day => @time.day, :hour => @time.hour, :min => @time.min, :sec => @time.sec}
320
452
 
@@ -331,6 +463,11 @@ class Knj::Datet
331
463
  return time
332
464
  end
333
465
 
466
+ #Returns the time as a database-valid string.
467
+ #===Examples
468
+ # datet.time #=> 2011-08-01 22:51:11 +0200
469
+ # datet.dbstr #=> "2011-08-01 22:51:11"
470
+ # datet.dbstr(:time => false) #=> "2011-08-01"
334
471
  def dbstr(args = {})
335
472
  str = "%04d" % @time.year.to_s + "-" + "%02d" % @time.month.to_s + "-" + "%02d" % @time.day.to_s
336
473
 
@@ -341,6 +478,10 @@ class Knj::Datet
341
478
  return str
342
479
  end
343
480
 
481
+ #Parses the date from a database-format.
482
+ #===Examples
483
+ # datet = Knj::Datet.from_dbstr("2011-08-01 22:51:11")
484
+ # datet.time #=> 2011-08-01 22:51:11 +0200
344
485
  def self.from_dbstr(date_string)
345
486
  if date_string.is_a?(Time)
346
487
  return Knj::Datet.new(date_string)
@@ -354,21 +495,33 @@ class Knj::Datet
354
495
  return Knj::Datet.new(Time.local(*ParseDate.parsedate(date_string.to_s)))
355
496
  end
356
497
 
498
+ #Alias for 'from_dbstr'.
357
499
  def self.parse(str)
358
500
  return Knj::Datet.from_dbstr(str)
359
501
  end
360
502
 
503
+ #Returns true of the given stamp is a 'nullstamp'.
504
+ #===Examples
505
+ # Knj::Datet.is_nullstamp?("0000-00-00") #=> true
506
+ # Knj::Datet.is_nullstamp?("0000-00-00 00:00:00") #=> true
507
+ # Knj::Datet.is_nullstamp?("") #=> true
508
+ # Knj::Datet.is_nullstamp?("1985-06-17") #=> false
361
509
  def self.is_nullstamp?(stamp)
362
510
  return true if !stamp or stamp == "0000-00-00" or stamp == "0000-00-00 00:00:00" or stamp.to_s.strip == ""
363
511
  return false
364
512
  end
365
513
 
366
- #Returns the day of the year (0-365).
514
+ #Returns the day of the year (0-365) as an integer.
367
515
  def day_of_year
368
516
  return @time.strftime("%j").to_i
369
517
  end
370
518
 
371
- #Returns how many days there is between the two timestamps given.
519
+ #Returns how many days there is between the two timestamps given as an integer.
520
+ #===Examples
521
+ # d1 = Knj::Datet.new #=> 2012-05-03 18:04:12 +0200
522
+ # d2 = Knj::Datet.new #=> 2012-05-03 18:04:16 +0200
523
+ # d2.months + 5 #=> 2012-10-03 18:04:16 +0200
524
+ # Knj::Datet.days_between(d1, d2) #=> 153
372
525
  def self.days_between(t1, t2)
373
526
  raise "Timestamp 2 should be larger than timestamp 1." if t2 < t1
374
527
 
@@ -389,6 +542,11 @@ class Knj::Datet
389
542
  return upto + after
390
543
  end
391
544
 
545
+ #Returns a string based on the date and time.
546
+ #===Examples
547
+ # datet.out #=> "03/05 2012 - 18:04"
548
+ # datet.out(:time => false) #=> "03/05 2012"
549
+ # datet.out(:date => false) #=> "18:04"
392
550
  def out(args = {})
393
551
  str = ""
394
552
  date_shown = false
@@ -422,6 +580,11 @@ class Knj::Datet
422
580
  return str
423
581
  end
424
582
 
583
+ #Parses various objects into Knj::Datet-objects.
584
+ #===Examples
585
+ # datet = Knj::Datet.in("1985-06-17") #=> 1985-06-17 00:00:00 +0200
586
+ # datet = Knj::Datet.in("1985-06-17 10:00:00") #=> 1985-06-17 10:00:00 +0200
587
+ # datet = Knj::Datet.in("17/06 1985 10:00") #=> 1985-06-17 10:00:00 +0200
425
588
  def self.in(timestr)
426
589
  if timestr.is_a?(Time)
427
590
  return Knj::Datet.new(timestr)
@@ -480,7 +643,7 @@ class Knj::Datet
480
643
  raise Knj::Errors::InvalidData.new("Wrong format: '#{timestr}', class: '#{timestr.class.name}'")
481
644
  end
482
645
 
483
- #Returns a hash with the month-no as key and month-name as value.
646
+ #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.
484
647
  def self.months_arr(args = {})
485
648
  ret = {
486
649
  1 => _("January"),
@@ -509,6 +672,7 @@ class Knj::Datet
509
672
  return ret
510
673
  end
511
674
 
675
+ #Returns a hash with the day-number as value (starting with 1 for monday). It uses the method "_" to translate the months names.
512
676
  def self.days_arr(args = {})
513
677
  ret = {
514
678
  1 => _("Monday"),
@@ -532,6 +696,11 @@ class Knj::Datet
532
696
  return ret
533
697
  end
534
698
 
699
+ #Returns the month-number for a given string (starting with 1 for january).
700
+ #===Examples
701
+ # Knj::Datet.month_str_to_no("JaNuArY") #=> 1
702
+ # Knj::Datet.month_str_to_no("DECEMBER") #=> 12
703
+ # Knj::Datet.month_str_to_no("kasper") #=> <Error>-raised
535
704
  def self.month_str_to_no(str)
536
705
  ret = {
537
706
  "jan" => 1,
@@ -580,21 +749,36 @@ class Knj::Datet
580
749
  return @time.to_s
581
750
  end
582
751
 
752
+ #This returns a code-string that can be used to recreate the Knj::Datet-object.
753
+ #===Examples
754
+ # code = datet.code #=> "1985061710000000000"
755
+ # newdatet = Knj::Datet.in(code) #=> 1985-06-17 10:00:00 +0200
583
756
  def code
584
757
  return "#{"%04d" % @time.year}#{"%02d" % @time.month}#{"%02d" % @time.day}#{"%02d" % @time.hour}#{"%02d" % @time.min}#{"%02d" % @time.sec}#{"%05d" % @time.usec}"
585
758
  end
586
759
 
760
+ #Returns the unix timestamp for this object.
761
+ #===Examples
762
+ # datet.unixt #=> 487843200
763
+ # datet.to_i #=> 487843200
587
764
  def unixt
588
765
  return @time.to_i
589
766
  end
590
767
 
591
768
  alias :to_i :unixt
592
769
 
770
+ #Returns the HTTP-date that can be used in headers and such.
771
+ #===Examples
772
+ # datet.httpdate #=> "Mon, 17 Jun 1985 08:00:00 GMT"
593
773
  def httpdate
594
774
  require "time"
595
775
  return @time.httpdate
596
776
  end
597
777
 
778
+ #Returns various information about the offset as a hash.
779
+ #===Examples
780
+ # datet.time #=> 1985-06-17 10:00:00 +0200
781
+ # datet.offset_info #=> {:sign=>"+", :hours=>2, :mins=>0, :secs=>0}
598
782
  def offset_info
599
783
  offset_secs = @time.gmt_offset
600
784
 
@@ -618,17 +802,27 @@ class Knj::Datet
618
802
  }
619
803
  end
620
804
 
805
+ #Returns the offset as a string.
806
+ #===Examples
807
+ # datet.offset_str #=> "+0200"
621
808
  def offset_str
622
809
  offset_info_data = self.offset_info
623
810
  return "#{offset_info_data[:sign]}#{"%02d" % offset_info_data[:hours]}#{"%02d" % offset_info_data[:mins]}"
624
811
  end
625
812
 
626
813
  #Returns 'localtime' as of 1.9 - even in 1.8 which does it different.
814
+ #===Examples
815
+ # datet.localtime_str #=> "1985-06-17 10:00:00 +0200"
627
816
  def localtime_str
628
817
  return "#{"%04d" % @time.year}-#{"%02d" % @time.month}-#{"%02d" % @time.day} #{"%02d" % @time.hour}:#{"%02d" % @time.min}:#{"%02d" % @time.sec} #{self.offset_str}"
629
818
  end
630
819
 
631
820
  #Returns a human readable string based on the difference from the current time and date.
821
+ #===Examples
822
+ # datet.time #=> 1985-06-17 10:00:00 +0200
823
+ # datet.ago_str #=> "27 years ago"
824
+ # datet = Knj::Datet.new #=> 2012-05-03 20:31:58 +0200
825
+ # datet.ago_str #=> "18 seconds ago"
632
826
  def ago_str(args = {})
633
827
  args = {
634
828
  :year_ago_str => "%s year ago",
@@ -682,6 +876,10 @@ class Knj::Datet
682
876
  return args[:right_now_str]
683
877
  end
684
878
 
879
+ #Returns the object as a human understandable string.
880
+ #===Examples
881
+ # datet.time #=> 2012-05-03 20:31:58 +0200
882
+ # datet.human_str #=> "20:31"
685
883
  def human_str(args = {})
686
884
  args = {
687
885
  :time => true,