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

ruby-changes:10984

From: akr <ko1@a...>
Date: Mon, 23 Feb 2009 20:41:50 +0900 (JST)
Subject: [ruby-changes:10984] Ruby:r22571 (trunk): * ext/socket/ancdata.c (inspect_timespec_as_abstime): new function to

akr	2009-02-23 20:41:38 +0900 (Mon, 23 Feb 2009)

  New Revision: 22571

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

  Log:
    * ext/socket/ancdata.c (inspect_timespec_as_abstime): new function to
      show struct timespec.
      (ancillary_inspect): use it for SCM_TIMESTAMPNS on GNU/Linux.

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/ancdata.c
    trunk/test/socket/test_socket.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 22570)
+++ ChangeLog	(revision 22571)
@@ -1,3 +1,9 @@
+Mon Feb 23 20:39:21 2009  Tanaka Akira  <akr@f...>
+
+	* ext/socket/ancdata.c (inspect_timespec_as_abstime): new function to
+	  show struct timespec.
+	  (ancillary_inspect): use it for SCM_TIMESTAMPNS on GNU/Linux.
+
 Mon Feb 23 20:30:06 2009  Tanaka Akira  <akr@f...>
 
 	* ext/socket/ancdata.c (inspect_bintime_as_abstime): new function to
Index: ext/socket/ancdata.c
===================================================================
--- ext/socket/ancdata.c	(revision 22570)
+++ ext/socket/ancdata.c	(revision 22571)
@@ -700,7 +700,7 @@
 }
 #endif
 
-#if defined(SCM_TIMESTAMP)
+#if defined(SCM_TIMESTAMP) /* GNU/Linux, FreeBSD, NetBSD, OpenBSD, MacOS X, Solaris */
 static int
 inspect_timeval_as_abstime(int level, int optname, VALUE data, VALUE ret)
 {
@@ -722,8 +722,28 @@
 }
 #endif
 
-#if defined(SCM_BINTIME)
+#if defined(SCM_TIMESTAMPNS) /* GNU/Linux */
 static int
+inspect_timespec_as_abstime(int level, int optname, VALUE data, VALUE ret)
+{
+    if (RSTRING_LEN(data) == sizeof(struct timespec)) {
+        struct timespec ts;
+        struct tm tm;
+        char buf[32];
+        memcpy((char*)&ts, RSTRING_PTR(data), sizeof(ts));
+        tm = *localtime(&ts.tv_sec);
+        strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
+        rb_str_catf(ret, " %s.%09ld", buf, (long)ts.tv_nsec);
+        return 1;
+    }
+    else {
+        return 0;
+    }
+}
+#endif
+
+#if defined(SCM_BINTIME) /* FreeBSD */
+static int
 inspect_bintime_as_abstime(int level, int optname, VALUE data, VALUE ret)
 {
     if (RSTRING_LEN(data) == sizeof(struct bintime)) {
@@ -843,6 +863,9 @@
 #            if defined(SCM_TIMESTAMP) /* GNU/Linux, FreeBSD, NetBSD, OpenBSD, MacOS X, Solaris */
               case SCM_TIMESTAMP: inspected = inspect_timeval_as_abstime(level, type, data, ret); break;
 #            endif
+#            if defined(SCM_TIMESTAMPNS) /* GNU/Linux */
+              case SCM_TIMESTAMPNS: inspected = inspect_timespec_as_abstime(level, type, data, ret); break;
+#            endif
 #            if defined(SCM_BINTIME) /* FreeBSD */
               case SCM_BINTIME: inspected = inspect_bintime_as_abstime(level, type, data, ret); break;
 #            endif
Index: test/socket/test_socket.rb
===================================================================
--- test/socket/test_socket.rb	(revision 22570)
+++ test/socket/test_socket.rb	(revision 22571)
@@ -291,6 +291,29 @@
     assert_match(pat, stamp.inspect)
   end
 
+  def test_timestampns
+    return if /linux/ !~ RUBY_PLATFORM || !defined?(Socket::SO_TIMESTAMPNS)
+    t1 = Time.now.strftime("%Y-%m-%d")
+    stamp = nil
+    Addrinfo.udp("127.0.0.1", 0).bind {|s1|
+      Addrinfo.udp("127.0.0.1", 0).bind {|s2|
+        begin
+          s1.setsockopt(:SOCKET, :TIMESTAMPNS, true)
+        rescue Errno::ENOPROTOOPT
+          # SO_TIMESTAMPNS is available since Linux 2.6.22
+          return
+        end
+        s2.send "a", 0, s1.local_address
+        msg, addr, rflags, stamp = s1.recvmsg
+        assert_equal("a", msg)
+        assert(stamp.cmsg_is?(:SOCKET, :TIMESTAMPNS))
+      }
+    }
+    t2 = Time.now.strftime("%Y-%m-%d")
+    pat = Regexp.union([t1, t2].uniq)
+    assert_match(pat, stamp.inspect)
+  end
+
   def test_bintime
     return if /freebsd/ !~ RUBY_PLATFORM
     t1 = Time.now.strftime("%Y-%m-%d")

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

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