Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom timestamp format #325

Closed
ryan-ju opened this issue Feb 4, 2019 · 17 comments
Closed

Allow custom timestamp format #325

ryan-ju opened this issue Feb 4, 2019 · 17 comments

Comments

@ryan-ju
Copy link

ryan-ju commented Feb 4, 2019

It would be great to allow the user to specify a timestamp formatter, maybe using the moment.js standard.

This will also allow the user to get parts of the timestamp, like the hour, the minutes, etc.

@Huachao
Copy link
Owner

Huachao commented Feb 11, 2019

@ryan-ju we already support the timestamp with datetime offset, does this fit for you?

@cmjchrisjones
Copy link

Sorry to jump on the back of this, is there a way, using {{$datetime}} that we can specify the format of the output? Currently, if I use

"{{$datetime iso8601 10 d}}", this returns 17/05/2019 15:03:38, however, I would like to get this so it follows the yyyy-MM-dd format, I've tried {{$datetime iso8601 10 d d-m-y}} and variations on that, however don't seem to be able to get it to work?

@connelhooley
Copy link
Contributor

It would be nice if we could do the following:

"{{$datetime yyy-MM-dd 10 d}}"

To generic a date 10 days in the future with a custom format. But as far as I can see that isn't supported.

@Huachao
Copy link
Owner

Huachao commented May 8, 2019

"{{$datetime iso8601 10 d}}", this returns 17/05/2019 15:03:38

@cmjchrisjones can you confirm that the {{$datetime iso8601 10 d}} returns 17/05/2019 15:03:38, in my local test, this returns 2019-05-18T02:00:44.368Z

@cmjchrisjones
Copy link

Hi @Huachao, indeed {{$datetime iso8601 10 d}} does return a date that is 10 days further from now, however it is in the format of dd/MM/yyyy hh:mm:ss.

It would, however, be nice we could choose the format of the output for this, for example an API I'm trying to call will only accept a date rather than a timestamp, and the date format needs to be in the yyyy-MM-dd format without any time information.

@Huachao
Copy link
Owner

Huachao commented May 8, 2019

@cmjchrisjones I still want to know how can you find out that the {{$datetime iso8601 10 d}} returns the date time in the format of dd/MM/yyyy hh:mm:ss. Thanks

@cmjchrisjones
Copy link

@Huachao in the API I'm calling (which is private, not public so I can't share) its returning an error "message": "Supplied value could not be accepted. Supplied date '18/05/2019 09:34:21' is invalid, ensure to supply in yyyy-MM-dd format. when supplying "{{$datetime iso8601 10 d}}" as the value, this message is coming from an internal API.

I was also doing a bit of research and stumbled across this, as some further rationale: point 5 here, which says that you shouldn't use time if you don't need it.

@Huachao
Copy link
Owner

Huachao commented May 8, 2019

@cmjchrisjones can you try to replace the datetime variable with the following text 2019-05-18T02:00:44.368Z and check the response of your internal api?

@cmjchrisjones
Copy link

@Huachao as requested, tried sending in
{"value": "2019-05-18T02:00:44.368Z"} as requested, still the same error:
{ "message": "Supplied value could not be accepted. Supplied date '18/05/2019 02:00:44' is invalid, ensure to supply in yyyy-MM-dd format." }

@Huachao
Copy link
Owner

Huachao commented May 8, 2019

@cmjchrisjones from the error message we can find out that the datetime variable doesn't behave incorrectly, since the string you replaced with is the actual format that datetime variable generated. So the error message of your internal api is not defined quite well, BTW you should try to figure out what's the expected date time format, I guess your api doesn't allow the time part, only wants the date part 😉

@cmjchrisjones
Copy link

@cmjchrisjones from the error message we can find out that the datetime variable doesn't behave incorrectly, since the string you replaced with is the actual format that datetime variable generated. So the error message of your internal api is not defined quite well, BTW you should try to figure out what's the expected date time format, I guess your api doesn't allow the time part, only wants the date part 😉

Indeed, the expected format of our API is yyyy-MM-dd so isn't what the current implementation provides unfortunately. This would be a nice feature to be able to specify the format that gets returned from the timestamp and datetime variables if possible

@Huachao
Copy link
Owner

Huachao commented May 8, 2019

@cmjchrisjones totally agree, it's very useful for some cases

@connelhooley
Copy link
Contributor

I can raise a proper PR later, but I'm thinking the following changes will do it inside registerDateTimeVariable:

Replace line 99:

return { value: type === 'rfc1123' ? date.toString() : date.toISOString() };

With:

if (type === 'rfc1123') {
    return { value: date.toString() };
} else if (type === 'iso8601') {
    return { value: date.toISOString() };
} else {
    return { value: date.format(type) };
}

We'll then have to expand the regex a little bit to allow for custom formats. So:

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

Becomes:

private readonly datetimeRegex: RegExp = new RegExp(`\\${Constants.DateTimeVariableName}\\s(.*)(?:\\s(\\-?\\d+)\\s(y|Q|M|w|d|h|m|s|ms))?`);

I'll need to get my local version running to verify the changes, but @Huachao are these changes you'd accept in a PR?

@Huachao
Copy link
Owner

Huachao commented May 8, 2019

@connelhooley I will be very happy that you can create a PR for this feature

@connelhooley
Copy link
Contributor

Hi @Huachao I've raised a PR: #361

I haven't worked on a VS Code extension before so I haven't ran my changes. Is there a good guide anywhere for me to get the code running on my machine? If not would it be possible for you to run my branch and test it when you have time?

@Huachao
Copy link
Owner

Huachao commented May 10, 2019

@ryan-ju @cmjchrisjones with the weird PR from @connelhooley , you could expect to specify custom datetime format in next release. @connelhooley thanks, great work.

@Huachao Huachao closed this as completed May 10, 2019
@Huachao
Copy link
Owner

Huachao commented May 16, 2019

@ryan-ju @cmjchrisjones you can experience this feature in latest version 0.21.3 with the fantastic PR from @connelhooley

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants