1
1
import { Command , flags } from '@oclif/command'
2
2
import chalk from 'chalk'
3
- import * as moment from 'moment'
3
+ import * as moment from 'moment-timezone' //has both momentjs and timezone support
4
4
5
5
// @ts -ignore
6
6
moment . suppressDeprecationWarnings = true
7
7
8
8
import Logger from '../utilities/logger'
9
9
10
- // TODO: add timezone support
10
+ // TODO: add timezone support for input and output
11
11
export default class Datetime extends Command {
12
12
static description = 'Date and Time utility'
13
13
14
14
static defaultFormat = 'Do MMMM YYYY, h:m:s A, Z UTC'
15
15
16
16
static flags = {
17
17
help : flags . help ( { char : 'h' } ) ,
18
- date : flags . string ( { char : 'd' , description : 'Datetime input string, default: Now() , could also be passed through argument' } ) ,
18
+ date : flags . string ( { char : 'd' , description : 'Datetime input string, default: Current Datetime , could also be passed through argument' } ) ,
19
19
format : flags . string ( { char : 'f' , description : `Datetime format, default: ${ Datetime . defaultFormat } ` } ) ,
20
- timezone : flags . string ( { char : 'z' , description : 'Timezone for Datetime' } ) ,
20
+ timezone : flags . string ( { char : 'z' , description : 'Timezone for Datetime parsing, default: Your timezone ' } ) ,
21
21
locale : flags . string ( { char : 'l' , description : 'Locale, default: en' } ) ,
22
22
}
23
23
@@ -30,18 +30,28 @@ export default class Datetime extends Command {
30
30
args . date = this . getDateString ( flags , args ) // getting date object
31
31
args . locale = this . getLocale ( flags , args ) // getting date object
32
32
args . format = this . getFormat ( flags , args ) // getting date object
33
+ args . timezone = this . getTimezone ( flags , args ) // getting date object
33
34
34
- Logger . info ( this , `Input String: ${ args . date ? args . date : chalk . magenta ( 'Not Provided, using Now() ' ) } ` )
35
+ Logger . info ( this , `Input String: ${ args . date ? args . date : chalk . magenta ( 'Not Provided, using Current timestamp ' ) } ` )
35
36
Logger . info ( this , `Locale: ${ chalk . magenta ( args . locale ) } ` )
36
37
Logger . info ( this , `Format: ${ chalk . magenta ( args . format ) } ` )
38
+ Logger . info ( this , `Timezone: ${ chalk . magenta ( args . timezone ) } ` ) // true - do not used cached timezone, find every time
37
39
38
40
args . momentDate = this . getMomentDate ( flags , args )
39
41
this . checkParameters ( flags , args )
40
42
41
- Logger . success ( this , `${ args . momentDate . format ( args . format ) } ` )
43
+ Logger . success ( this , `${ args . momentDate . tz ( args . timezone ) . format ( args . format ) } ` )
42
44
43
45
}
44
46
47
+ // tslint:disable-next-line:no-unused
48
+ private getTimezone ( flags : any , args : any ) {
49
+ if ( flags . timezone )
50
+ return flags . timezone
51
+ else
52
+ return moment . tz . guess ( true )
53
+ }
54
+
45
55
// tslint:disable-next-line:no-unused
46
56
private getLocale ( flags : any , args : any ) {
47
57
if ( flags . locale )
0 commit comments