relativetimeprovider.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Definition the goog.debug.RelativeTimeProvider class.
  16. *
  17. */
  18. goog.provide('goog.debug.RelativeTimeProvider');
  19. /**
  20. * A simple object to keep track of a timestamp considered the start of
  21. * something. The main use is for the logger system to maintain a start time
  22. * that is occasionally reset. For example, in Gmail, we reset this relative
  23. * time at the start of a user action so that timings are offset from the
  24. * beginning of the action. This class also provides a singleton as the default
  25. * behavior for most use cases is to share the same start time.
  26. *
  27. * @constructor
  28. * @final
  29. */
  30. goog.debug.RelativeTimeProvider = function() {
  31. /**
  32. * The start time.
  33. * @type {number}
  34. * @private
  35. */
  36. this.relativeTimeStart_ = goog.now();
  37. };
  38. /**
  39. * Default instance.
  40. * @type {goog.debug.RelativeTimeProvider}
  41. * @private
  42. */
  43. goog.debug.RelativeTimeProvider.defaultInstance_ =
  44. new goog.debug.RelativeTimeProvider();
  45. /**
  46. * Sets the start time to the specified time.
  47. * @param {number} timeStamp The start time.
  48. */
  49. goog.debug.RelativeTimeProvider.prototype.set = function(timeStamp) {
  50. this.relativeTimeStart_ = timeStamp;
  51. };
  52. /**
  53. * Resets the start time to now.
  54. */
  55. goog.debug.RelativeTimeProvider.prototype.reset = function() {
  56. this.set(goog.now());
  57. };
  58. /**
  59. * @return {number} The start time.
  60. */
  61. goog.debug.RelativeTimeProvider.prototype.get = function() {
  62. return this.relativeTimeStart_;
  63. };
  64. /**
  65. * @return {goog.debug.RelativeTimeProvider} The default instance.
  66. */
  67. goog.debug.RelativeTimeProvider.getDefaultInstance = function() {
  68. return goog.debug.RelativeTimeProvider.defaultInstance_;
  69. };