realtime :: Timespec :: defaultinit
realtime :: Timespec :: monotonic_now
Init a new Timespec from now.core :: Pointer :: address_is_null
Is the address behind this Object at NULL?core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Pointer :: defaultinit
core :: Object :: defaultinit
realtime :: Timespec :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
realtime :: Timespec :: monotonic_now
Init a new Timespec from now.core :: Object :: native_class_name
The class name of the object in CString format.core :: Pointer :: native_equals
core :: Object :: output_class_name
Display class name on stdout (debug only).core :: Pointer :: premultiply_alpha
Multiply RGB values by their alpha value
# Elapsed time representation.
private extern class Timespec `{struct timespec*`}
# Init a new Timespec from `s` seconds and `ns` nanoseconds.
new ( s, ns : Int ) `{
struct timespec* tv = malloc( sizeof(struct timespec) );
tv->tv_sec = s; tv->tv_nsec = ns;
return tv;
`}
# Init a new Timespec from now.
new monotonic_now `{
struct timespec* tv = malloc( sizeof(struct timespec) );
nit_clock_gettime( CLOCK_MONOTONIC, tv );
return tv;
`}
# Init a new Timespec copied from another.
new copy_of( other : Timespec ) `{
struct timespec* tv = malloc( sizeof(struct timespec) );
tv->tv_sec = other->tv_sec;
tv->tv_nsec = other->tv_nsec;
return tv;
`}
# Update `self` clock.
fun update `{
nit_clock_gettime(CLOCK_MONOTONIC, self);
`}
# Subtract `other` from `self`
fun -(other: Timespec): Timespec `{
time_t s = self->tv_sec - other->tv_sec;
long ns = self->tv_nsec - other->tv_nsec;
if (ns < 0) {
s -= 1;
ns += 1000000000l;
}
struct timespec* tv = malloc(sizeof(struct timespec));
tv->tv_sec = s; tv->tv_nsec = ns;
return tv;
`}
# Set `self` to `a` - `b`
fun minus(a, b: Timespec) `{
time_t s = a->tv_sec - b->tv_sec;
long ns = a->tv_nsec - b->tv_nsec;
if (ns < 0) {
s -= 1;
ns += 1000000000l;
}
self->tv_sec = s;
self->tv_nsec = ns;
`}
# Number of whole seconds of elapsed time.
fun sec : Int `{
return self->tv_sec;
`}
# Rest of the elapsed time (a fraction of a second).
#
# Number of nanoseconds.
fun nanosec : Int `{
return self->tv_nsec;
`}
# Elapsed time in microseconds, with both whole seconds and the rest
#
# May cause an `Int` overflow, use only with a low number of seconds.
fun microsec: Int `{
return self->tv_sec*1000000 + self->tv_nsec/1000;
`}
# Elapsed time in milliseconds, with both whole seconds and the rest
#
# May cause an `Int` overflow, use only with a low number of seconds.
fun millisec: Int `{
return self->tv_sec*1000 + self->tv_nsec/1000000;
`}
# Number of seconds as a `Float`
#
# Incurs a loss of precision, but the result is pretty to print.
fun to_f: Float `{
return (double)self->tv_sec + 0.000000001 * self->tv_nsec;
`}
redef fun to_s do return "{to_f}s"
end
lib/realtime/realtime.nit:58,1--147,3