Skip to content

Commit b8fad99

Browse files
committed
[DATETIME]: added basic functionality
Signed-off-by: ashish <[email protected]>
1 parent 4fb8376 commit b8fad99

File tree

7 files changed

+152
-43
lines changed

7 files changed

+152
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ OPTIONS
127127
-n, --name=name name to print
128128
```
129129

130-
_See code: [src/commands/date.ts](https://github.com/codingtools/cdt/blob/v0.1.4/src/commands/date.ts)_
130+
_See code: [src/commands/datetime.ts](https://github.com/codingtools/cdt/blob/v0.1.4/src/commands/date.ts)_
131131

132132
## `cdt hash [STRING]`
133133

package-lock.json

Lines changed: 8 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"jshashes": "^1.0.7",
2525
"minify": "^4.1.3",
2626
"moment": "^2.24.0",
27+
"moment-timezone": "^0.5.27",
2728
"nyc": "^14.1.1",
2829
"ora": "^4.0.2",
2930
"signale": "^1.4.0",

src/commands/date.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/commands/datetime.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import {Command, flags} from '@oclif/command'
2+
import * as moment from 'moment'
3+
4+
// @ts-ignore
5+
moment.suppressDeprecationWarnings = true
6+
7+
import Logger from '../utilities/logger'
8+
9+
export default class Datetime extends Command {
10+
static description = 'Date and Time utility'
11+
12+
static defaultFormat = 'Do MMMM YYYY, h:m:s A, Z UTC'
13+
14+
static flags = {
15+
help: flags.help({char: 'h'}),
16+
date: flags.string({char: 'd', description: 'Datetime provided, could also be passed without -d flag'}),
17+
format: flags.string({char: 'f', description: 'Datetime format'}),
18+
timezone: flags.string({char: 'z', description: 'Timezone for Datetime'}),
19+
locale: flags.string({char: 'l', description: 'Locale, default: en'}),
20+
}
21+
22+
static args = [{name: 'date'}]
23+
24+
//arguments 'date' ( default now() ), 'format' ( default 'dd-MMM-YYYY' ), 'timezone' (default user local)
25+
async run() {
26+
const {args, flags} = this.parse(Datetime)
27+
28+
args.date = this.getDateString(flags, args) // getting date object
29+
args.locale = this.getLocale(flags, args) // getting date object
30+
args.format = this.getFormat(flags, args) // getting date object
31+
32+
Logger.info(this, `Input String: ${args.date}`)
33+
Logger.info(this, `Locale: ${args.locale}`)
34+
Logger.info(this, `Format: ${args.format}`)
35+
36+
args.momentDate = this.getMomentDate(flags, args)
37+
this.checkParameters(flags, args)
38+
39+
Logger.success(this, `${args.momentDate.format(args.format)}`)
40+
41+
}
42+
43+
// tslint:disable-next-line:no-unused
44+
private getLocale(flags: any, args: any) {
45+
if (flags.locale)
46+
return flags.locale
47+
else
48+
return 'en'
49+
}
50+
51+
private getDateString(flags: any, args: any) {
52+
let dateString: string | undefined
53+
54+
if (args.date)
55+
dateString = args.date
56+
else if (flags.date)
57+
dateString = flags.date
58+
else
59+
dateString = undefined // will be set to now()
60+
61+
return dateString
62+
}
63+
64+
private getMomentDate(flags: any, args: any) {
65+
let date: moment.Moment
66+
67+
if (args.date)
68+
date = moment(args.date)
69+
else
70+
date = moment([]) // return now() if args.date is undefined
71+
72+
// set locale
73+
date.locale(flags.locale)
74+
return date
75+
}
76+
77+
// tslint:disable-next-line:no-unused
78+
private checkParameters(flags: any, args: any) {
79+
if (!args.momentDate.isValid())
80+
Logger.error(this, 'Invalid Date String Passed')
81+
}
82+
83+
// tslint:disable-next-line:no-unused
84+
private getFormat(flags: any, args: any) {
85+
return flags.format ? flags.format : Datetime.defaultFormat
86+
}
87+
}

test/commands/date.test.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/commands/datetime.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {expect, test} from '@oclif/test'
2+
3+
describe('datetime', () => {
4+
test
5+
.stdout()
6+
.command(['datetime', '01-Jul-2019'])
7+
.it('If locale is not given, default - en', ctx => {
8+
expect(ctx.stdout).to.contain('1st July 2019, 12:0:0 AM, +05:30 UTC')
9+
})
10+
11+
test
12+
.stdout()
13+
.command(['datetime', '01-Jul-2019', '-l', 'fr'])
14+
.it('If locale is given for french', ctx => {
15+
expect(ctx.stdout).to.contain('1er juillet 2019, 12:0:0 AM, +05:30 UTC')
16+
})
17+
18+
test
19+
.stdout()
20+
.command(['datetime', '01-Jul-2019', '-l', 'hi'])
21+
.it('If locale is given for hindi', ctx => {
22+
expect(ctx.stdout).to.contain('१ जुलाई २०१९, १२:०:० रात, +०५:३० UTC')
23+
})
24+
25+
test
26+
.stdout()
27+
.command(['datetime', 'not a real date'])
28+
.exit(0)
29+
.it('Invalid Datetime or Datetime format with args', ctx => {
30+
expect(ctx.stdout).to.contain('Invalid Datetime or Datetime format')
31+
})
32+
33+
test
34+
.stdout()
35+
.command(['datetime', '20190501', '-f', 'DD-MM-YYYY'])
36+
.it('Format Date with a valid Format', ctx => {
37+
expect(ctx.stdout).to.contain('01-05-2019')
38+
})
39+
40+
test
41+
.stdout()
42+
.command(['datetime', '201901d'])
43+
.exit(0)
44+
.it('Invalid Date passed', ctx => {
45+
expect(ctx.stdout).to.contain('Invalid Date String Passed')
46+
})
47+
48+
test
49+
.stdout()
50+
.command(['datetime', '-d', 'not a real date'])
51+
.exit(0)
52+
.it('Invalid Datetime or Datetime format with flags', ctx => {
53+
expect(ctx.stdout).to.contain('Invalid Datetime or Datetime format')
54+
})
55+
})

0 commit comments

Comments
 (0)