#include <windows.h>
#include <v8.h>

using namespace v8;

static double ft2ms(FILETIME* ft) {
  ULARGE_INTEGER uli;
  uli.LowPart = ft->dwLowDateTime;
  uli.HighPart = ft->dwHighDateTime;

  return (double)(uli.QuadPart/10);
}

Handle<Value> CPUTime(const Arguments& args) {
  HandleScope scope;
  FILETIME ftime, fsys, fuser;

  HANDLE self = GetCurrentProcess();
  int ret = GetProcessTimes(self, &ftime, &ftime, &fsys, &fuser);

  if (!ret) {
    return scope.Close(Undefined());
  }

  double rtime = ft2ms(&fsys) + ft2ms(&fuser);

  return scope.Close(Number::New(rtime));
}