‘liusuyi’
2023-08-09 161b9318e345c8a0c9cdc133b33a1c759495f323
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict';
 
const Hoek = require('@hapi/hoek');
 
const Any = require('../any');
const Ref = require('../../ref');
 
 
const internals = {};
 
internals.isoDate = /^(?:[-+]\d{2})?(?:\d{4}(?!\d{2}\b))(?:(-?)(?:(?:0[1-9]|1[0-2])(?:\1(?:[12]\d|0[1-9]|3[01]))?|W(?:[0-4]\d|5[0-2])(?:-?[1-7])?|(?:00[1-9]|0[1-9]\d|[12]\d{2}|3(?:[0-5]\d|6[1-6])))(?![T]$|[T][\d]+Z$)(?:[T\s](?:(?:(?:[01]\d|2[0-3])(?:(:?)[0-5]\d)?|24\:?00)(?:[.,]\d+(?!:))?)(?:\2[0-5]\d(?:[.,]\d+)?)?(?:[Z]|(?:[+-])(?:[01]\d|2[0-3])(?::?[0-5]\d)?)?)?)?$/;
internals.invalidDate = new Date('');
internals.isIsoDate = (() => {
 
    const isoString = internals.isoDate.toString();
 
    return (date) => {
 
        return date && (date.toString() === isoString);
    };
})();
 
internals.Date = class extends Any {
 
    constructor() {
 
        super();
        this._type = 'date';
    }
 
    _base(value, state, options) {
 
        const result = {
            value: (options.convert && internals.Date.toDate(value, this._flags.format, this._flags.timestamp, this._flags.multiplier)) || value
        };
 
        if (result.value instanceof Date && !isNaN(result.value.getTime())) {
            result.errors = null;
        }
        else if (!options.convert) {
            result.errors = this.createError('date.strict', { value }, state, options);
        }
        else {
            let type;
            if (internals.isIsoDate(this._flags.format)) {
                type = 'isoDate';
            }
            else if (this._flags.timestamp) {
                type = `timestamp.${this._flags.timestamp}`;
            }
            else {
                type = 'base';
            }
 
            result.errors = this.createError(`date.${type}`, { value }, state, options);
        }
 
        return result;
    }
 
    static toDate(value, format, timestamp, multiplier) {
 
        if (value instanceof Date) {
            return value;
        }
 
        if (typeof value === 'string' ||
            (typeof value === 'number' && !isNaN(value) && isFinite(value))) {
 
            const isIsoDate = format && internals.isIsoDate(format);
            if (!isIsoDate &&
                typeof value === 'string' &&
                /^[+-]?\d+(\.\d+)?$/.test(value)) {
 
                value = parseFloat(value);
            }
 
            let date;
            if (isIsoDate) {
                date = format.test(value) ? new Date(value.toString()) : internals.invalidDate;
            }
            else if (timestamp) {
                date = /^\s*$/.test(value) ? internals.invalidDate : new Date(value * multiplier);
            }
            else {
                date = new Date(value);
            }
 
            if (!isNaN(date.getTime())) {
                return date;
            }
        }
 
        return null;
    }
 
    iso() {
 
        if (this._flags.format === internals.isoDate) {
            return this;
        }
 
        const obj = this.clone();
        obj._flags.format = internals.isoDate;
        return obj;
    }
 
    timestamp(type = 'javascript') {
 
        const allowed = ['javascript', 'unix'];
        Hoek.assert(allowed.includes(type), '"type" must be one of "' + allowed.join('", "') + '"');
 
        if (this._flags.timestamp === type) {
            return this;
        }
 
        const obj = this.clone();
        obj._flags.timestamp = type;
        obj._flags.multiplier = type === 'unix' ? 1000 : 1;
        return obj;
    }
 
    _isIsoDate(value) {
 
        return internals.isoDate.test(value);
    }
 
};
 
internals.compare = function (type, compare) {
 
    return function (date) {
 
        const isNow = date === 'now';
        const isRef = Ref.isRef(date);
 
        if (!isNow && !isRef) {
            date = internals.Date.toDate(date);
        }
 
        Hoek.assert(date, 'Invalid date format');
 
        return this._test(type, date, function (value, state, options) {
 
            let compareTo;
            if (isNow) {
                compareTo = Date.now();
            }
            else if (isRef) {
                const refValue = date(state.reference || state.parent, options);
                compareTo = internals.Date.toDate(refValue);
 
                if (!compareTo) {
                    return this.createError('date.ref', { ref: date, value: refValue }, state, options);
                }
 
                compareTo = compareTo.getTime();
            }
            else {
                compareTo = date.getTime();
            }
 
            if (compare(value.getTime(), compareTo)) {
                return value;
            }
 
            return this.createError('date.' + type, { limit: new Date(compareTo), value }, state, options);
        });
    };
};
 
 
internals.Date.prototype.min = internals.compare('min', (value, date) => value >= date);
internals.Date.prototype.max = internals.compare('max', (value, date) => value <= date);
internals.Date.prototype.greater = internals.compare('greater', (value, date) => value > date);
internals.Date.prototype.less = internals.compare('less', (value, date) => value < date);
 
 
module.exports = new internals.Date();