forked from NatLabRockies/nodehaystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTime.js
More file actions
131 lines (115 loc) · 3.26 KB
/
HTime.js
File metadata and controls
131 lines (115 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Copyright (c) 2015, Shawn Jacobson
// Licensed under the Academic Free License version 3.0
//
// Ported from @see {@link https://bitbucket.org/brianfrank/haystack-java|Haystack Java Toolkit}
//
// History:
// 21 Mar 2015 Shawn Jacobson Creation
//
var HVal = require('./HVal');
/**
* HTime models a time of day tag value.
* @see {@link http://project-haystack.org/doc/TagModel#tagKinds|Project Haystack}
*
* @constructor
* @extends {HVal}
* @param {int} hour
* @param {int} min
* @param {int} sec
* @param {int} ms
*/
function HTime(hour, min, sec, ms) {
/** int - Hour of day as 0-23 */
this.hour = hour;
/** int - Minute of hour as 0-59 */
this.min = min;
/** int - Second of minute as 0-59 */
this.sec = (typeof(sec) === 'undefined' ? 0 : sec);
/** int - Fractional seconds in milliseconds 0-999 */
this.ms = (typeof(ms) === 'undefined' ? 0 : ms);
// ensure singleton usage
if (hour==0 && min==0 && this.sec==0 && this.ms==0 && arguments.callee._midnightSingletonInstance) return arguments.callee._midnightSingletonInstance;
if (hour==0 && min==0 && this.sec==0 && this.ms==0) arguments.callee._midnightSingletonInstance = this;
}
HTime.prototype = Object.create(HVal.prototype);
module.exports = HTime;
var HZincReader = require('./io/HZincReader');
/**
* Equals is based on hour, min, sec, ms
* @param {HTime}
* @return {boolean}
*/
HTime.prototype.equals = function(that) {
return that instanceof HTime && this.hour === that.hour &&
this.min === that.min && this.sec === that.sec && this.ms === that.ms;
};
/**
* Return sort order as negative, 0, or positive
* @param {HTime}
* @return {int}
*/
HTime.prototype.compareTo = function(that) {
if (this.hour < that.hour) return -1;
else if (this.hour > that.hour) return 1;
if (this.min < that.min) return -1;
else if (this.min > that.min) return 1;
if (this.sec < that.sec) return -1;
else if (this.sec > that.sec) return 1;
if (this.ms < that.ms) return -1;
else if (this.ms > that.ms) return 1;
return 0;
};
/**
* Encode as "hh:mm:ss.FFF"
* @return {string}
*/
HTime.prototype.toZinc = function() {
var s = "";
if (this.hour < 10) s += "0";
s += this.hour + ":";
if (this.min < 10) s += "0";
s += this.min + ":";
if (this.sec < 10) s += "0";
s += this.sec;
if (this.ms !== 0) {
s += ".";
if (this.ms < 10) s += "0";
if (this.ms < 100) s += "0";
s += this.ms;
}
return s;
};
/**
* Encode as "h:hh:mm:ss.FFF"
* @return {string}
*/
HTime.prototype.toJSON = function() {
return "h:" + this.toZinc();
};
/**
* Singleton for midnight 00:00
* @static
* @return {HTime}
*/
HTime.MIDNIGHT = new HTime(0, 0, 0, 0);
/**
* Construct with all fields, with Javascript Date object, or Parse from string format "hh:mm:ss.FF"
* @static
* @param {int} hour
* @param {int} min
* @param {int} sec
* @param {int} ms
* @return {HTime}
*/
HTime.make = function(arg1, min, sec, ms) {
if (HVal.typeis(arg1, 'string', String)) {
var val = new HZincReader(arg1).readScalar();
if (val instanceof HTime) return val;
throw new Error("Parse Error: " + arg1);
} else if (arg1 instanceof Date) {
return new HTime(arg1.getHours(), arg1.getMinutes(), arg1.getSeconds(), arg1.getMilliseconds());
} else {
return new HTime(arg1, min, sec, ms);
}
};