1 /* 2 * Dual-licensed under the MIT License & the Academic Free License v. 2.1. 3 * See the file LICENSE for more information. 4 * 5 * (c) 2007-2008 by Per Cederberg & Dynabyte AB. All rights reserved. 6 */ 7 8 // Check for loaded MochiKit 9 if (typeof(MochiKit) == "undefined") { 10 throw new ReferenceError("MochiKit must be loaded before loading this script"); 11 } 12 13 /** 14 * @name MochiKit.DateTime 15 * @namespace Provides functions for handling date and time. 16 */ 17 // Check for loaded MochiKit.DateTime 18 if (typeof(MochiKit.DateTime) == "undefined") { 19 throw new ReferenceError("MochiKit.DateTime must be loaded before loading this script"); 20 } 21 22 MochiKit.DateTime.MILLIS_PER_SECOND = 1000; 23 MochiKit.DateTime.MILLIS_PER_MINUTE = 60 * 1000; 24 MochiKit.DateTime.MILLIS_PER_HOUR = 60 * 60 * 1000; 25 MochiKit.DateTime.MILLIS_PER_DAY = 24 * 60 * 60 * 1000; 26 MochiKit.DateTime.MILLIS_PER_WEEK = 7 * 24 * 60 * 60 * 1000; 27 28 /** 29 * Formats a number using two digits, i.e. pads with a leading zero 30 * character if the number is only one digit. 31 * 32 * @param {Number} value the number to format 33 * 34 * @return {String} the formatted number string 35 * 36 * @function 37 */ 38 MochiKit.DateTime._twoDigitNumber = MochiKit.Format.numberFormatter("00"); 39 40 /** 41 * Creates a new time period object from a number of milliseconds. 42 * 43 * @constructor 44 * @param {Number} millis the number of milliseconds in the period 45 * 46 * @return {Object} new time period object 47 * 48 * @class The time period class. Used to encapsulate a structured 49 * time period, split up into its variuos components. For time 50 * period calculations, the total millisecond value is normally 51 * a better choice (to avoid overflow and underflow issues). 52 * @property {Number} days The number of days in the period. This is 53 * an integer value from 0 and up. 54 * @property {Number} hours The number of hours in the period. This 55 * is an integer value between 0 and 23. 56 * @property {Number} minutes The number of minutes in the period. 57 * This is an integer value between 0 and 59. 58 * @property {Number} seconds The number of seconds in the period. 59 * This is an integer value between 0 and 59. 60 * @property {Number} millis The number of remaining milliseconds in 61 * the period. This is an integer value between 0 and 999. 62 */ 63 MochiKit.DateTime.TimePeriod = function(millis) { 64 return { 65 days: Math.floor(millis / MochiKit.DateTime.MILLIS_PER_DAY), 66 hours: Math.floor(millis / MochiKit.DateTime.MILLIS_PER_HOUR) % 24, 67 minutes: Math.floor(millis / MochiKit.DateTime.MILLIS_PER_MINUTE) % 60, 68 seconds: Math.floor(millis / MochiKit.DateTime.MILLIS_PER_SECOND) % 60, 69 millis: millis % 1000 70 }; 71 } 72 73 /** 74 * Converts a number of milliseconds to an approximate time period. 75 * 76 * @param {Number} millis the number of milliseconds 77 * 78 * @return {String} the string representation of the period 79 */ 80 MochiKit.DateTime.toApproxPeriod = function(millis) { 81 var p = MochiKit.DateTime.TimePeriod(millis); 82 83 if (p.days >= 10) { 84 return p.days + " days"; 85 } else if (p.days >= 1) { 86 return p.days + " days " + MochiKit.DateTime._twoDigitNumber(p.hours) + " hours"; 87 } else if (p.hours >= 1) { 88 return p.hours + ":" + MochiKit.DateTime._twoDigitNumber(p.minutes) + " hours"; 89 } else if (p.minutes >= 1) { 90 return p.minutes + ":" + MochiKit.DateTime._twoDigitNumber(p.seconds) + " minutes"; 91 } else if (p.seconds >= 1) { 92 return p.seconds + " seconds"; 93 } else { 94 return p.millis + " milliseconds"; 95 } 96 } 97