| 1 | /** |
|---|
| 2 | * Represents a time. |
|---|
| 3 | * @param hrs {Integer} 0 <= hrs <= 59 |
|---|
| 4 | * @param mins {Integer} 0 <= mins <= 59 |
|---|
| 5 | * @param secs {Integer} 0 <= secs <= 59 |
|---|
| 6 | */ |
|---|
| 7 | function Time(hrs, mins, secs) |
|---|
| 8 | { |
|---|
| 9 | //========== public read-only members ==========// |
|---|
| 10 | this.hrs = hrs; |
|---|
| 11 | this.mins = mins; |
|---|
| 12 | this.secs = secs; |
|---|
| 13 | |
|---|
| 14 | //========== public static methods ==========// |
|---|
| 15 | Time.formatTime = formatTime; |
|---|
| 16 | |
|---|
| 17 | //========== public methods ==========// |
|---|
| 18 | this.format = format; |
|---|
| 19 | this.compareTo = compareTo; |
|---|
| 20 | this.getSeconds = getSeconds; |
|---|
| 21 | |
|---|
| 22 | //========== method definitions ===========// |
|---|
| 23 | /** |
|---|
| 24 | * @return The number of seconds equal to the total amount time. |
|---|
| 25 | */ |
|---|
| 26 | function getSeconds() |
|---|
| 27 | { |
|---|
| 28 | return this.secs + this.mins * 60 + this.hrs * 3600; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * @param time {Time} The Time to compare this to. |
|---|
| 33 | * @return < 0 if this is less than time; |
|---|
| 34 | * 0 if this is equal to time; |
|---|
| 35 | * > 0 if this is greater than time |
|---|
| 36 | */ |
|---|
| 37 | function compareTo(time) |
|---|
| 38 | { |
|---|
| 39 | return this.getSeconds() - time.getSeconds(); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * @return Returns the time in the form of HH:MM:SS. |
|---|
| 44 | */ |
|---|
| 45 | function format() { |
|---|
| 46 | function pad0(n) |
|---|
| 47 | { |
|---|
| 48 | return n < 10 ? "0" + n : n; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | return pad0(hrs) + ":" + pad0(mins) + ":" + pad0(secs); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Converts time in seconds into the form of HH:MM:SS. |
|---|
| 56 | * @param seconds {Integer} |
|---|
| 57 | * @return The given time in the form of HH:MM:SS. |
|---|
| 58 | */ |
|---|
| 59 | function formatTime(seconds) |
|---|
| 60 | { |
|---|
| 61 | function pad0(n) |
|---|
| 62 | { |
|---|
| 63 | return n < 10 ? "0" + n : n; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | var remainSecs = seconds % 60; |
|---|
| 67 | var minutes = (seconds - remainSecs) / 60; |
|---|
| 68 | var remainMins = minutes % 60; |
|---|
| 69 | var hours = (minutes - remainMins) / 60; |
|---|
| 70 | return pad0(hours) + ":" + pad0(remainMins) + ":" + pad0(remainSecs); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|