home_run 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,6 +1,10 @@
1
+ === 1.0.7 (2012-08-08)
2
+
3
+ * Make DateTime constructors handle fractional seconds in seconds argument (jeremyevans)
4
+
1
5
  === 1.0.6 (2012-02-27)
2
6
 
3
- * Merge DateTime#<=> into Date#<=> to work around ActiveSupport bug (#45, #41, #38, #36)
7
+ * Merge DateTime#<=> into Date#<=> to work around ActiveSupport bug (jeremyevans) (#45, #41, #38, #36)
4
8
 
5
9
  === 1.0.5 (2011-12-22)
6
10
 
@@ -154,6 +154,33 @@ void rhrdt__set_time(rhrdt_t *dt, long h, long m, long s, double offset) {
154
154
  dt->flags |= RHR_HAVE_HMS;
155
155
  }
156
156
 
157
+ /* Call rhrdt__set_time without the nanos. If there are any nanos, convert the hms information
158
+ * to nanos and add the given nanos. Note that the given nanos should be less than or equal to
159
+ * 1,000,000,000. */
160
+ void rhrdt__set_time_nanos(rhrdt_t *dt, long h, long m, long s, long long nanos, double offset) {
161
+ rhrdt__set_time(dt, h, m, s, offset);
162
+ if (nanos) {
163
+ rhrdt__hms_to_nanos(dt);
164
+ dt->nanos += nanos;
165
+ if (nanos == 1000000000) {
166
+ dt->second++;
167
+ }
168
+ }
169
+ }
170
+
171
+ /* Return the number of nano seconds in the given VALUE. Should
172
+ * return 0 unless rsecond has a fractional part, and should never
173
+ * be more than 1,000,000,000. */
174
+ long long rhrdt__nanos_from_second(VALUE rsecond, long second) {
175
+ double sec_fraction;
176
+
177
+ if ((sec_fraction = (NUM2DBL(rsecond) - second)) > 0) {
178
+ return llround(sec_fraction * 1000000000);
179
+ }
180
+
181
+ return 0;
182
+ }
183
+
157
184
  /* Same as rhrd__civil_to_jd for rhrdt_t. */
