Property definitions

realtime $ Timespec :: defaultinit
# 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