#include <time.h>
`}
-redef class Object
- # Unix time: the number of seconds elapsed since January 1, 1970
- protected fun get_time: Int `{ return time(NULL); `}
-end
+# Unix time: the number of seconds elapsed since January 1, 1970
+fun get_time: Int `{ return time(NULL); `}
redef class Sys
# Wait a specific number of second and nanoseconds
`}
end
+redef class Float
+ # Sleep approximately `self` seconds
+ fun sleep `{
+ time_t s = self;
+ long ns = (self-s) * 1000000000.0;
+ const struct timespec req = {s, ns};
+ nanosleep(&req, NULL);
+ `}
+end
+
# Time since epoch
extern class TimeT `{time_t`}
fun update `{ time(&self); `}
# Convert `self` to a human readable String.
- fun ctime: String import NativeString.to_s_with_copy `{
- return NativeString_to_s_with_copy( ctime(&self) );
+ fun ctime: String import CString.to_s_with_copy `{
+ return CString_to_s_with_copy( ctime(&self) );
`}
# Difference in secondes from start (self if the end time)
fun is_dst: Bool `{ return self->tm_isdst; `}
# Convert `self` to a human readable String.
- fun asctime: String import NativeString.to_s_with_copy `{
- return NativeString_to_s_with_copy( asctime(self) );
- `}
+ private fun asctime: CString `{ return asctime(self); `}
# Convert `self` to a human readable String corresponding to `format`.
# TODO document allowed format.
- fun strftime(format: String): String import String.to_cstring, NativeString.to_s `{
+ fun strftime(format: String): String import String.to_cstring, CString.to_s_with_copy `{
char* buf, *c_format;
buf = (char*)malloc(100);
c_format = String_to_cstring(format);
strftime(buf, 100, c_format, self);
- String s = NativeString_to_s_with_copy(buf);
+ String s = CString_to_s_with_copy(buf);
free(buf);
return s;
`}
- redef fun to_s do return asctime.replace("\n", "")
+ redef fun to_s do return asctime.to_s_with_copy.replace("\n", "")
end
# Date using the international format defined by ISO 8601.