summaryrefslogtreecommitdiff
path: root/time.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-04-25 07:20:20 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-04-25 07:20:20 +0000
commit0dae2c910fa646a2b41d33f0b4d1dad818eb00da (patch)
tree15e64b5860bb78623ca3f0d8fa180a0bb64fc5a1 /time.c
parent939383275c555c1a23cee3d41676aee90e843b92 (diff)
* time.c (month_arg): extracted from time_arg.
(validate_vtm): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23276 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'time.c')
-rw-r--r--time.c74
1 files changed, 44 insertions, 30 deletions
diff --git a/time.c b/time.c
index 2b5606f61a..61caf0af35 100644
--- a/time.c
+++ b/time.c
@@ -1422,6 +1422,48 @@ usec2subsec(VALUE obj)
return quo(num_exact(obj), INT2FIX(1000000));
}
+static int
+month_arg(VALUE arg)
+{
+ int i, mon;
+
+ VALUE s = rb_check_string_type(arg);
+ if (!NIL_P(s)) {
+ mon = 0;
+ for (i=0; i<12; i++) {
+ if (RSTRING_LEN(s) == 3 &&
+ STRCASECMP(months[i], RSTRING_PTR(s)) == 0) {
+ mon = i+1;
+ break;
+ }
+ }
+ if (mon == 0) {
+ char c = RSTRING_PTR(s)[0];
+
+ if ('0' <= c && c <= '9') {
+ mon = obj2long(s);
+ }
+ }
+ }
+ else {
+ mon = obj2long(arg);
+ }
+ return mon;
+}
+
+static void
+validate_vtm(struct vtm *vtm)
+{
+ if ( vtm->mon < 1 || vtm->mon > 12
+ || vtm->mday < 1 || vtm->mday > 31
+ || vtm->hour < 0 || vtm->hour > 24
+ || (vtm->hour == 24 && (vtm->min > 0 || vtm->sec > 0))
+ || vtm->min < 0 || vtm->min > 59
+ || vtm->sec < 0 || vtm->sec > 60
+ || lt(vtm->subsec, INT2FIX(0)) || ge(vtm->subsec, INT2FIX(1)))
+ rb_raise(rb_eArgError, "argument out of range");
+}
+
static void
time_arg(int argc, VALUE *argv, struct vtm *vtm)
{
@@ -1465,27 +1507,7 @@ time_arg(int argc, VALUE *argv, struct vtm *vtm)
vtm->mon = 1;
}
else {
- VALUE s = rb_check_string_type(v[1]);
- if (!NIL_P(s)) {
- vtm->mon = 0;
- for (i=0; i<12; i++) {
- if (RSTRING_LEN(s) == 3 &&
- STRCASECMP(months[i], RSTRING_PTR(s)) == 0) {
- vtm->mon = i+1;
- break;
- }
- }
- if (vtm->mon == 0) {
- char c = RSTRING_PTR(s)[0];
-
- if ('0' <= c && c <= '9') {
- vtm->mon = obj2long(s);
- }
- }
- }
- else {
- vtm->mon = obj2long(v[1]);
- }
+ vtm->mon = month_arg(v[1]);
}
if (NIL_P(v[2])) {
@@ -1508,15 +1530,7 @@ time_arg(int argc, VALUE *argv, struct vtm *vtm)
vtm->sec = NIL_P(v[5])?0:obj2subsec(v[5], &vtm->subsec);
}
- /* value validation */
- if ( vtm->mon < 1 || vtm->mon > 12
- || vtm->mday < 1 || vtm->mday > 31
- || vtm->hour < 0 || vtm->hour > 24
- || (vtm->hour == 24 && (vtm->min > 0 || vtm->sec > 0))
- || vtm->min < 0 || vtm->min > 59
- || vtm->sec < 0 || vtm->sec > 60
- || lt(vtm->subsec, INT2FIX(0)) || ge(vtm->subsec, INT2FIX(1)))
- rb_raise(rb_eArgError, "argument out of range");
+ validate_vtm(vtm);
}
static int