158
185
  void rhrdt__civil_to_jd(rhrdt_t *d) {
159
186
  d->jd = rhrd__ymd_to_jd(d->year, d->month, d->day);
@@ -617,6 +644,7 @@ static VALUE rhrdt_s_civil(int argc, VALUE *argv, VALUE klass) {
617
644
  long hour = 0;
618
645
  long minute = 0;
619
646
  long second = 0;
647
+ long long nanos = 0;
620
648
  double offset = 0.0;
621
649
  VALUE rdt = Data_Make_Struct(klass, rhrdt_t, NULL, -1, dt);
622
650
 
@@ -629,6 +657,7 @@ static VALUE rhrdt_s_civil(int argc, VALUE *argv, VALUE klass) {
629
657
  offset = rhrdt__constructor_offset(klass, argv[6]);
630
658
  case 6:
631
659
  second = NUM2LONG(argv[5]);
660
+ nanos = rhrdt__nanos_from_second(argv[5], second);
632
661
  case 5:
633
662
  minute = NUM2LONG(argv[4]);
634
663
  case 4:
@@ -648,7 +677,7 @@ static VALUE rhrdt_s_civil(int argc, VALUE *argv, VALUE klass) {
648
677
  if (!rhrdt__valid_civil(dt, year, month, day)) {
649
678
  rb_raise(rb_eArgError, "invalid date (year: %li, month: %li, day: %li)", year, month, day);
650
679
  }
651
- rhrdt__set_time(dt, hour, minute, second, offset);
680
+ rhrdt__set_time_nanos(dt, hour, minute, second, nanos, offset);
652
681
  return rdt;
653
682
  }
654
683
 
@@ -674,6 +703,7 @@ static VALUE rhrdt_s_commercial(int argc, VALUE *argv, VALUE klass) {
674
703
  long hour = 0;
675
704
  long minute = 0;
676
705
  long second = 0;
706
+ long long nanos = 0;
677
707
  double offset = 0.0;
678
708
  VALUE rdt = Data_Make_Struct(klass, rhrdt_t, NULL, -1, dt);
679
709
 
@@ -683,6 +713,7 @@ static VALUE rhrdt_s_commercial(int argc, VALUE *argv, VALUE klass) {
683
713
  offset = rhrdt__constructor_offset(klass, argv[6]);
684
714
  case 6:
685
715
  second = NUM2LONG(argv[5]);
716
+ nanos = rhrdt__nanos_from_second(argv[5], second);
686
717
  case 5:
687
718
  minute = NUM2LONG(argv[4]);
688
719
  case 4:
@@ -709,7 +740,7 @@ static VALUE rhrdt_s_commercial(int argc, VALUE *argv, VALUE klass) {
709
740
  if(!rhrdt__valid_commercial(dt, cwyear, cweek, cwday)) {
710
741
  rb_raise(rb_eArgError, "invalid date (cwyear: %li, cweek: %li, cwday: %li)", cwyear, cweek, cwday);
711
742
  }
712
- rhrdt__set_time(dt, hour, minute, second, offset);
743
+ rhrdt__set_time_nanos(dt, hour, minute, second, nanos, offset);
713
744
  return rdt;
714
745
  }
715
746
 
@@ -726,6 +757,7 @@ static VALUE rhrdt_s_jd(int argc, VALUE *argv, VALUE klass) {
726
757
  long hour = 0;
727
758
  long minute = 0;
728
759
  long second = 0;
760
+ long long nanos = 0;
729
761
  double offset = 0.0;
730
762
  VALUE rdt = Data_Make_Struct(klass, rhrdt_t, NULL, -1, dt);
731
763
 
@@ -738,6 +770,7 @@ static VALUE rhrdt_s_jd(int argc, VALUE *argv, VALUE klass) {
738
770
  offset = rhrdt__constructor_offset(klass, argv[4]);
739
771
  case 4:
740
772
  second = NUM2LONG(argv[3]);
773
+ nanos = rhrdt__nanos_from_second(argv[3], second);
741
774
  case 3:
742
775
  minute = NUM2LONG(argv[2]);
743
776
  case 2:
@@ -755,7 +788,7 @@ static VALUE rhrdt_s_jd(int argc, VALUE *argv, VALUE klass) {
755
788
 
756
789
  RHR_CHECK_JD(dt)
757
790
  dt->flags = RHR_HAVE_JD;
758
- rhrdt__set_time(dt, hour, minute, second, offset);
791
+ rhrdt__set_time_nanos(dt, hour, minute, second, nanos, offset);
759
792
  if (TYPE(argv[0]) == T_FLOAT) {
760
793
  return rhrdt__add_days(rdt, NUM2DBL(argv[0]) - NUM2LONG(argv[0]));
761
794
  } else {
@@ -850,6 +883,7 @@ static VALUE rhrdt_s_ordinal(int argc, VALUE *argv, VALUE klass) {
850
883
  long hour = 0;
851
884
  long minute = 0;
852
885
  long second = 0;
886
+ long long nanos = 0;
853
887
  double offset = 0.0;
854
888
  rhrdt_t *dt;
855
889
  VALUE rdt = Data_Make_Struct(klass, rhrdt_t, NULL, -1, dt);
@@ -860,6 +894,7 @@ static VALUE rhrdt_s_ordinal(int argc, VALUE *argv, VALUE klass) {
860
894
  offset = rhrdt__constructor_offset(klass, argv[5]);
861
895
  case 5:
862
896
  second = NUM2LONG(argv[4]);
897
+ nanos = rhrdt__nanos_from_second(argv[4], second);
863
898
  case 4:
864
899
  minute = NUM2LONG(argv[3]);
865
900
  case 3:
@@ -881,7 +916,7 @@ static VALUE rhrdt_s_ordinal(int argc, VALUE *argv, VALUE klass) {
881
916
  RHR_CHECK_JD(dt)
882
917
  rb_raise(rb_eArgError, "invalid date (year: %li, yday: %li)", year, day);
883
918
  }
884
- rhrdt__set_time(dt, hour, minute, second, offset);
919
+ rhrdt__set_time_nanos(dt, hour, minute, second, nanos, offset);
885
920
  return rdt;
886
921
  }
887
922
 
@@ -176,4 +176,12 @@ describe "DateTime constructors" do
176
176
  DateTime.ordinal(2008, 1, 0, 0, 0, '+1200').should == DateTime.ordinal(2008, 1, 0, 0, 0, 0.5)
177
177
  DateTime.new(2008, 1, 1, 0, 0, 0, '+1200').should == DateTime.new(2008, 1, 1, 0, 0, 0, 0.5)
178
178
  end
179
+
180
+ it "should handle fractional seconds in seconds" do
181
+ DateTime.jd(2008, 0, 0, 1.5).should == DateTime.jd(2008, 0, 0, 1) + (0.5/86400)
182
+ DateTime.civil(2008, 1, 1, 0, 0, 1.5).should == DateTime.civil(2008, 1, 1, 0, 0, 1) + (0.5/86400)
183
+ DateTime.commercial(2008, 1, 1, 0, 0, 1.5).should == DateTime.commercial(2008, 1, 1, 0, 0, 1) + (0.5/86400)
184
+ DateTime.ordinal(2008, 1, 0, 0, 1.5).should == DateTime.ordinal(2008, 1, 0, 0, 1) + (0.5/86400)
185
+ DateTime.new(2008, 1, 1, 0, 0, 1.5).should == DateTime.new(2008, 1, 1, 0, 0, 1) + (0.5/86400)
186
+ end
179
187
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: home_run
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-27 00:00:00.000000000 Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'home_run is an implementation of ruby''s Date/DateTime classes in
15
15
  C,
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  version: '0'
143
143
  requirements: []
144
144
  rubyforge_project:
145
- rubygems_version: 1.8.11
145
+ rubygems_version: 1.8.23
146
146
  signing_key:
147
147
  specification_version: 3
148
148
  summary: Fast Date/DateTime classes for ruby