datet 0.0.8 → 0.0.9

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 +62 -1
  4. data/spec/datet_spec.rb +21 -0
  5. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 0.0.9
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"
8
+ s.version = "0.0.9"
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"]
data/lib/datet.rb CHANGED
@@ -657,11 +657,32 @@ class Datet
657
657
  # datet.time #=> 2012-05-09 05:36:08 +0200
658
658
  def hour=(newhour)
659
659
  newhour = newhour.to_i
660
- raise ArgumentError, "Invalid hour: '#{newhour}'." if newhour < 0 or newhour > 60
660
+ raise ArgumentError, "Invalid hour: '#{newhour}'." if newhour < 0 or newhour > 24
661
661
  @t_hour = newhour
662
662
  return self
663
663
  end
664
664
 
665
+ #Changes the hour to a new hour. If more than 60 is given, then the difference is converted into added days. If decimals are given, the minute of the timestamp is sat based on those decimals.
666
+ def lazy_hour=(newhour)
667
+ newhour = newhour.to_f
668
+
669
+ #Add days for every 24 hours given.
670
+ if newhour > 24
671
+ days = (newhour.to_f / 24.0).floor
672
+ newhour -= (days.to_f * 24.0)
673
+ self.add_days(days)
674
+ end
675
+
676
+ #Convert any decimals to setting minute.
677
+ diff = newhour - newhour.floor
678
+ self.lazy_min = diff * 60 if diff > 0.0
679
+
680
+ #Set the actual hour.
681
+ self.hour = newhour.floor
682
+
683
+ self
684
+ end
685
+
665
686
  #Changes the minute to a given new minute.
666
687
  #===Examples
667
688
  # datet.time #=> 2012-05-09 05:36:08 +0200
@@ -673,6 +694,32 @@ class Datet
673
694
  @t_min = newmin
674
695
  end
675
696
 
697
+ #Changes the minute to a new minute. If more than 60 is given, then the difference is converted into added hours. If decimals are given, the second of the timestamp is sat based on those decimals.
698
+ #===Examples
699
+ # datet = Datet.new #=> 2012-07-16 19:53:07
700
+ # datet.lazy_min = 30.5 #=> 2012-07-16 19:30:30
701
+ # datet.lazy_min = 90.5 #=> 2012-07-16 20:30:30
702
+ def lazy_min=(newmin)
703
+ newmin = newmin.to_f
704
+ raise ArgumentError, "Invalid minute: '#{newmin}'." if newmin < 0
705
+
706
+ #Add hours for every 60 minutes given.
707
+ if newmin > 60
708
+ hours = (newmin.to_f / 60.0).floor
709
+ newmin -= (hours.to_f * 60.0)
710
+ self.add_hours(hours)
711
+ end
712
+
713
+ #Convert any decimals to setting the second.
714
+ diff = newmin - newmin.floor
715
+ self.lazy_sec = diff * 60 if diff > 0.0
716
+
717
+ #Set the actual minute.
718
+ @t_min = newmin.floor
719
+
720
+ self
721
+ end
722
+
676
723
  #Changes the second to a given new second.
677
724
  #===Examples
678
725
  # datet.time #=> 2012-05-09 05:35:08 +0200
@@ -684,6 +731,20 @@ class Datet
684
731
  @t_sec = newsec.to_i
685
732
  end
686
733
 
734
+ #Changes the second to a given new second. If more than 60 is given, then the difference is converted into added minutes.
735
+ def lazy_sec=(newsec)
736
+ newsec = newsec.to_i
737
+ raise ArgumentError, "Invalid second: '#{newsec}'." if newsec < 0
738
+
739
+ if newsec > 60
740
+ mins = (newsec.to_f / 60.0).floor
741
+ newsec -= (mins * 60)
742
+ self.add_mins(mins)
743
+ end
744
+
745
+ @t_sec = newsec
746
+ end
747
+
687
748
  #Changes the usecond of the object.
688
749
  def usec=(newusec)
689
750
  newusec = newusec.to_i
data/spec/datet_spec.rb CHANGED
@@ -455,4 +455,25 @@ describe "Datet" do
455
455
  #ignore.
456
456
  end
457
457
  end
458
+
459
+ it "should be able to set seconds lazy" do
460
+ datet = Datet.new(1985, 6, 17, 10)
461
+ datet.lazy_sec = 125
462
+ raise "Expected time to be '1985-06-17 10:02:05' but it was: '#{datet.dbstr}'." if datet.dbstr != "1985-06-17 10:02:05"
463
+ end
464
+
465
+ it "should be able to set minutes lazy" do
466
+ datet = Datet.new(1985, 6, 17, 10)
467
+ datet.lazy_min = 125
468
+ raise "Expected time to be '1985-06-17 12:05:00' but it was: '#{datet.dbstr}'." if datet.dbstr != "1985-06-17 12:05:00"
469
+
470
+ datet.lazy_min = 30.5
471
+ raise "Expected time to be '1985-06-17 12:30:30' but it was: '#{datet.dbstr}'." if datet.dbstr != "1985-06-17 12:30:30"
472
+ end
473
+
474
+ it "should be able to set hours lazy" do
475
+ datet = Datet.new(1985, 6, 17, 10)
476
+ datet.lazy_hour = 28.5
477
+ raise "Expected time to be '1985-06-18 04:30:00' but it was: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 04:30:00"
478
+ end
458
479
  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.8
5
+ version: 0.0.9
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
- hash: -2767607844783670018
96
+ hash: 1974701347137718531
97
97
  segments:
98
98
  - 0
99
99
  version: "0"