artdaq_node_server  v1_00_07
 All Classes Namespaces Files Variables Pages
date_formatter.js
1 
7 var DateFormatter = function (opts) {
8  this.opts = {}
9  this.setOpts(opts)
10 }
11 
17 DateFormatter.DEFAULT_OPTIONS = {
18  colons: true
19 , hyphens: false
20 , local: true
21 , ms: false
22 , offset: false
23 }
24 
30 DateFormatter.ISO8601 = new RegExp(
31  '([0-9]{4})([-]?([0-9]{2}))([-]?([0-9]{2}))'
32 + '(T([0-9]{2})(((:?([0-9]{2}))?((:?([0-9]{2}))?(\.([0-9]+))?))?)'
33 + '(Z|([+-]([0-9]{2}(:?([0-9]{2}))?)))?)?'
34 )
35 
36 
52 DateFormatter.prototype.setOpts = function (opts) {
53  if (!opts) opts = DateFormatter.DEFAULT_OPTIONS
54 
55  var ctx = this
56  Object.keys(DateFormatter.DEFAULT_OPTIONS).forEach(function (k) {
57  ctx.opts[k] = opts.hasOwnProperty(k) ?
58  opts[k] : DateFormatter.DEFAULT_OPTIONS[k]
59  })
60 }
61 
69 DateFormatter.prototype.decodeIso8601 = function(time) {
70  var dateParts = time.toString().match(DateFormatter.ISO8601)
71  if (!dateParts) {
72  throw new Error('Expected a ISO8601 datetime but got \'' + time + '\'')
73  }
74 
75  var date = [
76  [dateParts[1], dateParts[3] || '01', dateParts[5] || '01'].join('-')
77  , 'T'
78  , [
79  dateParts[7] || '00'
80  , dateParts[11] || '00'
81  , dateParts[14] || '00'
82  ].join(':')
83  , '.'
84  , dateParts[16] || '000'
85  ].join('')
86 
87  date += (dateParts[17] !== undefined) ?
88  dateParts[17] +
89  ((dateParts[19] && dateParts[20] === undefined) ? '00' : '') :
90  DateFormatter.formatCurrentOffset(new Date(date))
91 
92  return new Date(date)
93 }
94 
101 DateFormatter.prototype.encodeIso8601 = function(date) {
102  var parts = this.opts.local ?
103  DateFormatter.getLocalDateParts(date) :
104  DateFormatter.getUTCDateParts(date)
105 
106  return [
107  [parts[0],parts[1],parts[2]].join(this.opts.hyphens ? '-' : '')
108  , 'T'
109  , [parts[3],parts[4],parts[5]].join(this.opts.colons ? ':' : '')
110  , (this.opts.ms) ? '.' + parts[6] : ''
111  , (this.opts.local) ? ((this.opts.offset) ?
112  DateFormatter.formatCurrentOffset(date) : '') : 'Z'
113  ].join('')
114 }
115 
123 DateFormatter.getUTCDateParts = function (date) {
124  return [
125  date.getUTCFullYear()
126  , DateFormatter.zeroPad(date.getUTCMonth()+1,2)
127  , DateFormatter.zeroPad(date.getUTCDate(),2)
128  , DateFormatter.zeroPad(date.getUTCHours(), 2)
129  , DateFormatter.zeroPad(date.getUTCMinutes(), 2)
130  , DateFormatter.zeroPad(date.getUTCSeconds(), 2)
131  , DateFormatter.zeroPad(date.getUTCMilliseconds(), 3)]
132 }
133 
134 
142 DateFormatter.getLocalDateParts = function (date) {
143  return [
144  date.getFullYear()
145  , DateFormatter.zeroPad(date.getMonth()+1,2)
146  , DateFormatter.zeroPad(date.getDate(),2)
147  , DateFormatter.zeroPad(date.getHours(), 2)
148  , DateFormatter.zeroPad(date.getMinutes(), 2)
149  , DateFormatter.zeroPad(date.getSeconds(), 2)
150  , DateFormatter.zeroPad(date.getMilliseconds(), 3)]
151 }
152 
162 DateFormatter.zeroPad = function (digit, length) {
163  var padded = '' + digit
164  while (padded.length < length) {
165  padded = '0' + padded
166  }
167 
168  return padded
169 }
170 
177 DateFormatter.formatCurrentOffset = function (d) {
178  var offset = (d || new Date()).getTimezoneOffset()
179  return (offset === 0) ? 'Z' : [
180  (offset < 0) ? '+' : '-'
181  , DateFormatter.zeroPad(Math.abs(Math.floor(offset/60)),2)
182  , ':'
183  , DateFormatter.zeroPad(Math.abs(offset%60),2)
184  ].join('')
185 }
186 
187 // export an instance of DateFormatter only.
188 module.exports = new DateFormatter()