Skip to content

Commit 0c06328

Browse files
committed
[DATETIME]: added same timezone parsing functionality
Signed-off-by: ashish <[email protected]>
1 parent f4b9df1 commit 0c06328

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
"@oclif/plugin-help": "^2.2.1",
1717
"@oclif/plugin-not-found": "^1.2.3",
1818
"@oclif/plugin-warn-if-update-available": "^1.7.0",
19-
"@types/crypto-js": "^3.1.43",
20-
"@types/signale": "^1.2.1",
2119
"axios": "^0.19.0",
2220
"chalk": "^2.4.2",
2321
"crypto-js": "^3.1.9-1",
@@ -36,7 +34,10 @@
3634
"@oclif/tslint": "^3.1.1",
3735
"@types/chai": "^4.2.3",
3836
"@types/mocha": "^5.2.7",
37+
"@types/moment-timezone": "^0.5.12",
3938
"@types/node": "^12.11.2",
39+
"@types/crypto-js": "^3.1.43",
40+
"@types/signale": "^1.2.1",
4041
"chai": "^4.2.0",
4142
"globby": "^10.0.1",
4243
"mocha": "^6.2.2",

src/commands/datetime.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import {Command, flags} from '@oclif/command'
22
import chalk from 'chalk'
3-
import * as moment from 'moment'
3+
import * as moment from 'moment-timezone' //has both momentjs and timezone support
44

55
// @ts-ignore
66
moment.suppressDeprecationWarnings = true
77

88
import Logger from '../utilities/logger'
99

10-
// TODO: add timezone support
10+
// TODO: add timezone support for input and output
1111
export default class Datetime extends Command {
1212
static description = 'Date and Time utility'
1313

1414
static defaultFormat = 'Do MMMM YYYY, h:m:s A, Z UTC'
1515

1616
static flags = {
1717
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'}),
1919
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'}),
2121
locale: flags.string({char: 'l', description: 'Locale, default: en'}),
2222
}
2323

@@ -30,18 +30,28 @@ export default class Datetime extends Command {
3030
args.date = this.getDateString(flags, args) // getting date object
3131
args.locale = this.getLocale(flags, args) // getting date object
3232
args.format = this.getFormat(flags, args) // getting date object
33+
args.timezone = this.getTimezone(flags, args) // getting date object
3334

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') }`)
3536
Logger.info(this, `Locale: ${chalk.magenta(args.locale)}`)
3637
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
3739

3840
args.momentDate = this.getMomentDate(flags, args)
3941
this.checkParameters(flags, args)
4042

41-
Logger.success(this, `${args.momentDate.format(args.format)}`)
43+
Logger.success(this, `${args.momentDate.tz(args.timezone).format(args.format)}`)
4244

4345
}
4446

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+
4555
// tslint:disable-next-line:no-unused
4656
private getLocale(flags: any, args: any) {
4757
if (flags.locale)

0 commit comments

Comments
 (0)