Skip to content

Instantly share code, notes, and snippets.

@zimbatm
Created October 5, 2010 08:25
Show Gist options
  • Save zimbatm/611220 to your computer and use it in GitHub Desktop.
Save zimbatm/611220 to your computer and use it in GitHub Desktop.
process.loopTime and loopDate
From cdec69e42bdd698947102ec07a5705d5088d9a92 Mon Sep 17 00:00:00 2001
From: Jonas Pfenniger <jonas@stvs.ch>
Date: Tue, 5 Oct 2010 10:01:35 +0200
Subject: [PATCH 1/2] NEW: process.loopTime
This new variable contains the timestamp at the start of the grand loop.
On first iteration, it will hold the timestamp at boot time.
---
src/node.cc | 12 ++++++++++++
1 files changed, 12 insertions(+), 0 deletions(-)
diff --git a/src/node.cc b/src/node.cc
index 004fa99..cb844d4 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -68,6 +68,8 @@ static Persistent<String> listeners_symbol;
static Persistent<String> uncaught_exception_symbol;
static Persistent<String> emit_symbol;
+static Persistent<String> loop_time_symbol;
+
static int option_end_index = 0;
static bool use_debug_agent = false;
static bool debug_wait_connect = false;
@@ -181,6 +183,11 @@ static void Spin(EV_P_ ev_idle *watcher, int revents) {
}
+static void UpdateLoopTime(void) {
+ HandleScope scope;
+ process->Set(loop_time_symbol, Number::New((uint64_t)(ev_now(EV_DEFAULT_UC) * 1000)));
+}
+
static void Tick(void) {
// Avoid entering a V8 scope.
if (!need_tick_cb) return;
@@ -220,6 +227,7 @@ static void PrepareTick(EV_P_ ev_prepare *watcher, int revents) {
static void CheckTick(EV_P_ ev_check *watcher, int revents) {
assert(watcher == &check_tick_watcher);
assert(revents == EV_CHECK);
+ UpdateLoopTime();
Tick();
}
@@ -1545,11 +1553,15 @@ static void ProcessTitleSetter(Local<String> property,
static void Load(int argc, char *argv[]) {
HandleScope scope;
+ // some constants
+ loop_time_symbol = NODE_PSYMBOL("loopTime");
+
Local<FunctionTemplate> process_template = FunctionTemplate::New();
node::EventEmitter::Initialize(process_template);
process = Persistent<Object>::New(process_template->GetFunction()->NewInstance());
+ UpdateLoopTime();
process->SetAccessor(String::New("title"),
ProcessTitleGetter,
--
1.7.0.4
From bd994d1aab7821b8e977ba48ac21305e24064e42 Mon Sep 17 00:00:00 2001
From: Jonas Pfenniger <jonas@stvs.ch>
Date: Tue, 5 Oct 2010 10:44:14 +0200
Subject: [PATCH 2/2] NEW: process.loopDate()
Small utility function that transforms process.loopTime into a Date object.
---
src/node.js | 15 +++++++++++++++
1 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/src/node.js b/src/node.js
index 7d177eb..f3763e6 100644
--- a/src/node.js
+++ b/src/node.js
@@ -29,6 +29,21 @@ process.assert = function (x, msg) {
var writeError = process.binding('stdio').writeError;
+// loopDate()
+
+var cached_ts, cached_date;
+process.loopDate = function () {
+ var ts = process.loopTime;
+
+ if (cached_ts != ts) {
+ cached_date = new Date();
+ cached_date.setTime(ts);
+ cached_ts = ts;
+ }
+
+ return cached_date;
+}
+
// nextTick()
var nextTickQueue = [];
--
1.7.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment