Permalink
Browse files

added working module with getUnixTime getUnixTime getMilliseconds met…

…hods
  • Loading branch information...
0 parents commit 3312dd8cfa2264c548d731a7f34ad958d0fdf003 @pkrumins committed Jul 3, 2010
Showing with 115 additions and 0 deletions.
  1. +5 −0 examples/millisecs.js
  2. +8 −0 examples/timeval.js
  3. +5 −0 examples/unix_time.js
  4. +45 −0 readme.txt
  5. +52 −0 time.cc
@@ -0,0 +1,5 @@
+var sys = require('sys');
+var getMilliseconds = require('time').getMilliseconds;
+
+sys.log("Milliseconds: " + getMilliseconds());
+
@@ -0,0 +1,8 @@
+var sys = require('sys');
+var getTimeval = require('time').getTimeval;
+
+tv = getTimeval();
+
+sys.log("tv.sec: " + tv.sec);
+sys.log("tv.usec: " + tv.usec);
+
@@ -0,0 +1,5 @@
+var sys = require('sys');
+var getUnixTime = require('time').getUnixTime;
+
+sys.log("Unix time: " + (getUnixTime()).toString());
+
@@ -0,0 +1,45 @@
+This is a node.js module, writen in C++, that implements some missing time
+functions, such as getting timeval struct and getting unix time.
+
+It was written by Peteris Krumins (peter@catonmat.net).
+His blog is at http://www.catonmat.net -- good coders code, great reuse.
+
+------------------------------------------------------------------------------
+
+The module exports three functions:
+
+ * getUnixTime - returns seconds since 1970, Jan 1 (+/- timezone offset)
+ * getTimeval - returns timeval struct as object with .sec and .usec props
+ * getMilliseconds - returns milliseconds since 1970, Jan 1 (+/- tz offset)
+
+See examples/ dir for working examples.
+
+
+How to compile?
+---------------
+
+As usual,
+
+ node-waf configure build
+
+The module will be called `time.node`. To use it, make sure it's in NODE_PATH.
+
+
+Extra info
+----------
+
+This library was written for my and SubStack's StackVM startup:
+
+ http://github.com/pkrumins/stackvm
+ http://github.com/substack/stackvm
+
+
+------------------------------------------------------------------------------
+
+Have fun with time!
+
+
+Sincerely,
+Peteris Krumins
+http://www.catonmat.net
+
52 time.cc
@@ -0,0 +1,52 @@
+#include <v8.h>
+#include <node.h>
+#include <time.h>
+#include <sys/time.h>
+
+using namespace v8;
+using namespace node;
+
+Handle<Value>
+getUnixTime(const Arguments &args)
+{
+ HandleScope scope;
+
+ Local<Integer> unixTime = Integer::New(time(NULL));
+ return scope.Close(unixTime);
+}
+
+Handle<Value>
+getTimeval(const Arguments &args)
+{
+ HandleScope scope;
+
+ struct timeval t;
+ gettimeofday(&t, NULL);
+
+ Local<Object> ret = Object::New();
+ ret->Set(String::NewSymbol("sec"), Integer::New(t.tv_sec));
+ ret->Set(String::NewSymbol("usec"), Integer::New(t.tv_usec));
+
+ return scope.Close(ret);
+}
+
+Handle<Value>
+getMilliseconds(const Arguments &args)
+{
+ HandleScope scope;
+
+ struct timeval t;
+ gettimeofday(&t, NULL);
+
+ Local<Number> mil = Number::New((double)t.tv_sec*1000 + t.tv_usec/1000);
+ return scope.Close(mil);
+}
+
+extern "C" void init (Handle<Object> target)
+{
+ HandleScope scope;
+ target->Set(String::New("getUnixTime"), FunctionTemplate::New(getUnixTime)->GetFunction());
+ target->Set(String::New("getTimeval"), FunctionTemplate::New(getTimeval)->GetFunction());
+ target->Set(String::New("getMilliseconds"), FunctionTemplate::New(getMilliseconds)->GetFunction());
+}
+

0 comments on commit 3312dd8

Please sign in to comment.