@@ -2,6 +2,20 @@ const chrono = require('chrono-node');
2
2
const moment = require ( 'moment-timezone' ) ;
3
3
const UserError = require ( './UserError' ) ;
4
4
5
+ const momentFromResult = ( result , timezone ) => {
6
+ const dateMoment = moment ( ) . tz ( timezone ) ;
7
+
8
+ dateMoment . set ( 'year' , result . get ( 'year' ) ) ;
9
+ dateMoment . set ( 'month' , result . get ( 'month' ) - 1 ) ;
10
+ dateMoment . set ( 'date' , result . get ( 'day' ) ) ;
11
+ dateMoment . set ( 'hour' , result . get ( 'hour' ) ) ;
12
+ dateMoment . set ( 'minute' , result . get ( 'minute' ) ) ;
13
+ dateMoment . set ( 'second' , result . get ( 'second' ) ) ;
14
+ dateMoment . set ( 'millisecond' , result . get ( 'millisecond' ) ) ;
15
+
16
+ return dateMoment ;
17
+ } ;
18
+
5
19
module . exports = ( dateString , timezone ) => {
6
20
let momentRange ;
7
21
dateString = dateString . toLowerCase ( ) ;
@@ -29,18 +43,37 @@ module.exports = (dateString, timezone) => {
29
43
moment . tz ( timezone ) . startOf ( 'day' ) . add ( - 1 , 'day' ) ,
30
44
moment . tz ( timezone ) . endOf ( 'day' ) . add ( - 1 , 'day' )
31
45
] ;
46
+ } else if ( dateString . match ( / ^ f r o m ( .* ) t o ( .* ) $ / ) ) {
47
+ // eslint-disable-next-line no-unused-vars
48
+ const [ all , from , to ] = dateString . match ( / ^ f r o m ( .* ) t o ( .* ) $ / ) ;
49
+ const fromResults = chrono . parse ( from , moment ( ) . tz ( timezone ) ) ;
50
+ const toResults = chrono . parse ( to , moment ( ) . tz ( timezone ) ) ;
51
+ if ( ! fromResults ) {
52
+ throw new UserError ( `Can't parse date: '${ from } '` ) ;
53
+ }
54
+ if ( ! toResults ) {
55
+ throw new UserError ( `Can't parse date: '${ to } '` ) ;
56
+ }
57
+ const exactGranularity = [ 'second' , 'minute' , 'hour' ] . find ( g => dateString . indexOf ( g ) !== - 1 ) || 'day' ;
58
+ momentRange = [
59
+ momentFromResult ( fromResults [ 0 ] . start , timezone ) ,
60
+ momentFromResult ( toResults [ 0 ] . start , timezone )
61
+ ] ;
62
+ momentRange = [ momentRange [ 0 ] . startOf ( exactGranularity ) , momentRange [ 1 ] . endOf ( exactGranularity ) ] ;
32
63
} else {
33
- const results = chrono . parse ( dateString ) ;
64
+ const results = chrono . parse ( dateString , moment ( ) . tz ( timezone ) ) ;
34
65
if ( ! results ) {
35
66
throw new UserError ( `Can't parse date: '${ dateString } '` ) ;
36
67
}
68
+ const exactGranularity = [ 'second' , 'minute' , 'hour' ] . find ( g => dateString . indexOf ( g ) !== - 1 ) || 'day' ;
37
69
momentRange = results [ 0 ] . end ? [
38
- moment ( results [ 0 ] . start . moment ( ) ) . tz ( timezone ) . startOf ( 'day' ) ,
39
- moment ( results [ 0 ] . end . moment ( ) ) . tz ( timezone ) . endOf ( 'day' )
70
+ momentFromResult ( results [ 0 ] . start , timezone ) ,
71
+ momentFromResult ( results [ 0 ] . end , timezone )
40
72
] : [
41
- moment ( results [ 0 ] . start . moment ( ) ) . tz ( timezone ) . startOf ( 'day' ) ,
42
- moment ( results [ 0 ] . start . moment ( ) ) . tz ( timezone ) . endOf ( 'day' )
73
+ momentFromResult ( results [ 0 ] . start , timezone ) ,
74
+ momentFromResult ( results [ 0 ] . start , timezone )
43
75
] ;
76
+ momentRange = [ momentRange [ 0 ] . startOf ( exactGranularity ) , momentRange [ 1 ] . endOf ( exactGranularity ) ] ;
44
77
}
45
78
return momentRange . map ( d => d . format ( moment . HTML5_FMT . DATETIME_LOCAL_MS ) ) ;
46
79
} ;
0 commit comments