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

parse timestamps (number) to Dates #364

Closed
rcnavas opened this issue May 20, 2014 · 15 comments
Closed

parse timestamps (number) to Dates #364

rcnavas opened this issue May 20, 2014 · 15 comments

Comments

@rcnavas
Copy link

rcnavas commented May 20, 2014

It would be nice if jq can parse timestamps (in milliseconds since Epoch) to Dates.
for example:

input:
{ "created": 1405663200000 }

command:
jq '.created | @Date("yyyy-mm-dd")'

output:
"2014-07-18"

@ghost
Copy link

ghost commented May 20, 2014

It could probably be implemented as a native date filter that could convert between date formats, like .created | date("original format string";"desired format string")

@perbergland
Copy link

Yes please

@nicowilliams
Copy link
Contributor

Yeah, strftime()/strptime()-based utility functions for jq would be nice. I'll add this to the todo list. Thanks for the idea.

@logankoester
Copy link

Would like to have this also.

@nicowilliams
Copy link
Contributor

And today I happen to need this badly too.

@esamattis
Copy link

Would it also parse ISO timestamps like 2015-03-05T19:40:53.324Z? Eg. what you get from javascript new Date().toISOString().

@ghost
Copy link

ghost commented Mar 5, 2015

Well, it's not pretty, but you can now do this with regex to some extent. A nice thing to do is to parse the date to an array, as in [2015, 3, 5, 19, 40, 53, 324], so that the lexicographic order sorts it correctly.

@esamattis
Copy link

@nicowilliams awesome!

@nicowilliams
Copy link
Contributor

nicowilliams commented Mar 6, 2015 via email

@pkoppstein
Copy link
Contributor

@nicowilliams -- Please note:

(1) On a Mac (Yosemite with Xcode), HAVE_STRPTIME is not set. Here is the relevant section of config.log:

configure:13891: checking for strptime
configure:13919: gcc -o conftest -g -O2   conftest.c     -lonig >&5
conftest.c:32:11: error: too few arguments to function call, expected 3, have 1
strptime(0)
~~~~~~~~  ^
/usr/include/time.h:117:1: note: 'strptime' declared here
char *strptime(const char * __restrict, const char * __restrict, struct tm * __restrict) __DARWIN_ALIAS(strptime);
^

(2) The error message (strptime/2 not implemented on this platform) can be very misleading, as a jq user might think it is referring to the jq filter named strptime.

@nicowilliams
Copy link
Contributor

@pkoppstein Can you check the date branch of my github clone of jq? It should fix that and add more builtins.

@pkoppstein
Copy link
Contributor

@nicowilliams - I only had time to run your manual.yml test cases as detailed below.

The good news is that there were no failures; the bad news is that in all three cases, the results differ from the advertized output values.

./jq -n '"2015-03-05T23:51:47Z" | fromdate'
1425599507               # i.e. not an array

./jq -c 'strptime("%Y-%m-%dT%H:%M:%SZ")'
"2015-03-05T23:51:47Z"
[2015,2,5,23,51,47,0,0]  # vs output: ['[2015,2,5,23,51,47,4,63]']

./jq 'strptime("%Y-%m-%dT%H:%M:%SZ")|mktime'
"2015-03-05T23:51:47Z"
[2015,2,5,23,51,47,0,0]  # vs output: ['1425621107']

@joelpurra
Copy link
Contributor

I can see this being a package rather than in core.

@nicowilliams
Copy link
Contributor

@pkoppstein Oops, fixed.

@joelpurra Yes, indeed, but having C-coded functions in third-party modules is quite a ways off, and these are super convenient.

I was really annoyed at POSIX though. I'd completely forgotten about the sheer awfulness of mktime(), which conflates two completely different purposes and has fairly shocking side-effects, plus no standard and thread-safe way to correct for the timezone correction that it wants to apply. Plus WIN32 and WIN64 lack a strptime().

On the other hand, to deal just with ISO 8601, one could just parse and format dates in entirely jq-coded functions. But then there are the pesky leap years and leap seconds that one would have to very carefully code around. Calling the C library to handle these is very convenient, not least because any bugs can then be blamed on someone else!

@nicowilliams
Copy link
Contributor

I suppose the docs should say something about localization, given the use of strp/ftime format strings... Up until now jq has completely avoided the subject of localization. If we ever add something like a printf() (which has been requested, and with good reasons), we may have to say something about localization there as well (e.g., for number formatting). Mostly I want to say that jq is agnostic as to localization, and explain where the host OS might do some that is visible to the jq user.

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

7 participants