summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--time.c74
2 files changed, 49 insertions, 30 deletions
diff --git a/ChangeLog b/ChangeLog
index 14e277b808..ebf58e9919 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sat Apr 25 16:19:48 2009 Tanaka Akira <akr@fsij.org>
+
+ * time.c (month_arg): extracted from time_arg.
+ (validate_vtm): ditto.
+
Sat Apr 25 16:03:21 2009 Tanaka Akira <akr@fsij.org>
* time.c (TIME_COPY_GMT): new macro.
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