/src/js/Rickshaw.Fixtures.Time.js

http://github.com/shutterstock/rickshaw · JavaScript · 115 lines · 94 code · 21 blank · 0 comment · 12 complexity · 71962c7800df7b55427c9194aaa6c7bc MD5 · raw file

  1. Rickshaw.namespace('Rickshaw.Fixtures.Time');
  2. Rickshaw.Fixtures.Time = function() {
  3. var self = this;
  4. this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  5. this.units = [
  6. {
  7. name: 'decade',
  8. seconds: 86400 * 365.25 * 10,
  9. formatter: function(d) { return (parseInt(d.getUTCFullYear() / 10, 10) * 10) }
  10. }, {
  11. name: 'year',
  12. seconds: 86400 * 365.25,
  13. formatter: function(d) { return d.getUTCFullYear() }
  14. }, {
  15. name: 'month',
  16. seconds: 86400 * 30.5,
  17. formatter: function(d) { return self.months[d.getUTCMonth()] }
  18. }, {
  19. name: 'week',
  20. seconds: 86400 * 7,
  21. formatter: function(d) { return self.formatDate(d) }
  22. }, {
  23. name: 'day',
  24. seconds: 86400,
  25. formatter: function(d) { return d.getUTCDate() }
  26. }, {
  27. name: '6 hour',
  28. seconds: 3600 * 6,
  29. formatter: function(d) { return self.formatTime(d) }
  30. }, {
  31. name: 'hour',
  32. seconds: 3600,
  33. formatter: function(d) { return self.formatTime(d) }
  34. }, {
  35. name: '15 minute',
  36. seconds: 60 * 15,
  37. formatter: function(d) { return self.formatTime(d) }
  38. }, {
  39. name: 'minute',
  40. seconds: 60,
  41. formatter: function(d) { return d.getUTCMinutes() + 'm' }
  42. }, {
  43. name: '15 second',
  44. seconds: 15,
  45. formatter: function(d) { return d.getUTCSeconds() + 's' }
  46. }, {
  47. name: 'second',
  48. seconds: 1,
  49. formatter: function(d) { return d.getUTCSeconds() + 's' }
  50. }, {
  51. name: 'decisecond',
  52. seconds: 1/10,
  53. formatter: function(d) { return d.getUTCMilliseconds() + 'ms' }
  54. }, {
  55. name: 'centisecond',
  56. seconds: 1/100,
  57. formatter: function(d) { return d.getUTCMilliseconds() + 'ms' }
  58. }
  59. ];
  60. this.unit = function(unitName) {
  61. return this.units.filter( function(unit) { return unitName == unit.name } ).shift();
  62. };
  63. this.formatDate = function(d) {
  64. return d3.time.format('%b %e')(d);
  65. };
  66. this.formatTime = function(d) {
  67. return d.toUTCString().match(/(\d+:\d+):/)[1];
  68. };
  69. this.ceil = function(time, unit) {
  70. var date, floor, year;
  71. if (unit.name == 'month') {
  72. date = new Date(time * 1000);
  73. floor = Date.UTC(date.getUTCFullYear(), date.getUTCMonth()) / 1000;
  74. if (floor == time) return time;
  75. year = date.getUTCFullYear();
  76. var month = date.getUTCMonth();
  77. if (month == 11) {
  78. month = 0;
  79. year = year + 1;
  80. } else {
  81. month += 1;
  82. }
  83. return Date.UTC(year, month) / 1000;
  84. }
  85. if (unit.name == 'year') {
  86. date = new Date(time * 1000);
  87. floor = Date.UTC(date.getUTCFullYear(), 0) / 1000;
  88. if (floor == time) return time;
  89. year = date.getUTCFullYear() + 1;
  90. return Date.UTC(year, 0) / 1000;
  91. }
  92. return Math.ceil(time / unit.seconds) * unit.seconds;
  93. };
  94. };