1 | /*
|
2 |
|
3 | ----------------------------------------------------------------------------
|
4 | | qewd-ripple: QEWD-based Middle Tier for Ripple OSI |
|
5 | | |
|
6 | | Copyright (c) 2016-17 Ripple Foundation Community Interest Company |
|
7 | | All rights reserved. |
|
8 | | |
|
9 | | http://rippleosi.org |
|
10 | | Email: code.custodian@rippleosi.org |
|
11 | | |
|
12 | | Author: Rob Tweed, M/Gateway Developments Ltd |
|
13 | | |
|
14 | | Licensed under the Apache License, Version 2.0 (the "License"); |
|
15 | | you may not use this file except in compliance with the License. |
|
16 | | You may obtain a copy of the License at |
|
17 | | |
|
18 | | http://www.apache.org/licenses/LICENSE-2.0 |
|
19 | | |
|
20 | | Unless required by applicable law or agreed to in writing, software |
|
21 | | distributed under the License is distributed on an "AS IS" BASIS, |
|
22 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
23 | | See the License for the specific language governing permissions and |
|
24 | | limitations under the License. |
|
25 | ----------------------------------------------------------------------------
|
26 |
|
27 | 26 January 2017
|
28 |
|
29 | */
|
30 |
|
31 | var moment = require('moment-timezone');
|
32 | var timezone = 'Europe/London';
|
33 |
|
34 |
|
35 | function format(date) {
|
36 | if (typeof date !== 'object') date = new Date(date);
|
37 | return moment(date).tz(timezone).format();
|
38 | }
|
39 |
|
40 | function now() {
|
41 | return format(new Date());
|
42 | }
|
43 |
|
44 | function isDST(date) {
|
45 | if (typeof date !== 'object') date = new Date(date);
|
46 | return moment(date).tz(timezone).isDST();
|
47 | }
|
48 |
|
49 | function toSqlPASFormat(date) {
|
50 | if (typeof date !== 'object') date = new Date(date);
|
51 | return moment(date).tz(timezone).format("YYYY-MM-DD");
|
52 | }
|
53 |
|
54 | function toGMT(date) {
|
55 | // if a date is in summer time, return as GMT, ie with an hour deducted
|
56 | var result = date;
|
57 | if (moment(date).tz(timezone).isDST()) result = new Date(date.getTime() - 3600000);
|
58 | return result;
|
59 | }
|
60 |
|
61 | function getRippleTime(date, host) {
|
62 | //console.log('*** host = ' + host);
|
63 | if (date === '') return date;
|
64 | var dt = new Date(date);
|
65 | if (host === 'ethercis') dt = toGMT(dt);
|
66 | return dt.getTime();
|
67 | }
|
68 |
|
69 | function msSinceMidnight(date, host, GMTCheck) {
|
70 | var e = new Date(date);
|
71 | //if (host === 'ethercis') e = toGMT(e);
|
72 | if (GMTCheck) e = toGMT(e);
|
73 | return e.getTime() - e.setHours(0,0,0,0);
|
74 | }
|
75 |
|
76 | function msAtMidnight(date, host, GMTCheck) {
|
77 | var e = new Date(date);
|
78 | //if (host === 'ethercis') e = toGMT(e);
|
79 | if (GMTCheck) e = toGMT(e);
|
80 | return e.setHours(0,0,0,0);
|
81 | }
|
82 |
|
83 | module.exports = {
|
84 | format: format,
|
85 | now: now,
|
86 | isDST: isDST,
|
87 | toGMT: toGMT,
|
88 | msSinceMidnight: msSinceMidnight,
|
89 | msAtMidnight: msAtMidnight,
|
90 | getRippleTime: getRippleTime,
|
91 | toSqlPASFormat: toSqlPASFormat
|
92 | }; |
\ | No newline at end of file |