home_run 0.9.0-x86-mingw32 → 0.9.1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,146 +1,5 @@
1
-
2
- #include <math.h>
3
- #include <stdio.h>
4
- #include <stdlib.h>
5
- #include <inttypes.h>
6
- #include <ruby.h>
7
-
8
- #ifdef RUBY19
9
- #define RHR_DEFAULT_CWYEAR -4713
10
- #define RHR_DEFAULT_CWEEK 1
11
- #define RHR_DEFAULT_CWDAY 1
12
- #else
13
- #define RHR_DEFAULT_CWYEAR 1582
14
- #define RHR_DEFAULT_CWEEK 41
15
- #define RHR_DEFAULT_CWDAY 5
16
- #endif
17
-
18
- #define RHR_JD_MJD 2400001
19
- #define RHR_JD_LD 2299160
20
- #define RHR_JD_ITALY 2299161
21
- #define RHR_JD_ENGLAND 2361222
22
- #define RHR_UNIX_EPOCH 2440588
23
- #define RHR_SECONDS_PER_HOUR 3600
24
- #define RHR_MINUTES_PER_DAYD 1440.0
25
- #define RHR_SECONDS_PER_DAY 86400
26
- #define RHR_SECONDS_PER_DAYD 86400.0
27
- #define RHR_MAX_OFFSET_MINUTES 864
28
- #define RHR_MAX_OFFSET_FRACT 0.6
29
- #define RHR_MIN_OFFSET_MINUTES -864
30
- #define RHR_MIN_OFFSET_FRACT -0.6
31
-
32
- /*
33
- In both the 32-bit and 64-bit cases, the limits are chosen so that you cannot
34
- store a civil date where converting it to a jd would cause an overflow.
35
- */
36
- #if __LP64__
37
- /*
38
- On 64-bit systems, the limits depend on the number of significant digits in
39
- a double (15). These are slightly below the maximum so that all numbers used
40
- in calculations have fewer than 15 digits.
41
- */
42
- #define RHR_JD_MAX 999979466117609
43
- #define RHR_JD_MIN -999979466119058
44
- #define RHR_YEAR_MAX 2737850782415
45
- #define RHR_MONTH_MAX 12
46
- #define RHR_DAY_MAX 5
47
- #define RHR_YEAR_MIN -2737850791845
48
- #define RHR_MONTH_MIN 11
49
- #define RHR_DAY_MIN 26
50
-
51
- #else
52
- /*
53
- On 32-bit systems, the limits depend on the storage limits of 32-bit integers.
54
- The numbers are slightly less than 2**31 - 1 and slightly greater than -2**31
55
- so that no calculations can overflow.
56
- */
57
- #define RHR_JD_MAX 2147438064
58
- #define RHR_JD_MIN -2145083647
59
- #define RHR_YEAR_MAX 5874773
60
- #define RHR_MONTH_MAX 8
61
- #define RHR_DAY_MAX 15
62
- #define RHR_YEAR_MIN -5877752
63
- #define RHR_MONTH_MIN 5
64
- #define RHR_DAY_MIN 8
65
- #endif
66
-
67
- #define RHR_HAVE_JD 1
68
- #define RHR_HAVE_CIVIL 2
69
- #define RHR_HAVE_NANOS 4
70
- #define RHR_HAVE_HMS 8
71
-
72
- #define RHR_NANOS_PER_MILLISECOND 1000000LL
73
- #define RHR_NANOS_PER_SECOND 1000000000LL
74
- #define RHR_NANOS_PER_MINUTE 60000000000LL
75
- #define RHR_NANOS_PER_DAY 86400000000000LL
76
- #define RHR_NANOS_PER_DAYD 86400000000000.0
77
- #define RHR_NANOS_PER_SECONDD 1000000000.0
78
-
79
- #define RHRR_YEAR_SET 0x1
80
- #define RHRR_MONTH_SET 0x2
81
- #define RHRR_DAY_SET 0x4
82
- #define RHRR_YDAY_SET 0x8
83
- #define RHRR_HOUR_SET 0x10
84
- #define RHRR_MINUTE_SET 0x20
85
- #define RHRR_SECOND_SET 0x40
86
- #define RHRR_WDAY_SET 0x80
87
- #define RHRR_CENTURY_SET 0x100
88
- #define RHRR_CWYEAR_SET 0x200
89
- #define RHRR_CWEEK_SET 0x400
90
- #define RHRR_CWDAY_SET 0x800
91
- #define RHRR_SEC_FRACTION_SET 0x1000
92
- #define RHRR_UNIX_SET 0x2000
93
- #define RHRR_WNUM0_SET 0x4000
94
- #define RHRR_WNUM1_SET 0x8000
95
- #define RHRR_MERIDIAN_SET 0x10000
96
- #define RHRR_ZONE_SET 0x20000
97
- #define RHRR_OFFSET_SET 0x40000
98
- #define RHRR_UNIXM_SET 0x80000
99
-
100
- #define RHR_HAS_JD(d) (((d)->flags & RHR_HAVE_JD) == RHR_HAVE_JD)
101
- #define RHR_HAS_CIVIL(d) (((d)->flags & RHR_HAVE_CIVIL) == RHR_HAVE_CIVIL)
102
-
103
- #define RHR_FILL_JD(d) if (((d)->flags & RHR_HAVE_JD) == 0) { rhrd__civil_to_jd(d); }
104
- #define RHR_FILL_CIVIL(d) if (((d)->flags & RHR_HAVE_CIVIL) == 0) { rhrd__jd_to_civil(d); }
105
-
106
- #define RHRDT_FILL_JD(d) if (!((d)->flags & RHR_HAVE_JD)) { rhrdt__civil_to_jd(d); }
107
- #define RHRDT_FILL_CIVIL(d) if (!((d)->flags & RHR_HAVE_CIVIL)) { rhrdt__jd_to_civil(d); }
108
- #define RHRDT_FILL_HMS(d) if (!((d)->flags & RHR_HAVE_HMS)) { rhrdt__nanos_to_hms(d); }
109
- #define RHRDT_FILL_NANOS(d) if (!((d)->flags & RHR_HAVE_NANOS)) { rhrdt__hms_to_nanos(d); }
110
-
111
- #ifdef RUBY186
112
- #define RHR_RETURN_RESIZED_STR(s, len) return rb_str_resize(s, len);
113
- #else
114
- #define RHR_RETURN_RESIZED_STR(s, len) rb_str_set_len(s, len); return s;
115
- #endif
116
-
117
- #define RHR_SPACE_SHIP(x, l, r) if (l < r) { x = -1; } else if (l == r) { x = 0; } else { x = 1; }
118
-
119
- #define RHR_CHECK_JD(d) if ((d->jd > RHR_JD_MAX) || (d->jd < RHR_JD_MIN)) { rb_raise(rb_eRangeError, "date out of range: jd = %li", d->jd);}
120
- #define RHR_CHECK_CIVIL(d) if (!rhrd__valid_civil_limits(d->year, d->month, d->day)) { rb_raise(rb_eRangeError, "date out of range: year = %li, month = %hhi, day = %hhi", d->year, d->month, d->day);}
121
-
122
- #define RHR_CACHED_IV(self, iv) VALUE v = rb_ivar_get(self, iv); if (RTEST(v)) {return v;}
123
-
124
- typedef struct rhrd_s {
125
- long jd;
126
- long year;
127
- unsigned char month;
128
- unsigned char day;
129
- unsigned char flags;
130
- } rhrd_t;
131
-
132
- typedef struct rhrdt_s {
133
- long long nanos; /* Nanoseconds since start of day */
134
- long jd;
135
- long year;
136
- short offset; /* Offset from UTC in minutes */
137
- unsigned char month;
138
- unsigned char day;
139
- unsigned char hour;
140
- unsigned char minute;
141
- unsigned char second;
142
- unsigned char flags;
143
- } rhrdt_t;
1
+ #include <ctype.h>
2
+ #include "date_ext.h"
144
3
 
