[前][次][番号順一覧][スレッド一覧]

ruby-changes:40433

From: naruse <ko1@a...>
Date: Tue, 10 Nov 2015 12:00:32 +0900 (JST)
Subject: [ruby-changes:40433] naruse:r52514 (trunk): * time.c (rb_timespec_now): added.

naruse	2015-11-10 11:59:47 +0900 (Tue, 10 Nov 2015)

  New Revision: 52514

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52514

  Log:
    * time.c (rb_timespec_now): added.
    
    * time.c (rb_time_timespec_new): added.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/include/ruby/intern.h
    trunk/time.c
Index: time.c
===================================================================
--- time.c	(revision 52513)
+++ time.c	(revision 52514)
@@ -1892,19 +1892,11 @@ timew2timespec_exact(wideval_t timew, st https://github.com/ruby/ruby/blob/trunk/time.c#L1892
     return ts;
 }
 
-static VALUE
-time_init_0(VALUE time)
+void
+rb_timespec_now(struct timespec *ts)
 {
-    struct time_object *tobj;
-    struct timespec ts;
-
-    time_modify(time);
-    GetNewTimeval(time, tobj);
-    tobj->gmt = 0;
-    tobj->tm_got=0;
-    tobj->timew = WINT2FIXWV(0);
 #ifdef HAVE_CLOCK_GETTIME
-    if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
+    if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
 	rb_sys_fail("clock_gettime");
     }
 #else
@@ -1913,10 +1905,24 @@ time_init_0(VALUE time) https://github.com/ruby/ruby/blob/trunk/time.c#L1905
         if (gettimeofday(&tv, 0) < 0) {
             rb_sys_fail("gettimeofday");
         }
-        ts.tv_sec = tv.tv_sec;
-        ts.tv_nsec = tv.tv_usec * 1000;
+        ts->tv_sec = tv.tv_sec;
+        ts->tv_nsec = tv.tv_usec * 1000;
     }
 #endif
+}
+
+static VALUE
+time_init_0(VALUE time)
+{
+    struct time_object *tobj;
+    struct timespec ts;
+
+    time_modify(time);
+    GetNewTimeval(time, tobj);
+    tobj->gmt = 0;
+    tobj->tm_got=0;
+    tobj->timew = WINT2FIXWV(0);
+    rb_timespec_now(&ts);
     tobj->timew = timespec2timew(&ts);
 
     return time;
@@ -2299,12 +2305,41 @@ rb_time_new(time_t sec, long usec) https://github.com/ruby/ruby/blob/trunk/time.c#L2305
     return time_new_timew(rb_cTime, timew);
 }
 
+/* returns localtime time object */
 VALUE
 rb_time_nano_new(time_t sec, long nsec)
 {
     return time_new_timew(rb_cTime, nsec2timew(sec, nsec));
 }
 
+/**
+ * Returns a time object with UTC/localtime/fixed offset
+ *
+ * offset is -86400 < fixoff < 86400 or INT_MAX (UTC) or INT_MAX-1 (localtime)
+ */
+VALUE
+rb_time_timespec_new(const struct timespec *ts, int offset)
+{
+    struct time_object *tobj;
+    VALUE time = time_new_timew(rb_cTime, nsec2timew(ts->tv_sec, ts->tv_nsec));
+
+    if (-86400 < offset && offset <  86400) { /* fixoff */
+	GetTimeval(time, tobj);
+	TIME_SET_FIXOFF(tobj, INT2FIX(offset));
+    }
+    else if (offset == INT_MAX) { /* UTC */
+	GetTimeval(time, tobj);
+	TIME_SET_UTC(tobj);
+    }
+    else if (offset == INT_MAX-1) { /* localtime */
+    }
+    else {
+	rb_raise(rb_eArgError, "utc_offset out of range");
+    }
+
+    return time;
+}
+
 VALUE
 rb_time_num_new(VALUE timev, VALUE off)
 {
Index: include/ruby/intern.h
===================================================================
--- include/ruby/intern.h	(revision 52513)
+++ include/ruby/intern.h	(revision 52514)
@@ -919,8 +919,10 @@ VALUE rb_mutex_unlock(VALUE mutex); https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L919
 VALUE rb_mutex_sleep(VALUE self, VALUE timeout);
 VALUE rb_mutex_synchronize(VALUE mutex, VALUE (*func)(VALUE arg), VALUE arg);
 /* 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);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52513)
+++ ChangeLog	(revision 52514)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Nov 10 11:25:29 2015  NARUSE, Yui  <naruse@r...>
+
+	* time.c (rb_timespec_now): added.
+
+	* time.c (rb_time_timespec_new): added.
+
 Tue Nov 10 06:17:17 2015  Eric Wong  <e@8...>
 
 	* variable.c (rb_autoload_load): allow recursive calls
Index: NEWS
===================================================================
--- NEWS	(revision 52513)
+++ NEWS	(revision 52514)
@@ -209,6 +209,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L209
   class is already defined but its superclass does not match the given
   superclass, as well as definitions in ruby level.
 
+* rb_timespec_now() is added to fetch current datetime as struct timespec.
+
+* rb_time_timespec_new() is added to create a time object with epoch,
+  nanosecond, and UTC/localtime/time offset arguments.
+
 === Build system updates
 
 === Implementation changes

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]