Skip to content

Commit 86a2fc1

Browse files
Connel HooleyHuachao
authored andcommitted
Support custom display formats in datetime system variable (Huachao#361)
* Added support for custom date formats * Updated README * Minor tweak
1 parent c4df204 commit 86a2fc1

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,9 @@ System variables provide a pre-defined set of variables that can be used in any
453453
* `{{$guid}}`: Add a RFC 4122 v4 UUID
454454
* `{{$randomInt min max}}`: Returns a random integer between min (included) and max (excluded)
455455
* `{{$timestamp [offset option]}}`: Add UTC timestamp of now. You can even specify any date time based on current time in the format `{{$timestamp number option}}`, e.g., to represent 3 hours ago, simply `{{$timestamp -3 h}}`; to represent the day after tomorrow, simply `{{$timestamp 2 d}}`.
456-
* `{{$datetime rfc1123|iso8601 [offset option]}}`: Add a datetime string in either _ISO8601_ or _RFC1123_ format. You can even specify any date time based on current time similar to `timestamp` like: `{{$datetime iso8601 1 y}}` to represent a year later in _ISO8601_ format.
456+
* `{{$datetime rfc1123|iso8601|"custom format"|'custom format' [offset option]}}`: Add a datetime string in either _ISO8601_, _RFC1123_ or a custom display format. You can even specify a date time relative to the current date similar to `timestamp` like: `{{$datetime iso8601 1 y}}` to represent a year later in _ISO8601_ format. If specifying a custom format, wrap it in single or double quotes like: `{{$datetime "DD-MM-YYYY" 1 y}}`. The date is formatted using moment.js, read [here](https://momentjs.com/docs/#/parsing/string-format/) for information on format strings.
457457

458-
The option string you can specify in `timestamp` and `datetime` are:
458+
The offset options you can specify in `timestamp` and `datetime` are:
459459

460460
Option | Description
461461
------ | -----------
@@ -479,7 +479,8 @@ Date: {{$datetime rfc1123}}
479479
"request_id": "{{$guid}}",
480480
"updated_at": "{{$timestamp}}",
481481
"created_at": "{{$timestamp -1 d}}",
482-
"review_count": "{{$randomInt 5, 200}}"
482+
"review_count": "{{$randomInt 5, 200}}",
483+
"custom_date": "{{$datetime 'YYYY-MM-DD'}}"
483484
}
484485
```
485486
> More details about `aadToken` (Azure Active Directory Token) can be found on [Wiki](https://github.com/Huachao/vscode-restclient/wiki/Azure-Active-Directory-Authentication-Samples)

‎src/utils/httpVariableProviders/systemVariableProvider.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class SystemVariableProvider implements HttpVariableProvider {
2222
private readonly resolveFuncs: Map<string, ResolveSystemVariableFunc> = new Map<string, ResolveSystemVariableFunc>();
2323

2424
private readonly timestampRegex: RegExp = new RegExp(`\\${Constants.TimeStampVariableName}(?:\\s(\\-?\\d+)\\s(y|Q|M|w|d|h|m|s|ms))?`);
25-
private readonly datetimeRegex: RegExp = new RegExp(`\\${Constants.DateTimeVariableName}\\s(rfc1123|iso8601)(?:\\s(\\-?\\d+)\\s(y|Q|M|w|d|h|m|s|ms))?`);
25+
private readonly datetimeRegex: RegExp = new RegExp(`\\${Constants.DateTimeVariableName}\\s(rfc1123|iso8601|\'.+\'|\".+\")(?:\\s(\\-?\\d+)\\s(y|Q|M|w|d|h|m|s|ms))?`);
2626
private readonly randomIntegerRegex: RegExp = new RegExp(`\\${Constants.RandomIntVariableName}\\s(\\-?\\d+)\\s(\\-?\\d+)`);
2727

2828
private readonly requestUrlRegex: RegExp = /^(?:[^\s]+\s+)([^:]*:\/\/\/?[^/\s]*\/?)/;
@@ -96,7 +96,13 @@ export class SystemVariableProvider implements HttpVariableProvider {
9696
date = utc();
9797
}
9898

99-
return { value: type === 'rfc1123' ? date.toString() : date.toISOString() };
99+
if (type === 'rfc1123') {
100+
return { value: date.toString() };
101+
} else if (type === 'iso8601') {
102+
return { value: date.toISOString() };
103+
} else {
104+
return { value: date.format(type.slice(1, type.length - 2)) };
105+
}
100106
}
101107

102108
return { warning: ResolveWarningMessage.IncorrectDateTimeVariableFormat };

0 commit comments

Comments
 (0)