Skip to content
New issue

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

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

Already on GitHub? # to your account

string of format "YYYY-MM-DD hh:mm" not recognized as DATETIME #11

Open
terefang opened this issue Oct 24, 2023 · 6 comments
Open

string of format "YYYY-MM-DD hh:mm" not recognized as DATETIME #11

terefang opened this issue Oct 24, 2023 · 6 comments

Comments

@terefang
Copy link

the query yields an empty string:

$ usql_static -C -c "select 0,UNIX_TIME('2020-01-01 12:12'),0 " 'csv:./'
Connected with driver csvq (CSVQ v1.18.1)
0,"UNIX_TIME('2020-01-01 12:12')",0
0,,0

$ usql_static -C -c "select 0,DATETIME('2020-01-01 12:12'),0 " 'csv:./'
Connected with driver csvq (CSVQ v1.18.1)
0,"DATETIME('2020-01-01 12:12')",0
0,,0

but this works

$ usql_static -C -c "select 0,DATETIME('2020-01-01 12:12:12'),0 " 'csv:./'
Connected with driver csvq (CSVQ v1.18.1)
0,"DATETIME('2020-01-01 12:12:12')",0
0,2020-01-01T12:12:12+01:00,0
@mithrandie
Copy link
Owner

mithrandie commented Oct 24, 2023

The DATETIME function and other time related functions convert only some forms of strings by default.
If you want to convert other formats, you can specify the format.

ADD '%Y-%m-%d %h:%i' TO @@DATETIME_FORMAT;
SELECT DATETIME('2020-01-01 12:12');
SELECT UNIX_TIME('2020-01-01 12:12');

Placeholders are the same as used in the DATETIME_FORMAT function.

@terefang
Copy link
Author

The DATETIME function and other time related functions convert only some forms of strings by default. If you want to convert other formats, you can specify the format.

ADD '%Y-%m-%d %h:%i' TO @@DATETIME_FORMAT;
SELECT DATETIME('2020-01-01 12:12');
SELECT UNIX_TIME('2020-01-01 12:12');

Placeholders are the same as used in the DATETIME_FORMAT function.

oh ... did not notice you can add datetime formats to the parser

will this also work if two or more different formats are used in the dataset ?

can multiple format specs added to @@DATETIME_FORMAT ?

@terefang
Copy link
Author

btw ... cheers for such a nice lib 👍

@terefang
Copy link
Author

terefang commented Oct 25, 2023

just for your understanding i was comparing csvq funtionality to the sqlite ones

sqlite has support for 12 different formats by default https://www.sqlite.org/lang_datefunc.html#time_values

c.

@mithrandie
Copy link
Owner

Multiple formats can be added by executing multiple ADD TO statements.

@terefang
Copy link
Author

looks like i am wrong in this repo, then csvq repo would be more proper ... anyway:

in github.com/mithrandie/csvq/lib/value/conv.go the following changes need to be made:

on line 85 from:

if t, e := time.ParseInLocation("2006-01-02T15:04:05.999999999", s, location); e == nil {
	return t, true
}

to:

if t, e := time.ParseInLocation("2006-01-02T15:04:05.999999999", s, location); e == nil {
	return t, true
} else if t, e := time.ParseInLocation("2006-01-02T15:04:05", s, location); e == nil {
	return t, true
} else if t, e := time.ParseInLocation("2006-01-02T15:04", s, location); e == nil {
	return t, true
}

and on line 90 from:

if t, e := time.ParseInLocation("2006-01-02 15:04:05.999999999", s, location); e == nil {
	return t, true
}

to:

if t, e := time.ParseInLocation("2006-01-02 15:04:05.999999999", s, location); e == nil {
	return t, true
} else if t, e := time.ParseInLocation("2006-01-02 15:04:05", s, location); e == nil {
	return t, true
} else if t, e := time.ParseInLocation("2006-01-02 15:04", s, location); e == nil {
	return t, true
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants