|
1 | 1 | import CandleInterface from './CandleInterface';
|
2 | 2 | import MiscInterface from './MiscInterface';
|
3 | 3 | import IndicatorsInterface from './IndicatorsInterface';
|
| 4 | +import { translate } from '../../../common/i18n'; |
4 | 5 |
|
5 | 6 | // prettier-ignore
|
6 | 7 | export default Interface => class extends IndicatorsInterface(
|
7 | 8 | MiscInterface(CandleInterface(Interface))) {
|
8 | 9 | getToolsInterface() {
|
9 | 10 | return {
|
10 |
| - getTime: () => parseInt(new Date().getTime() / 1000), |
| 11 | + getTime : () => parseInt(new Date().getTime() / 1000), |
| 12 | + toDateTime: (timestamp) => { |
| 13 | + const getTwoDigitValue = input => { |
| 14 | + if (input < 10) { |
| 15 | + return `0${input}`; |
| 16 | + } |
| 17 | + return `${input}`; |
| 18 | + } |
| 19 | + const invalidTimestamp = () => `${translate('Invalid timestamp')}: ${timestamp}`; |
| 20 | + if (typeof timestamp === 'number') { |
| 21 | + const dateTime = new Date(timestamp * 1000); |
| 22 | + if (dateTime.getTime()) { |
| 23 | + const year = dateTime.getFullYear(); |
| 24 | + const month = getTwoDigitValue(dateTime.getMonth() + 1); |
| 25 | + const day = getTwoDigitValue(dateTime.getDate()); |
| 26 | + const hours = getTwoDigitValue(dateTime.getHours()); |
| 27 | + const minutes = getTwoDigitValue(dateTime.getMinutes()); |
| 28 | + const seconds = getTwoDigitValue(dateTime.getSeconds()); |
| 29 | + const formatGTMoffset = () => { |
| 30 | + const GMToffsetRaw = dateTime.getTimezoneOffset(); |
| 31 | + const sign = GMToffsetRaw > 0 ? '-' : '+'; |
| 32 | + const GMToffset = Math.abs(GMToffsetRaw); |
| 33 | + const h = Math.floor(GMToffset / 60); |
| 34 | + const m = GMToffset - h * 60; |
| 35 | + return `GMT${sign}${getTwoDigitValue(h)}${getTwoDigitValue(m)}`; |
| 36 | + } |
| 37 | + return `${year}-${month}-${day} ${hours}:${minutes}:${seconds} ${formatGTMoffset()}`; |
| 38 | + } |
| 39 | + return invalidTimestamp(); |
| 40 | + } |
| 41 | + return invalidTimestamp(); |
| 42 | + }, |
| 43 | + toTimestamp: (dateTimeString) => { |
| 44 | + const invalidDatetime = () => `${translate('Invalid date/time')}: ${dateTimeString}`; |
| 45 | + if (typeof dateTimeString === 'string') { |
| 46 | + const dateTime = dateTimeString |
| 47 | + .replace(/[^0-9.:-\s]/g, '') |
| 48 | + .replace(/\s+/g,' ') |
| 49 | + .trim() |
| 50 | + .split(' '); |
| 51 | + |
| 52 | + const d = /^[12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/; |
| 53 | + const t = /^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])(:([0-5][0-9])?)?$/; |
| 54 | + |
| 55 | + let validatedDateTime; |
| 56 | + |
| 57 | + if(dateTime.length >= 2) { |
| 58 | + validatedDateTime = d.test(dateTime[0]) && t.test(dateTime[1]) ? `${dateTime[0]}T${dateTime[1]}` : null; |
| 59 | + } else if(dateTime.length === 1) { |
| 60 | + validatedDateTime = d.test(dateTime[0]) ? dateTime[0] : null; |
| 61 | + } else { |
| 62 | + validatedDateTime = null; |
| 63 | + } |
| 64 | + |
| 65 | + if(validatedDateTime) { |
| 66 | + const dateObj = new Date(validatedDateTime); |
| 67 | + // eslint-disable-next-line no-restricted-globals |
| 68 | + if(dateObj instanceof Date && !isNaN(dateObj)) { |
| 69 | + return dateObj.getTime() / 1000; |
| 70 | + } |
| 71 | + } |
| 72 | + return invalidDatetime(); |
| 73 | + } |
| 74 | + return invalidDatetime(); |
| 75 | + }, |
11 | 76 | ...this.getCandleInterface(),
|
12 | 77 | ...this.getMiscInterface(),
|
13 | 78 | ...this.getIndicatorsInterface(),
|
|
0 commit comments