win32ole_gettimeofday.cc
2.3 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
win32ole_gettimeofday.cc
*/
#include "node_win32ole.h"
#include "ole32core.h"
#ifdef _WIN32
#include <sys/timeb.h>
#endif
using namespace v8;
using namespace ole32core;
namespace node_win32ole {
void timeval_gettimeofday(struct timeval *ptv)
{
#ifdef _WIN32
_timeb btv;
_ftime(&btv);
ptv->tv_sec = btv.time; // unsafe convert __time64_t to long
ptv->tv_usec = 1000LL * btv.millitm;
#else
gettimeofday(ptv, NULL); // no &tz
#endif
}
Handle<Value> Method_gettimeofday(const Arguments& args)
{
HandleScope scope;
boolean result = false;
BEVERIFY(done, args.Length() >= 2);
struct timeval tv;
timeval_gettimeofday(&tv);
BEVERIFY(done, args[0]->IsObject());
v8::Handle<v8::Object> buf = args[0]->ToObject();
BEVERIFY(done, node::Buffer::Length(buf) == sizeof(struct timeval));
memcpy(node::Buffer::Data(buf), &tv, sizeof(struct timeval));
result = true;
done:
return scope.Close(Boolean::New(result));
}
Handle<Value> Method_sleep(const Arguments& args) // ms, bool: msg, bool: \n
{
HandleScope scope;
boolean result = false;
BEVERIFY(done, args.Length() >= 1);
if(!args[0]->IsInt32())
return ThrowException(Exception::TypeError(
String::New("type of argument 1 must be Int32")));
long ms = args[0]->Int32Value();
bool msg = false;
if(args.Length() >= 2){
if(!args[1]->IsBoolean())
return ThrowException(Exception::TypeError(
String::New("type of argument 2 must be Boolean")));
msg = args[1]->BooleanValue();
}
bool crlf = false;
if(args.Length() >= 3){
if(!args[2]->IsBoolean())
return ThrowException(Exception::TypeError(
String::New("type of argument 3 must be Boolean")));
crlf = args[2]->BooleanValue();
}
if(ms){
if(msg){
printf("waiting %ld milliseconds...", ms);
if(crlf) printf("\n");
}
struct timeval tv_start;
timeval_gettimeofday(&tv_start);
while(true){
struct timeval tv_now;
timeval_gettimeofday(&tv_now);
// it is wrong way (must release CPU)
if((double)tv_now.tv_sec + (double)tv_now.tv_usec / 1000. / 1000.
>= (double)tv_start.tv_sec + (double)tv_start.tv_usec / 1000. / 1000.
+ (double)ms / 1000.) break;
}
}
result = true;
done:
return scope.Close(Boolean::New(result));
}
} // namespace node_win32ole