Skip to content

Commit

Permalink
Support custom display formats in datetime system variable (#361)
Browse files Browse the repository at this point in the history
* Added support for custom date formats

* Updated README

* Minor tweak
  • Loading branch information
connelhooley authored and Huachao committed May 10, 2019
1 parent c4df204 commit 86a2fc1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ System variables provide a pre-defined set of variables that can be used in any
* `{{$guid}}`: Add a RFC 4122 v4 UUID
* `{{$randomInt min max}}`: Returns a random integer between min (included) and max (excluded)
* `{{$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}}`.
* `{{$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.
* `{{$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.

The option string you can specify in `timestamp` and `datetime` are:
The offset options you can specify in `timestamp` and `datetime` are:

Option | Description
------ | -----------
Expand All @@ -479,7 +479,8 @@ Date: {{$datetime rfc1123}}
"request_id": "{{$guid}}",
"updated_at": "{{$timestamp}}",
"created_at": "{{$timestamp -1 d}}",
"review_count": "{{$randomInt 5, 200}}"
"review_count": "{{$randomInt 5, 200}}",
"custom_date": "{{$datetime 'YYYY-MM-DD'}}"
}
```
> 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)
Expand Down
10 changes: 8 additions & 2 deletions src/utils/httpVariableProviders/systemVariableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class SystemVariableProvider implements HttpVariableProvider {
private readonly resolveFuncs: Map<string, ResolveSystemVariableFunc> = new Map<string, ResolveSystemVariableFunc>();

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

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

return { value: type === 'rfc1123' ? date.toString() : date.toISOString() };
if (type === 'rfc1123') {
return { value: date.toString() };
} else if (type === 'iso8601') {
return { value: date.toISOString() };
} else {
return { value: date.format(type.slice(1, type.length - 2)) };
}
}

return { warning: ResolveWarningMessage.IncorrectDateTimeVariableFormat };
Expand Down

0 comments on commit 86a2fc1

Please sign in to comment.