MillisecondExpression.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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.fixedArity(1);
  12. if (arguments.length !== 0) throw new Error("zero args 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(doc) {
  32. this.checkArgCount(1);
  33. var date = this.operands[0].evaluateInternal(doc);
  34. return date.getUTCMilliseconds(); //TODO: incorrect for last millisecond of leap year, need to fix...
  35. // currently leap milliseconds are unsupported in v8
  36. // http://code.google.com/p/v8/issues/detail?id=1944
  37. };
  38. /** Register Expression */
  39. Expression.registerExpression("$millisecond", MillisecondExpression.parse);