MillisecondExpression.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. /**
  3. * An $millisecond pipeline expression.
  4. * @see evaluateInternal
  5. * @class MillisecondExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var MillisecondExpression = module.exports = function MillisecondExpression() {
  11. this.nargs = 1;
  12. if (arguments.length !== 1) throw new Error("one arg expected");
  13. base.call(this);
  14. }, klass = MillisecondExpression,
  15. base = require("./NaryExpression"),
  16. proto = klass.prototype = Object.create(base.prototype, {
  17. constructor: {
  18. value: klass
  19. }
  20. });
  21. // DEPENDENCIES
  22. var Expression = require("./Expression");
  23. // PROTOTYPE MEMBERS
  24. proto.getOpName = function getOpName() {
  25. return "$millisecond";
  26. };
  27. /**
  28. * Takes a date and returns the millisecond between 0 and 999, but can be 1000 to account for leap milliseconds.
  29. * @method evaluateInternal
  30. **/
  31. proto.evaluateInternal = function evaluateInternal(vars) {
  32. var date = this.operands[0].evaluateInternal(vars);
  33. return date.getUTCMilliseconds(); //TODO: incorrect for last millisecond of leap year, need to fix...
  34. // currently leap milliseconds are unsupported in v8
  35. // http://code.google.com/p/v8/issues/detail?id=1944
  36. };
  37. /** Register Expression */
  38. Expression.registerExpression("$millisecond", base.parse(MillisecondExpression));