forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.cpp
47 lines (37 loc) · 1.36 KB
/
time.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <math.h>
#include "builtin/object.hpp"
#include "builtin/time.hpp"
#include "capi/capi.hpp"
#include "capi/18/include/ruby.h"
using namespace rubinius;
using namespace rubinius::capi;
extern "C" {
VALUE rb_time_new(time_t sec, long usec) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Class* cls = env->state()->vm()->shared.globals.time_class.get();
// Prevent overflow before multiplying.
if(usec >= 1000000) {
sec += usec / 1000000;
usec %= 1000000;
}
Time* obj = Time::specific(env->state(), cls,
Integer::from(env->state(), sec),
Integer::from(env->state(), usec * 1000),
cFalse, cNil);
return env->get_handle(obj);
}
VALUE rb_time_nano_new(time_t sec, long nsec) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Class* cls = env->state()->vm()->shared.globals.time_class.get();
// Prevent overflow before multiplying.
if(nsec >= 1000000000) {
sec += nsec / 1000000000;
nsec %= 1000000000;
}
Time* obj = Time::specific(env->state(), cls,
Integer::from(env->state(), sec),
Integer::from(env->state(), nsec),
cFalse, cNil);
return env->get_handle(obj);
}
}