datet 0.0.7 → 0.0.8

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 +2 -2
  3. data/lib/datet.rb +165 -30
  4. data/spec/datet_spec.rb +118 -0
  5. metadata +3 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{datet}
8
- s.version = "0.0.7"
8
+ s.version = "0.0.8"
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-07-15}
12
+ s.date = %q{2012-07-16}
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 = [
@@ -92,6 +92,29 @@ class Datet
92
92
  return nil
93
93
  end
94
94
 
95
+ #Handle nil-support.
96
+ all_nil = true
97
+ args.each do |arg|
98
+ if arg != 0 and arg
99
+ all_nil = false
100
+ break
101
+ end
102
+ end
103
+
104
+ if all_nil
105
+ @t_nil = true
106
+ @t_year = 0
107
+ @t_month = 0
108
+ @t_day = 0
109
+ @t_hour = 0
110
+ @t_min = 0
111
+ @t_sec = 0
112
+ @t_usec = 0
113
+ return nil
114
+ end
115
+
116
+
117
+ #Normal date-calculation support.
95
118
  days_left = 0
96
119
  months_left = 0
97
120
  hours_left = 0
@@ -508,6 +531,17 @@ class Datet
508
531
  return diw
509
532
  end
510
533
 
534
+ #Returns the week-number of the year (1..53).
535
+ def week_no(args = nil)
536
+ week_no = (self.day_of_year.to_f / 7).to_i + 1
537
+
538
+ if args and args[:mfirst] and self.day_in_week == 1
539
+ week_no -= 1
540
+ end
541
+
542
+ return week_no
543
+ end
544
+
511
545
  #Returns the days name as a string.
512
546
  def day_name
513
547
  return @@day_names[self.day_in_week]
@@ -553,6 +587,13 @@ class Datet
553
587
  return @t_hour
554
588
  end
555
589
 
590
+ #Returns the hour as an integer in the twelve-hour-clock.
591
+ def hour_thc
592
+ hour = @t_hour
593
+ hour -= 12 if hour > 12
594
+ return hour
595
+ end
596
+
556
597
  #Returns the minute as an integer.
557
598
  def min
558
599
  return @t_min
@@ -568,6 +609,15 @@ class Datet
568
609
  return @t_usec
569
610
  end
570
611
 
612
+ #Returns "am" or "pm" based on the hours values.
613
+ def ampm
614
+ if @t_hour <= 12
615
+ return "am"
616
+ else
617
+ return "pm"
618
+ end
619
+ end
620
+
571
621
  #Changes the year to the given year.
572
622
  # datet = Datet.now #=> 2014-05-03 17:46:11 +0200
573
623
  # datet.year = 2005
@@ -583,25 +633,20 @@ class Datet
583
633
  end
584
634
 
585
635
  #Returns the day in month as an integer.
586
- def date
636
+ def day
587
637
  Thread.current[:datet_mode] = :days
588
638
  return @t_day
589
639
  end
590
640
 
591
- #Changes the date to a given date.
641
+ #Changes the day to a given day.
592
642
  #===Examples
593
643
  # datet.time #=> 2005-05-03 17:46:11 +0200
594
- # datet.date = 8
644
+ # datet.day = 8
595
645
  # datet.time #=> 2005-05-08 17:46:11 +0200
596
- def date=(newday)
646
+ def day=(newday)
597
647
  newday = newday.to_i
598
-
599
- if newday <= 0
600
- self.add_days(newday - 1)
601
- else
602
- @t_day = newday
603
- end
604
-
648
+ raise ArgumentError, "Invalid day: '#{newday}'." if newday < 0 or newday > self.days_in_month
649
+ @t_day = newday
605
650
  return self
606
651
  end
607
652
 
@@ -612,23 +657,8 @@ class Datet
612
657
  # datet.time #=> 2012-05-09 05:36:08 +0200
613
658
  def hour=(newhour)
614
659
  newhour = newhour.to_i
615
- day = @t_day
616
-
617
- loop do
618
- break if newhour >= 0
619
- day += -1
620
- newhour += 24
621
- end
622
-
623
- loop do
624
- break if newhour < 24
625
- day += 1
626
- newhour += -24
627
- end
628
-
660
+ raise ArgumentError, "Invalid hour: '#{newhour}'." if newhour < 0 or newhour > 60
629
661
  @t_hour = newhour
630
-
631
- self.date = day if day != @t_day
632
662
  return self
633
663
  end
634
664
 
@@ -638,7 +668,9 @@ class Datet
638
668
  # datet.min = 35
639
669
  # datet.time #=> 2012-05-09 05:35:08 +0200
640
670
  def min=(newmin)
641
- @t_min = newmin.to_i
671
+ newmin = newmin.to_i
672
+ raise ArgumentError, "Invalid minute: '#{newmin}'." if newmin < 0 or newmin > 60
673
+ @t_min = newmin
642
674
  end
643
675
 
644
676
  #Changes the second to a given new second.
@@ -647,10 +679,17 @@ class Datet
647
679
  # datet.sec = 20
648
680
  # datet.time #=> 2012-05-09 05:35:20 +0200
649
681
  def sec=(newsec)
682
+ newsec = newsec.to_i
683
+ raise ArgumentError, "Invalid second: '#{newsec}'." if newsec < 0 or newsec > 60
650
684
  @t_sec = newsec.to_i
651
685
  end
652
686
 
653
- alias :day :date
687
+ #Changes the usecond of the object.
688
+ def usec=(newusec)
689
+ newusec = newusec.to_i
690
+ raise ArgumentError, "Invalid usecond: '#{newusec}'." if newusec < 0 or newusec > 1000000
691
+ @t_usec = newusec
692
+ end
654
693
 
655
694
  #Changes the month to a given new month.
656
695
  #===Examples
@@ -659,7 +698,7 @@ class Datet
659
698
  # datet.time #=> 2012-07-09 05:35:20 +0200
660
699
  def month=(newmonth)
661
700
  newmonth = newmonth.to_i
662
- raise ArgumentError, "Invalid month: '#{newmonth}'." if newmonth <= 0
701
+ raise ArgumentError, "Invalid month: '#{newmonth}'." if newmonth <= 0 or newmonth > 12
663
702
  @t_month = newmonth
664
703
  end
665
704
 
@@ -853,6 +892,12 @@ class Datet
853
892
  return false
854
893
  end
855
894
 
895
+ #Returns true if this date is a nullstamp (0000-00-00 or something like it).
896
+ def nullstamp?
897
+ return true if @t_nil
898
+ return false
899
+ end
900
+
856
901
  #Returns the day of the year (0-365) as an integer.
857
902
  #===Examples
858
903
  # Datet.new.day_of_year #=> 123
@@ -1320,4 +1365,94 @@ class Datet
1320
1365
 
1321
1366
  return date_str
1322
1367
  end
1368
+
1369
+ #See the documentation for Time#strftime. It emulates that method.
1370
+ #This method is not complete yet and only contains some functionality.
1371
+ def strftime(str)
1372
+ replaces = {}
1373
+ res = "#{str}"
1374
+
1375
+ str.scan(/(%%|%(\^|)([A-z]))/) do |match|
1376
+ #The does the functionality of actually having a '%' in the string.
1377
+ if match[0] == "%%"
1378
+ replaces["%%"] = "%"
1379
+ next
1380
+ end
1381
+
1382
+ #Set up replace-hash for later replace.
1383
+ bigsign = match[1]
1384
+ letter = match[2]
1385
+
1386
+ le = "%#{bigsign}#{letter}"
1387
+
1388
+ #Skip if letter has already been set.
1389
+ next if replaces.key?(le)
1390
+
1391
+ case letter
1392
+ when "Y"
1393
+ replaces[le] = @t_year
1394
+ when "m"
1395
+ replaces[le] = "%02d" % @t_month
1396
+ when "d"
1397
+ replaces[le] = "%02d" % @t_day
1398
+ when "e"
1399
+ replaces[le] = @t_day
1400
+ when "H"
1401
+ replaces[le] = "%02d" % @t_hour
1402
+ when "k"
1403
+ replaces[le] = @t_hour
1404
+ when "l"
1405
+ replaces[le] = self.hour_thc
1406
+ when "I"
1407
+ replaces[le] = "%02d" % self.hour_thc
1408
+ when "M"
1409
+ replaces[le] = "%02d" % @t_min
1410
+ when "S"
1411
+ replaces[le] = "%02d" % @t_sec
1412
+ when "T"
1413
+ replaces[le] = "#{"%02d" % @t_hour}:#{"%02d" % @t_min}:#{"%02d" % @t_sec}"
1414
+ when "R"
1415
+ replaces[le] = "#{"%02d" % @t_hour}:#{"%02d" % @t_min}"
1416
+ when "r"
1417
+ replaces[le] = "#{"%02d" % self.hour_thc}:#{"%02d" % @t_min}:#{"%02d" % @t_sec} #{self.ampm.upcase}"
1418
+ when "B"
1419
+ replaces[le] = self.month_name
1420
+ when "b", "h"
1421
+ replaces[le] = self.month_name[0, 3]
1422
+ when "j"
1423
+ replaces[le] = self.day_of_year
1424
+ when "A"
1425
+ replaces[le] = self.day_name
1426
+ when "a"
1427
+ replaces[le] = self.day_name[0, 3]
1428
+ when "w"
1429
+ replaces[le] = self.day_in_week
1430
+ when "u"
1431
+ replaces[le] = self.day_in_week(:mfirst => true) + 1
1432
+ when "s"
1433
+ replaces[le] = self.to_i
1434
+ when "p"
1435
+ replaces[le] = self.ampm.upcase
1436
+ when "P"
1437
+ replaces[le] = self.ampm
1438
+ when "V"
1439
+ replaces[le] = self.week_no
1440
+ when "W"
1441
+ replaces[le] = self.week_no(:mfirst => true)
1442
+ end
1443
+
1444
+ #Replace should be uppercase.
1445
+ if bigsign == "^" and replaces.key?(le)
1446
+ replaces[le] = replaces[le].to_s.upcase
1447
+ end
1448
+ end
1449
+
1450
+ #Do the actual replaces.
1451
+ replaces.each do |key, val|
1452
+ res = res.gsub(key, val.to_s)
1453
+ end
1454
+
1455
+ #Return the replaced string.
1456
+ return res
1457
+ end
1323
1458
  end
@@ -337,4 +337,122 @@ describe "Datet" do
337
337
  datet.find(:incr => :month, :wday => 5)
338
338
  raise "Expected dbstr to be '1970-05-08' but it wasnt: '#{datet.dbstr(:time => false)}'." if datet.dbstr(:time => false) != "1970-05-08"
339
339
  end
340
+
341
+ it "should be able to calculate week numbers" do
342
+ tests = [
343
+ [Datet.new(1985, 1, 1), 1],
344
+ [Datet.new(1985, 12, 31), 53]
345
+ ]
346
+ tests.each do |data|
347
+ res = data[0].week_no
348
+ raise "Expected '#{data[1]}' but got '#{res}'." if res != data[1]
349
+ end
350
+ end
351
+
352
+ it "should emulate strftime" do
353
+ datet = Datet.new(1985, 6, 17, 10, 30, 25)
354
+
355
+ tests = {
356
+ "%Y" => 1985,
357
+ "%m" => "06",
358
+ "%d" => "17",
359
+ "%e" => 17,
360
+ "%H" => 10,
361
+ "%l" => 10,
362
+ "%I" => 10,
363
+ "%k" => 10,
364
+ "%M" => 30,
365
+ "%S" => 25,
366
+ "%T" => "10:30:25",
367
+ "%R" => "10:30",
368
+ "%r" => "10:30:25 AM",
369
+ "%p" => "am",
370
+ "%P" => "AM",
371
+ "%B" => "June",
372
+ "%^B" => "JUNE",
373
+ "%b" => "Jun",
374
+ "%h" => "Jun",
375
+ "%^b" => "JUN",
376
+ "%j" => 168,
377
+ "%A" => "Monday",
378
+ "%^A" => "MONDAY",
379
+ "%a" => "Mon",
380
+ "%^a" => "MON",
381
+ "%w" => 1,
382
+ "%s" => datet.to_i,
383
+ "%P" => "am",
384
+ "%p" => "AM",
385
+ "%V" => 25,
386
+ "%W" => 24,
387
+ "%%Y" => "%Y",
388
+ "%%%%%Y" => "%%1985",
389
+ "%%%%%%%%%Y" => "%%%%1985"
390
+ }
391
+
392
+ tests.each do |key, val|
393
+ res = datet.strftime(key)
394
+ raise "Expected '#{val}' but got: '#{res}' for '#{key}'." if val.to_s != res
395
+
396
+ res2 = datet.time.strftime(key)
397
+ raise "Expected res to be the same as time res but it wasnt: '#{res}', '#{res2}' for '#{key}'." if res != res2
398
+ end
399
+ end
400
+
401
+ it "should handle nil-dates gently" do
402
+ datet = Datet.new(0, 0, 0)
403
+
404
+ tests = [
405
+ [:dbstr, "0000-00-00 00:00:00"]
406
+ ]
407
+ tests.each do |data|
408
+ res = datet.__send__(data[0])
409
+ raise "Expected '#{data[1]}' but got '#{res}'." if res != data[1]
410
+ end
411
+ end
412
+
413
+ it "should not be possible to set invalid months and dates" do
414
+ datet = Datet.new
415
+
416
+ begin
417
+ datet.month = 14
418
+ raise "Should have raised error."
419
+ rescue ArgumentError
420
+ #ignore
421
+ end
422
+
423
+ begin
424
+ datet.usec = 1000005
425
+ raise "Should have raised error."
426
+ rescue ArgumentError
427
+ #ignore.
428
+ end
429
+
430
+ begin
431
+ datet.sec = 61
432
+ raise "Should have raised error."
433
+ rescue ArgumentError
434
+ #ignore
435
+ end
436
+
437
+ begin
438
+ datet.min = 70
439
+ raise "Should have raised error."
440
+ rescue ArgumentError
441
+ #ignore.
442
+ end
443
+
444
+ begin
445
+ datet.hour = 90
446
+ raise "Should have raised error."
447
+ rescue ArgumentError
448
+ #ignore.
449
+ end
450
+
451
+ begin
452
+ datet.day = 45
453
+ raise "Should have raised error."
454
+ rescue ArgumentError
455
+ #ignore.
456
+ end
457
+ end
340
458
  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.7
5
+ version: 0.0.8
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-07-15 00:00:00 +02:00
13
+ date: 2012-07-16 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: 3008034930327995715
96
+ hash: -2767607844783670018
97
97
  segments:
98
98
  - 0
99
99
  version: "0"