[ruby-dev:49334] [Ruby trunk - Feature #11558] Time related C APIs

From: naruse@...
Date: 2015-11-08 06:44:20 UTC
List: ruby-dev #49334
Issue #11558 has been updated by Yui NARUSE.


=E3=81=A8=E3=82=8A=E3=81=82=E3=81=88=E3=81=9A=E7=89=B9=E3=81=AB=E7=95=B0=E8=
=AB=96=E3=81=AE=E7=84=A1=E3=81=8B=E3=81=A3=E3=81=9F=E4=B8=8B=E8=A8=98=E3=82=
=92=E5=85=A5=E3=82=8C=E3=82=88=E3=81=86=E3=81=8B=E3=81=A8=E6=80=9D=E3=81=84=
=E3=81=BE=E3=81=99=E3=80=82

```diff
diff --git a/include/ruby/intern.h b/include/ruby/intern.h
index af6b75d..3fb1637 100644
--- a/include/ruby/intern.h
+++ b/include/ruby/intern.h
@@ -919,8 +919,10 @@ VALUE rb_mutex_unlock(VALUE mutex);
 VALUE rb_mutex_sleep(VALUE self, VALUE timeout);
 VALUE rb_mutex_synchronize(VALUE mutex, VALUE (*func)(VALUE arg), VALUE ar=
g);
 /* time.c */
+void rb_timespec_now(struct timespec *);
 VALUE rb_time_new(time_t, long);
 VALUE rb_time_nano_new(time_t, long);
+VALUE rb_time_timespec_new(const struct timespec *, int);
 VALUE rb_time_num_new(VALUE, VALUE);
 struct timeval rb_time_interval(VALUE num);
 struct timeval rb_time_timeval(VALUE time);
diff --git a/time.c b/time.c
index 11c76a5..da8cf25 100644
--- a/time.c
+++ b/time.c
@@ -1892,6 +1892,25 @@ timew2timespec_exact(wideval_t timew, struct timespe=
c *ts)
     return ts;
 }
=20
+void
+rb_timespec_now(struct timespec *ts)
+{
+#ifdef HAVE_CLOCK_GETTIME
+    if (clock_gettime(CLOCK_REALTIME, ts) =3D=3D -1) {
+	rb_sys_fail("clock_gettime");
+    }
+#else
+    {
+	struct timeval tv;
+	if (gettimeofday(&tv, 0) < 0) {
+	    rb_sys_fail("gettimeofday");
+	}
+	ts->tv_sec =3D tv.tv_sec;
+	ts->tv_nsec =3D tv.tv_usec * 1000;
+    }
+#endif
+}
+
 static VALUE
 time_init_0(VALUE time)
 {
@@ -1903,20 +1922,7 @@ time_init_0(VALUE time)
     tobj->gmt =3D 0;
     tobj->tm_got=3D0;
     tobj->timew =3D WINT2FIXWV(0);
-#ifdef HAVE_CLOCK_GETTIME
-    if (clock_gettime(CLOCK_REALTIME, &ts) =3D=3D -1) {
-	rb_sys_fail("clock_gettime");
-    }
-#else
-    {
-        struct timeval tv;
-        if (gettimeofday(&tv, 0) < 0) {
-            rb_sys_fail("gettimeofday");
-        }
-        ts.tv_sec =3D tv.tv_sec;
-        ts.tv_nsec =3D tv.tv_usec * 1000;
-    }
-#endif
+    rb_timespec_now(&ts);
     tobj->timew =3D timespec2timew(&ts);
=20
     return time;
@@ -2306,6 +2312,23 @@ rb_time_nano_new(time_t sec, long nsec)
 }
=20
 VALUE
+rb_time_timespec_new(const struct timespec *ts, int offset)
+{
+    VALUE time =3D time_new_timew(rb_cTime, nsec2timew(ts->tv_sec, ts->tv_=
nsec));
+    if (offset) {
+	struct time_object *tobj;
+	if (offset < -86400 || 86400 < offset)
+	    rb_raise(rb_eArgError, "utc_offset out of range");
+	GetTimeval(time, tobj);
+	tobj->tm_got =3D 0;
+	tobj->gmt =3D 2;
+	tobj->vtm.utc_offset =3D INT2FIX(offset);
+	tobj->vtm.zone =3D NULL;
+    }
+    return time;
+}
+
+VALUE
 rb_time_num_new(VALUE timev, VALUE off)
 {
     VALUE time =3D time_new_timew(rb_cTime, rb_time_magnify(v2w(timev)));
```

----------------------------------------
Feature #11558: Time related C APIs
https://bugs.ruby-lang.org/issues/11558#change-54755

* Author: Yui NARUSE
* Status: Open
* Priority: Normal
* Assignee:=20
----------------------------------------
Time=E9=96=A2=E9=80=A3=E3=81=AEC API=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=
=81=A6=E6=AC=B2=E3=81=97=E3=81=84=E3=81=A7=E3=81=99=E3=80=82
=E5=85=B7=E4=BD=93=E7=9A=84=E3=81=AB=E3=81=AF=E4=BB=A5=E4=B8=8B=E3=81=AE=E3=
=82=88=E3=81=86=E3=81=AA=E3=82=82=E3=81=AE=E3=81=8C=E6=AC=B2=E3=81=97=E3=81=
=84=E3=81=A7=E3=81=99=E3=80=82

struct timespec=E3=81=A8offset=E3=82=92=E5=8F=96=E3=81=A3=E3=81=A6=E3=80=81=
Time=E3=82=92=E8=BF=94=E3=81=97=E3=81=A6=E3=81=8F=E3=81=A0=E3=81=95=E3=81=
=84=E3=80=82
VALUE rb_time_timespec_new(const struct timespec *ts, int offset);
=E8=B6=A3=E6=97=A8=E3=81=A8=E3=81=97=E3=81=A6=E3=81=AF=E3=80=81[=E7=A7=92, =
=E3=83=8A=E3=83=8E=E7=A7=92, offset]=E3=81=8B=E3=82=89Time=E3=82=92=E4=BD=
=9C=E3=81=A3=E3=81=A6=E6=AC=B2=E3=81=97=E3=81=84=E3=81=A8=E8=A8=80=E3=81=86=
=E3=81=93=E3=81=A8=E3=81=A7=E3=81=99=E3=80=82
=EF=BC=88rb_time_num_new(Rational, offset)=E3=81=A8=E3=81=8B=E3=81=A0=E3=81=
=A8=E6=89=8B=E9=96=93=E3=81=8B=E3=81=8B=E3=82=8B=E4=B8=8A=E3=81=AB=E9=81=85=
=E3=81=84=EF=BC=89

=E7=8F=BE=E5=9C=A8=E6=99=82=E5=88=BB=E3=82=92struct timespec=E3=81=A7=E6=AC=
=B2=E3=81=97=E3=81=84=E3=81=A7=E3=81=99=E3=80=82
void timespec_now(struct timespec *ts);


=E6=97=A2=E5=AD=98=E3=81=AE=E9=9D=9E=E5=85=AC=E9=96=8BAPI=E3=82=92=E5=85=AC=
=E9=96=8B=E3=81=97=E3=81=A6=E3=81=8F=E3=81=A0=E3=81=95=E3=81=84=E3=80=82
struct tm * localtime_with_gmtoff_zone(const time_t *t, struct tm *result, =
long *gmtoff, const char **zone);
time_t timegm_noleapsecond(struct tm *tm);
void tm_add_offset(struct tm *tm, long diff);


https://github.com/nurse/strptime/blob/v0.1.1/ext/strptime/ruby_time.c



--=20
https://bugs.ruby-lang.org/

In This Thread

Prev Next