145
4
  const unsigned char rhrd_days_in_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
146
5
  const long rhrd_cumulative_days_in_month[13] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
@@ -153,6 +12,7 @@ const char * rhrd__abbr_day_names[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri"
153
12
  const char rhrd__zone_re_str[] = "\\A(?:gmt|utc?)?[-+]\\d+(?:[,.:]\\d+(?::\\d+)?)?|[[:alpha:].\\s]+(?:standard|daylight)\\s+time\\b|[[:alpha:]]+(?:\\s+dst)?\\b";
154
13
  const char rhrd__zone_dst_re_str[] = "\\s+(?:(?:(standard)|daylight)\\s+time|dst)\\z";
155
14
  const char rhrd__zone_sign_re_str[] = "\\A(?:gmt|utc?)?(?:(-)|\\+)";
15
+ const char rhrd__strptime_num_patterns[] = "CDdeFGgHIjkLlMmNQRrSsTUuVvWwXxYy0123456789";
156
16
 
157
17
  VALUE rhrd_class;
158
18
  VALUE rhrd_s_class;
@@ -191,6 +51,7 @@ ID rhrd_id_offset;
191
51
  ID rhrd_id_slice;
192
52
  ID rhrd_id_split;
193
53
  ID rhrd_id_sub_b;
54
+ ID rhrd_id_to_enum;
194
55
  ID rhrd_id_to_i;
195
56
  #ifdef RUBY19
196
57
  ID rhrd_id_nsec;
@@ -223,6 +84,7 @@ VALUE rhrd_sym_offset;
223
84
  VALUE rhrd_sym_sec;
224
85
  VALUE rhrd_sym_sec_fraction;
225
86
  VALUE rhrd_sym_seconds;
87
+ VALUE rhrd_sym_step;
226
88
  VALUE rhrd_sym_wday;
227
89
  VALUE rhrd_sym_wnum0;
228
90
  VALUE rhrd_sym_wnum1;
@@ -231,15 +93,7 @@ VALUE rhrd_sym_year;
231
93
  VALUE rhrd_sym_zone;
232
94
 
233
95
  static VALUE rhrd_step(int argc, VALUE *argv, VALUE self);
234
- static VALUE rhrdt_step(int argc, VALUE *argv, VALUE self);
235
96
  static VALUE rhrd_to_s(VALUE self);
236
- static VALUE rhrdt_to_s(VALUE self);
237
- static VALUE rhrd_s_zone_to_diff(VALUE self, VALUE zone);
238
- VALUE rhrdt__new_offset(VALUE self, double offset);
239
- void rhrdt__civil_to_jd(rhrdt_t *d);
240
- void rhrdt__jd_to_civil(rhrdt_t *date);
241
- void rhrdt__nanos_to_hms(rhrdt_t *d);
242
- void rhrdt__hms_to_nanos(rhrdt_t *d);
243
97
 
244
98
  /* C helper methods */
245
99
 
@@ -264,8 +118,8 @@ long rhrd__modll(long long a, long b) {
264
118
  return c;
265
119
  }
266
120
 
267
- /* Return 0 if the year, month, day provided are greater than
268
- * the allowed limits, 1 otherwise. */
121
+ /* Return 1 if the year, month, day provided are within
122
+ * the allowed limits, 0 otherwise. */
269
123
  int rhrd__valid_civil_limits(long year, long month, long day) {
270
124
  if (year > RHR_YEAR_MAX || year < RHR_YEAR_MIN) {
271
125
  return 0;
@@ -365,7 +219,7 @@ int rhrd__leap_year(long year) {
365
219
  * fields in the rhrd_t and returning 1 if so. If the fields
366
220
  * given are not a valid date, return 0.
367
221
  * This also handles wrap around if the month or day is negative. */
368
- int rhrd__valid_civil(rhrd_t *d, long year, long month, long day) {
222
+ int rhrd__valid_civil(rhrd_t *d, long year, long month, long day, int overlimit_raise) {
369
223
  if (month < 0 && month >= -12) {
370
224
  month += 13;
371
225
  }
@@ -397,6 +251,9 @@ int rhrd__valid_civil(rhrd_t *d, long year, long month, long day) {
397
251
  }
398
252
 
399
253
  if(!rhrd__valid_civil_limits(year, month, day)) {
254
+ if (overlimit_raise == RHR_OVERLIMIT_RAISE) {
255
+ rb_raise(rb_eRangeError, "date out of range: year = %li, month = %li, day = %li", year, month, day);
256
+ }
400
257
  return 0;
401
258
  }
402
259
 
@@ -425,7 +282,7 @@ VALUE rhrd__add_days(VALUE self, long n) {
425
282
  VALUE new;
426
283
  long x;
427
284
  Data_Get_Struct(self, rhrd_t, d);
428
- new = Data_Make_Struct(rhrd_class, rhrd_t, NULL, free, newd);
285
+ new = Data_Make_Struct(rhrd_class, rhrd_t, NULL, -1, newd);
429
286
 
430
287
  if(!(RHR_HAS_JD(d))) {
431
288
  x = rhrd__safe_add_long(n, (long)(d->day));
@@ -456,7 +313,7 @@ VALUE rhrd__add_months(VALUE self, long n) {
456
313
  long x;
457
314
  Data_Get_Struct(self, rhrd_t, d);
458
315
 
459
- new = Data_Make_Struct(rhrd_class, rhrd_t, NULL, free, newd);
316
+ new = Data_Make_Struct(rhrd_class, rhrd_t, NULL, -1, newd);
460
317
  RHR_FILL_CIVIL(d)
461
318
  n = rhrd__safe_add_long(n, (long)(d->month));
462
319
  if(n > 1 && n <= 12) {
@@ -556,7 +413,7 @@ void rhrd__fill_commercial(rhrd_t *d) {
556
413
  * and cwday arguments, if the date is valid, and return 1. If the
557
414
  * date is not valid, return 0. This also handles wrap around if
558
415
  * the cweek or cwday arguments is negative. */
559
- int rhrd__valid_commercial(rhrd_t *d, long cwyear, long cweek, long cwday) {
416
+ int rhrd__valid_commercial(rhrd_t *d, long cwyear, long cweek, long cwday, int overlimit_raise) {
560
417
  rhrd_t n;
561
418
  memset(&n, 0, sizeof(rhrd_t));
562
419
 
@@ -586,6 +443,9 @@ int rhrd__valid_commercial(rhrd_t *d, long cwyear, long cweek, long cwday) {
586
443
  }
587
444
 
588
445
  if ((n.jd > RHR_JD_MAX) || (n.jd < RHR_JD_MIN)) {
446
+ if (overlimit_raise == RHR_OVERLIMIT_RAISE) {
447
+ rb_raise(rb_eRangeError, "date out of range");
448
+ }
589
449
  return 0;
590
450
  }
591
451
 
@@ -608,7 +468,7 @@ long rhrd__ordinal_day(long year, unsigned char month, unsigned char day) {
608
468
  * given ordinal day fields, if they are valid. Returns
609
469
  * 1 if the date is valid, 0 if not. This also handles
610
470
  * wrap around for a negative yday argument. */
611
- int rhrd__valid_ordinal(rhrd_t *d, long year, long yday) {
471
+ int rhrd__valid_ordinal(rhrd_t *d, long year, long yday, int overlimit_raise) {
612
472
  int leap;
613
473
  long month, day;
614
474
 
@@ -636,6 +496,9 @@ int rhrd__valid_ordinal(rhrd_t *d, long year, long yday) {
636
496
  }
637
497
 
638
498
  if(!rhrd__valid_civil_limits(year, month, day)) {
499
+ if (overlimit_raise == RHR_OVERLIMIT_RAISE) {
500
+ rb_raise(rb_eRangeError, "date out of range");
501
+ }
639
502
  return 0;
640
503
  }
641
504
 
@@ -778,7 +641,7 @@ int rhrd__fill_from_hash(rhrd_t *d, VALUE hash) {
778
641
  d->jd = rhrd__yday1_jd(year);
779
642
  d->flags |= RHR_HAVE_JD;
780
643
  rhrd__fill_commercial(d);
781
- if(!rhrd__valid_commercial(d, d->year, 1, NUM2LONG(rwday))) {
644
+ if(!rhrd__valid_commercial(d, d->year, 1, NUM2LONG(rwday), RHR_NO_RAISE)) {
782
645
  return 1;
783
646
  }
784
647
  d->flags &= ~RHR_HAVE_CIVIL;
@@ -835,15 +698,17 @@ int rhrd__fill_from_hash(rhrd_t *d, VALUE hash) {
835
698
  } else {
836
699
  return -1;
837
700
  }
838
- if (yday && rhrd__valid_ordinal(d, year, yday)) {
701
+
702
+ if (yday && rhrd__valid_ordinal(d, year, yday, RHR_OVERLIMIT_RAISE)) {
839
703
  return 0;
840
- } else if (cweek && cwday && rhrd__valid_commercial(d, cwyear, cweek, cwday)) {
704
+ }
705
+ if (cweek && cwday && rhrd__valid_commercial(d, cwyear, cweek, cwday, RHR_OVERLIMIT_RAISE)) {
841
706
  return 0;
842
- } else if (!rhrd__valid_civil(d, year, month, day)) {
843
- return 1;
844
707
  }
845
-
846
- return 0;
708
+ if (rhrd__valid_civil(d, year, month, day, RHR_OVERLIMIT_RAISE)) {
709
+ return 0;
710
+ }
711
+ return 1;
847
712
  }
848
713
 
849
714
  /* Returns a new ruby object filled with information from
@@ -851,7 +716,7 @@ int rhrd__fill_from_hash(rhrd_t *d, VALUE hash) {
851
716
  * did not contain valid date information. */
852
717
  VALUE rhrd__from_hash(VALUE hash) {
853
718
  rhrd_t *d;
854
- VALUE rd = Data_Make_Struct(rhrd_class, rhrd_t, NULL, free, d);
719
+ VALUE rd = Data_Make_Struct(rhrd_class, rhrd_t, NULL, -1, d);
855
720
  if(rhrd__fill_from_hash(d, hash)) {
856
721
  rb_raise(rb_eArgError, "invalid date");
857
722
  } else {
@@ -1071,6 +936,7 @@ VALUE rhrd__strptime(VALUE rstr, const char *fmt_str, long fmt_len) {
1071
936
  long wnum1 = 0;
1072
937
  long i;
1073
938
  long fmt_pos;
939
+ int year_only4 = 0;
1074
940
  int scan_len;
1075
941
  VALUE zone = Qnil;
1076
942
  VALUE hash;
@@ -1180,7 +1046,16 @@ VALUE rhrd__strptime(VALUE rstr, const char *fmt_str, long fmt_len) {
1180
1046
  state |= RHRR_CWYEAR_SET | RHRR_CENTURY_SET;
1181
1047
  break;
1182
1048
  case 'G':
1183
- if (sscanf(str + pos, "%ld%n", &cwyear, &scan_len) != 1) {
1049
+ #define RHR_PARSE_year_only4 year_only4 = 0;\
1050
+ if (fmt_pos + 1 < fmt_len) {\
1051
+ if(isdigit(fmt_str[fmt_pos + 1])) {\
1052
+ year_only4 = 1;\
1053
+ } else if ((fmt_pos + 2 < fmt_len) && (fmt_str[fmt_pos + 1] == '%') && strchr(rhrd__strptime_num_patterns, fmt_str[fmt_pos + 2])) {\
1054
+ year_only4 = 1;\
1055
+ }\
1056
+ }
1057
+ RHR_PARSE_year_only4
1058
+ if (sscanf(str + pos, year_only4 ? "%4ld%n" : "%ld%n", &cwyear, &scan_len) != 1) {
1184
1059
  return Qnil;
1185
1060
  }
1186
1061
  state |= RHRR_CWYEAR_SET + RHRR_CENTURY_SET;
@@ -1365,10 +1240,11 @@ VALUE rhrd__strptime(VALUE rstr, const char *fmt_str, long fmt_len) {
1365
1240
  RHR_PARSE_y
1366
1241
  break;
1367
1242
  case 'Y':
1368
- #define RHR_PARSE_Y if (sscanf(str + pos, "%ld%n", &year, &scan_len) != 1) {\
1243
+ #define RHR_PARSE_Y if (sscanf(str + pos, year_only4 ? "%4ld%n" : "%ld%n", &year, &scan_len) != 1) {\
1369
1244
  return Qnil;\
1370
1245
  }\
1371
1246
  state |= RHRR_YEAR_SET + RHRR_CENTURY_SET;
1247
+ RHR_PARSE_year_only4
1372
1248
  RHR_PARSE_Y
1373
1249
  break;
1374
1250
  case 'z':
@@ -1402,6 +1278,7 @@ VALUE rhrd__strptime(VALUE rstr, const char *fmt_str, long fmt_len) {
1402
1278
  RHR_PARSE_sep(':')
1403
1279
  RHR_PARSE_S
1404
1280
  RHR_PARSE_sep(' ')
1281
+ RHR_PARSE_year_only4
1405
1282
  RHR_PARSE_Y
1406
1283
  break;
1407
1284
  case 'x':
@@ -1446,6 +1323,7 @@ VALUE rhrd__strptime(VALUE rstr, const char *fmt_str, long fmt_len) {
1446
1323
  RHR_PARSE_sep('-')
1447
1324
  RHR_PARSE_b
1448
1325
  RHR_PARSE_sep('-')
1326
+ RHR_PARSE_year_only4
1449
1327
  RHR_PARSE_Y
1450
1328
  break;
1451
1329
  case '+':
@@ -1463,6 +1341,7 @@ VALUE rhrd__strptime(VALUE rstr, const char *fmt_str, long fmt_len) {
1463
1341
  RHR_PARSE_sep(' ')
1464
1342
  RHR_PARSE_Z
1465
1343
  RHR_PARSE_sep(' ')
1344
+ RHR_PARSE_year_only4
1466
1345
  RHR_PARSE_Y
1467
1346
  break;
1468
1347
  default:
@@ -1569,8 +1448,6 @@ void rhrd__set_cw_ivs(VALUE self, rhrd_t *d) {
1569
1448
  rb_ivar_set(self, rhrd_id_cwday, LONG2NUM(d->day));
1570
1449
  }
1571
1450
 
1572
- #include "date_parser.c"
1573
-
1574
1451
  /* Ruby class methods */
1575
1452
 
1576
1453
  /* call-seq:
@@ -1587,7 +1464,7 @@ static VALUE rhrd_s__load(VALUE klass, VALUE string) {
1587
1464
  rhrd_t * d;
1588
1465
  VALUE jd, rd;
1589
1466
  jd = rb_marshal_load(string);
1590
- rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
1467
+ rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
1591
1468
  d->jd = NUM2LONG(jd);
1592
1469
  RHR_CHECK_JD(d)
1593
1470
  d->flags = RHR_HAVE_JD;
@@ -1648,7 +1525,7 @@ static VALUE rhrd_s_civil(int argc, VALUE *argv, VALUE klass) {
1648
1525
  long year;
1649
1526
  long month = 1;
1650
1527
  long day = 1;
1651
- VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
1528
+ VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
1652
1529
 
1653
1530
  switch(argc) {
1654
1531
  case 3:
@@ -1667,8 +1544,7 @@ static VALUE rhrd_s_civil(int argc, VALUE *argv, VALUE klass) {
1667
1544
  break;
1668
1545
  }
1669
1546
 
1670
- if (!rhrd__valid_civil(d, year, month, day)) {
1671
- RHR_CHECK_CIVIL(d)
1547
+ if (!rhrd__valid_civil(d, year, month, day, RHR_OVERLIMIT_RAISE)) {
1672
1548
  rb_raise(rb_eArgError, "invalid date (year: %li, month: %li, day: %li)", year, month, day);
1673
1549
  }
1674
1550
  return rd;
@@ -1693,7 +1569,7 @@ static VALUE rhrd_s_commercial(int argc, VALUE *argv, VALUE klass) {
1693
1569
  long cwyear = RHR_DEFAULT_CWYEAR;
1694
1570
  long cweek = RHR_DEFAULT_CWEEK;
1695
1571
  long cwday = RHR_DEFAULT_CWDAY;
1696
- VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
1572
+ VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
1697
1573
 
1698
1574
  switch(argc) {
1699
1575
  case 3:
@@ -1717,8 +1593,7 @@ static VALUE rhrd_s_commercial(int argc, VALUE *argv, VALUE klass) {
1717
1593
  break;
1718
1594
  }
1719
1595
 
1720
- if(!rhrd__valid_commercial(d, cwyear, cweek, cwday)) {
1721
- RHR_CHECK_JD(d)
1596
+ if(!rhrd__valid_commercial(d, cwyear, cweek, cwday, RHR_OVERLIMIT_RAISE)) {
1722
1597
  rb_raise(rb_eArgError, "invalid date (cwyear: %li, cweek: %li, cwday: %li)", cwyear, cweek, cwday);
1723
1598
  }
1724
1599
  return rd;
@@ -1742,7 +1617,7 @@ static VALUE rhrd_s_gregorian_leap_q(VALUE klass, VALUE year) {
1742
1617
  */
1743
1618
  static VALUE rhrd_s_jd (int argc, VALUE *argv, VALUE klass) {
1744
1619
  rhrd_t *d;
1745
- VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
1620
+ VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
1746
1621
 
1747
1622
  switch(argc) {
1748
1623
  case 0:
@@ -1779,7 +1654,7 @@ static VALUE rhrd_s_julian_leap_q(VALUE klass, VALUE y) {
1779
1654
  */
1780
1655
  static VALUE rhrd_s_new_b(int argc, VALUE *argv, VALUE klass) {
1781
1656
  rhrd_t *d;
1782
- VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
1657
+ VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
1783
1658
 
1784
1659
  switch(argc) {
1785
1660
  case 0:
@@ -1812,7 +1687,7 @@ static VALUE rhrd_s_ordinal(int argc, VALUE *argv, VALUE klass) {
1812
1687
  long year;
1813
1688
  long day = 1;
1814
1689
  rhrd_t *d;
1815
- VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
1690
+ VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
1816
1691
 
1817
1692
  switch(argc) {
1818
1693
  case 2:
@@ -1820,8 +1695,7 @@ static VALUE rhrd_s_ordinal(int argc, VALUE *argv, VALUE klass) {
1820
1695
  day = NUM2LONG(argv[1]);
1821
1696
  case 1:
1822
1697
  year = NUM2LONG(argv[0]);
1823
- if(!rhrd__valid_ordinal(d, year, day)) {
1824
- RHR_CHECK_JD(d)
1698
+ if(!rhrd__valid_ordinal(d, year, day, RHR_OVERLIMIT_RAISE)) {
1825
1699
  rb_raise(rb_eArgError, "invalid date (year: %li, yday: %li)", year, day);
1826
1700
  }
1827
1701
  break;
@@ -1854,7 +1728,7 @@ static VALUE rhrd_s_parse(int argc, VALUE *argv, VALUE klass) {
1854
1728
 
1855
1729
  switch(argc) {
1856
1730
  case 0:
1857
- rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
1731
+ rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
1858
1732
  d->flags = RHR_HAVE_JD;
1859
1733
  return rd;
1860
1734
  case 1:
@@ -1885,7 +1759,7 @@ static VALUE rhrd_s_strptime(int argc, VALUE *argv, VALUE klass) {
1885
1759
 
1886
1760
  switch(argc) {
1887
1761
  case 0:
1888
- rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
1762
+ rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
1889
1763
  d->flags = RHR_HAVE_JD;
1890
1764
  return rd;
1891
1765
  case 1:
@@ -1914,7 +1788,7 @@ static VALUE rhrd_s_strptime(int argc, VALUE *argv, VALUE klass) {
1914
1788
  */
1915
1789
  static VALUE rhrd_s_today(int argc, VALUE *argv, VALUE klass) {
1916
1790
  rhrd_t *d;
1917
- VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
1791
+ VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
1918
1792
 
1919
1793
  switch(argc) {
1920
1794
  case 0:
@@ -1948,7 +1822,7 @@ static VALUE rhrd_s_valid_civil_q(int argc, VALUE *argv, VALUE klass) {
1948
1822
  switch(argc) {
1949
1823
  case 3:
1950
1824
  case 4:
1951
- if (!rhrd__valid_civil(&d, NUM2LONG(argv[0]), NUM2LONG(argv[1]), NUM2LONG(argv[2]))) {
1825
+ if (!rhrd__valid_civil(&d, NUM2LONG(argv[0]), NUM2LONG(argv[1]), NUM2LONG(argv[2]), RHR_NO_RAISE)) {
1952
1826
  #ifdef RUBY19
1953
1827
  return Qfalse;
1954
1828
  #else
@@ -1988,7 +1862,7 @@ static VALUE rhrd_s_valid_commercial_q(int argc, VALUE *argv, VALUE klass) {
1988
1862
  switch(argc) {
1989
1863
  case 3:
1990
1864
  case 4:
1991
- if (!rhrd__valid_commercial(&d, NUM2LONG(argv[0]), NUM2LONG(argv[1]), NUM2LONG(argv[2]))) {
1865
+ if (!rhrd__valid_commercial(&d, NUM2LONG(argv[0]), NUM2LONG(argv[1]), NUM2LONG(argv[2]), RHR_NO_RAISE)) {
1992
1866
  #ifdef RUBY19
1993
1867
  return Qfalse;
1994
1868
  #else
@@ -2052,7 +1926,7 @@ static VALUE rhrd_s_valid_ordinal_q(int argc, VALUE *argv, VALUE klass) {
2052
1926
  switch(argc) {
2053
1927
  case 2:
2054
1928
  case 3:
2055
- if (!rhrd__valid_ordinal(&d, NUM2LONG(argv[0]), NUM2LONG(argv[1]))) {
1929
+ if (!rhrd__valid_ordinal(&d, NUM2LONG(argv[0]), NUM2LONG(argv[1]), RHR_NO_RAISE)) {
2056
1930
  #ifdef RUBY19
2057
1931
  return Qfalse;
2058
1932
  #else
@@ -2087,7 +1961,7 @@ static VALUE rhrd_s_valid_ordinal_q(int argc, VALUE *argv, VALUE klass) {
2087
1961
  *
2088
1962
  * On ruby 1.9, this method is private.
2089
1963
  */
2090
- static VALUE rhrd_s_zone_to_diff(VALUE klass, VALUE str) {
1964
+ VALUE rhrd_s_zone_to_diff(VALUE klass, VALUE str) {
2091
1965
  long offset = 0;
2092
1966
  long len, i, j;
2093
1967
  char *s;
@@ -2125,8 +1999,8 @@ static VALUE rhrd_s_zone_to_diff(VALUE klass, VALUE str) {
2125
1999
  if((s[i] == ',') || (s[i] == '.')) {
2126
2000
  v = rb_funcall(str, rhrd_id_split, 1, rhrd_re_comma_period);
2127
2001
  e = rb_ary_entry(v, 1);
2128
- return LONG2NUM((NUM2LONG(rb_funcall(rb_ary_entry(v, 0), rhrd_id_to_i, 0)) * RHR_SECONDS_PER_HOUR)
2129
- + (NUM2LONG(rb_funcall(e, rhrd_id_to_i, 0)) * RHR_SECONDS_PER_HOUR) / (long)pow(10, RSTRING_LEN(rb_str_to_str(e))) * offset);
2002
+ return LONG2NUM(((NUM2LONG(rb_funcall(rb_ary_entry(v, 0), rhrd_id_to_i, 0)) * RHR_SECONDS_PER_HOUR)
2003
+ + ((NUM2LONG(rb_funcall(e, rhrd_id_to_i, 0)) * RHR_SECONDS_PER_HOUR) / (long)pow(10, RSTRING_LEN(rb_str_to_str(e))))) * offset);
2130
2004
  }
2131
2005
  }
2132
2006
  switch (len) {
@@ -2574,24 +2448,33 @@ static VALUE rhrd_step(int argc, VALUE *argv, VALUE self) {
2574
2448
  rhrd_t *d, *n, *newd;
2575
2449
  rhrdt_t *ndt;
2576
2450
  long step, limit, current;
2577
- VALUE rlimit, new;
2451
+ VALUE rlimit, new, rstep;
2578
2452
  Data_Get_Struct(self, rhrd_t, d);
2579
2453
  RHR_FILL_JD(d)
2580
2454
 
2581
- rb_need_block();
2582
2455
  switch(argc) {
2583
2456
  case 1:
2584
2457
  step = 1;
2458
+ rstep = LONG2NUM(step);
2585
2459
  break;
2586
2460
  case 2:
2587
- step = NUM2LONG(argv[1]);
2461
+ rstep = argv[1];
2462
+ step = NUM2LONG(rstep);
2588
2463
  break;
2589
2464
  default:
2590
2465
  rb_raise(rb_eArgError, "wrong number of arguments: %i for 2", argc);
2591
2466
  break;
2592
2467
  }
2593
-
2594
2468
  rlimit = argv[0];
2469
+
2470
+ #ifdef RUBY19
2471
+ if (!rb_block_given_p()) {
2472
+ return rb_funcall(self, rhrd_id_to_enum, 3, rhrd_sym_step, rlimit, rstep);
2473
+ }
2474
+ #else
2475
+ rb_need_block();
2476
+ #endif
2477
+
2595
2478
  if (RTEST(rb_obj_is_kind_of(rlimit, rb_cNumeric))) {
2596
2479
  limit = NUM2LONG(rlimit);
2597
2480
  } else if (RTEST((rb_obj_is_kind_of(rlimit, rhrdt_class)))) {
@@ -2610,7 +2493,7 @@ static VALUE rhrd_step(int argc, VALUE *argv, VALUE self) {
2610
2493
  if (limit > current) {
2611
2494
  if (step > 0) {
2612
2495
  while(limit >= current) {
2613
- new = Data_Make_Struct(rhrd_class, rhrd_t, NULL, free, newd);
2496
+ new = Data_Make_Struct(rhrd_class, rhrd_t, NULL, -1, newd);
2614
2497
  newd->jd = current;
2615
2498
  RHR_CHECK_JD(newd)
2616
2499
  newd->flags = RHR_HAVE_JD;
@@ -2621,7 +2504,7 @@ static VALUE rhrd_step(int argc, VALUE *argv, VALUE self) {
2621
2504
  } else if (limit < current) {
2622
2505
  if (step < 0) {
2623
2506
  while(limit <= current) {
2624
- new = Data_Make_Struct(rhrd_class, rhrd_t, NULL, free, newd);
2507
+ new = Data_Make_Struct(rhrd_class, rhrd_t, NULL, -1, newd);
2625
2508
  newd->jd = current;
2626
2509
  RHR_CHECK_JD(newd)
2627
2510
  newd->flags = RHR_HAVE_JD;
@@ -3006,7 +2889,7 @@ VALUE rhrd__add_years(VALUE self, long n) {
3006
2889
  VALUE new;
3007
2890
  Data_Get_Struct(self, rhrd_t, d);
3008
2891
 
3009
- new = Data_Make_Struct(rhrd_class, rhrd_t, NULL, free, newd);
2892
+ new = Data_Make_Struct(rhrd_class, rhrd_t, NULL, -1, newd);
3010
2893
  RHR_FILL_CIVIL(d)
3011
2894
  newd->year = rhrd__safe_add_long(n, d->year);
3012
2895
  newd->month = d->month;
@@ -3054,7 +2937,7 @@ static VALUE rhrd_s_httpdate(int argc, VALUE *argv, VALUE klass) {
3054
2937
 
3055
2938
  switch(argc) {
3056
2939
  case 0:
3057
- rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
2940
+ rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
3058
2941
  d->flags = RHR_HAVE_JD;
3059
2942
  return rd;
3060
2943
  case 1:
@@ -3088,7 +2971,7 @@ static VALUE rhrd_s_iso8601(int argc, VALUE *argv, VALUE klass) {
3088
2971
 
3089
2972
  switch(argc) {
3090
2973
  case 0:
3091
- rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
2974
+ rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
3092
2975
  d->flags = RHR_HAVE_JD;
3093
2976
  return rd;
3094
2977
  case 1:
@@ -3122,7 +3005,7 @@ static VALUE rhrd_s_jisx0301(int argc, VALUE *argv, VALUE klass) {
3122
3005
 
3123
3006
  switch(argc) {
3124
3007
  case 0:
3125
- rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
3008
+ rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
3126
3009
  d->flags = RHR_HAVE_JD;
3127
3010
  return rd;
3128
3011
  case 1:
@@ -3156,7 +3039,7 @@ static VALUE rhrd_s_rfc2822(int argc, VALUE *argv, VALUE klass) {
3156
3039
 
3157
3040
  switch(argc) {
3158
3041
  case 0:
3159
- rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
3042
+ rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
3160
3043
  d->flags = RHR_HAVE_JD;
3161
3044
  return rd;
3162
3045
  case 1:
@@ -3190,7 +3073,7 @@ static VALUE rhrd_s_rfc3339(int argc, VALUE *argv, VALUE klass) {
3190
3073
 
3191
3074
  switch(argc) {
3192
3075
  case 0:
3193
- rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
3076
+ rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
3194
3077
  d->flags = RHR_HAVE_JD;
3195
3078
  return rd;
3196
3079
  case 1:
@@ -3224,7 +3107,7 @@ static VALUE rhrd_s_xmlschema(int argc, VALUE *argv, VALUE klass) {
3224
3107
 
3225
3108
  switch(argc) {
3226
3109
  case 0:
3227
- rd = Data_Make_Struct(klass, rhrd_t, NULL, free, d);
3110
+ rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
3228
3111
  d->flags = RHR_HAVE_JD;
3229
3112
  return rd;
3230
3113
  case 1:
@@ -3560,7 +3443,7 @@ static VALUE rhrd_rfc3339(VALUE self) {
3560
3443
  static VALUE rhrd_to_datetime(VALUE self) {
3561
3444
  rhrd_t *d;
3562
3445
  rhrdt_t *dt;
3563
- VALUE rdt = Data_Make_Struct(rhrdt_class, rhrdt_t, NULL, free, dt);
3446
+ VALUE rdt = Data_Make_Struct(rhrdt_class, rhrdt_t, NULL, -1, dt);
3564
3447
  Data_Get_Struct(self, rhrd_t, d);
3565
3448
 
3566
3449
  if (RHR_HAS_CIVIL(d)) {
@@ -3607,7 +3490,7 @@ static VALUE rhrd_to_time(VALUE self) {
3607
3490
  static VALUE rhrd_time_to_date(VALUE self) {
3608
3491
  rhrd_t *d;
3609
3492
  VALUE rd;
3610
- rd = Data_Make_Struct(rhrd_class, rhrd_t, NULL, free, d);
3493
+ rd = Data_Make_Struct(rhrd_class, rhrd_t, NULL, -1, d);
3611
3494
  d->jd = rhrd__unix_to_jd(NUM2LONG(rb_funcall(self, rhrd_id_to_i, 0)) + NUM2LONG(rb_funcall(self, rhrd_id_utc_offset, 0)));
3612
3495
  d->flags = RHR_HAVE_JD;
3613
3496
  RHR_CHECK_JD(d)
@@ -4059,8 +3942,6 @@ static VALUE rhrd_s_valid_time_q(VALUE klass, VALUE rh, VALUE rm, VALUE rs) {
4059
3942
 
4060
3943
  #endif
4061
3944
 
4062
- #include "datetime.c"
4063
-
4064
3945
  /* Ruby Library Initialization */
4065
3946
 
4066
3947
  /* +Date+ is used to store a single date in the gregorian calendar.
@@ -4096,6 +3977,7 @@ void Init_date_ext(void) {
4096
3977
  rhrd_id_slice = rb_intern("slice");
4097
3978
  rhrd_id_split = rb_intern("split");
4098
3979
  rhrd_id_sub_b = rb_intern("sub!");
3980
+ rhrd_id_to_enum = rb_intern("to_enum");
4099
3981
  rhrd_id_to_i = rb_intern("to_i");
4100
3982
  #ifdef RUBY19
4101
3983
  rhrd_id_nsec = rb_intern("nsec");
@@ -4128,6 +4010,7 @@ void Init_date_ext(void) {
4128
4010
  rhrd_sym_sec = ID2SYM(rb_intern("sec"));
4129
4011
  rhrd_sym_sec_fraction = ID2SYM(rb_intern("sec_fraction"));
4130
4012
  rhrd_sym_seconds = ID2SYM(rb_intern("seconds"));
4013
+ rhrd_sym_step = ID2SYM(rb_intern("step"));
4131
4014
  rhrd_sym_wday = ID2SYM(rb_intern("wday"));
4132
4015
  rhrd_sym_wnum0 = ID2SYM(rb_intern("wnum0"));
4133
4016
  rhrd_sym_wnum1 = ID2SYM(rb_intern("wnum1"));
@@ -4135,6 +4018,10 @@ void Init_date_ext(void) {
4135
4018
  rhrd_sym_year = ID2SYM(rb_intern("year"));
4136
4019
  rhrd_sym_zone = ID2SYM(rb_intern("zone"));
4137
4020
 
4021
+ #ifdef RHR_ENCODING
4022
+ rhrd_encoding_index = rb_enc_to_index(rb_usascii_encoding());
4023
+ #endif
4024
+
4138
4025
  /* Define classes*/
4139
4026
 
4140
4027
  rhrd_class = rb_define_class("Date", rb_cObject);