nitdoc: Add tests with attributes.
[nit.git] / lib / realtime.nit
index cb62251..68b7834 100644 (file)
@@ -11,7 +11,7 @@
 # another product.
 
 # Provides the Clock utility class to keep time of real time flow
-module realtime
+module realtime is c_linker_option("-lrt")
 
 in "C header" `{
 #ifdef _POSIX_C_SOURCE
@@ -21,7 +21,7 @@ in "C header" `{
 #include <time.h>
 `}
 
-extern Timespec `{struct timespec*`}
+extern class Timespec `{struct timespec*`}
        new ( s, ns : Int ) `{
                struct timespec* tv = malloc( sizeof(struct timespec) );
                tv->tv_sec = s; tv->tv_nsec = ns;
@@ -60,21 +60,21 @@ extern Timespec `{struct timespec*`}
        fun destroy `{
                free( recv );
        `}
+
+       # Seconds in Float
+       # Loss of precision but great to print
+       fun to_f: Float do return sec.to_f + nanosec.to_f / 1000000000.0
+
+       redef fun to_s do return "{to_f}s"
 end
 
 # Keeps track of real time
 class Clock
        # Time at instanciation
-       protected var time_at_beginning : Timespec
+       protected var time_at_beginning = new Timespec.monotonic_now
 
        # Time at last time a lapse method was called
-       protected var time_at_last_lapse : Timespec
-
-       init
-       do
-               time_at_beginning = new Timespec.monotonic_now
-               time_at_last_lapse = new Timespec.monotonic_now
-       end
+       protected var time_at_last_lapse = new Timespec.monotonic_now
 
        # Smallest time frame reported by clock
        fun resolution : Timespec `{