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

Add a function to create a datetime from a Timeable #4749

Closed
onelson opened this issue May 12, 2022 · 0 comments · Fixed by #4867
Closed

Add a function to create a datetime from a Timeable #4749

onelson opened this issue May 12, 2022 · 0 comments · Fixed by #4867
Assignees

Comments

@onelson
Copy link
Contributor

onelson commented May 12, 2022

Today, when v.startTimeRange is supplied to flux, it can be either a duration or datetime.

This can cause trouble for script authors who need to produce a datetime string from this value without knowing specifically which type the script will receive.

Use case

A use case for this is when you have a script that needs a datetime regardless of how the v.startTimeRange is specified.

As a concrete example, if you were calling sql.from() and needed to string interpolate this value as ISO 8601 in your query to limit the returned rows, your script could only be written to support either relative (duration-based) values, or explicit datetime values.

The script would need to be tweaked if your change the time range drop down in the UI, for example.

Proposal

Providing a function that can receive any Timeable and produce a datetime could solve for this situation.

package date

builtin time : (t: A) => time where A: Timeable

Example:

import "date"

// normalize to datetimes regardless of incoming types
start = date.time(t: v.startTimeRange)
end = date.time(t: v.endTimeRange)

sql.from(
  ...,
  // cast to ISO 8601 for use with SQL...
  query: "SELECT * from events WHERE timestamp >= '${start}' AND timestamp < '${end}'"
) |> // snip...

This also makes some sense for use with JSON APIs.

Note: there's a getTimeableTime function in stdlib/date/date.go which can be used to this aim.

@onelson onelson changed the title Add a function to create a datetime from a Timeable. Add a function to create a datetime from a Timeable May 12, 2022
Rahul-Vasan added a commit to Rahul-Vasan/flux that referenced this issue Jun 7, 2022
…any timeable into datetime (influxdata#4749)

This feature addition now addresses the need for a function as part of the date package to be able to convert any Timeable into a datetime.

fixes: influxdata#4749
Rahul-Vasan added a commit that referenced this issue Jun 10, 2022
…any timeable into datetime (#4749)

This feature addition now addresses the need for a function as part of the date package to be able to convert any Timeable into a datetime.

fixes: #4749
Rahul-Vasan added a commit that referenced this issue Jun 14, 2022
…any timeable into datetime (#4749) (#4867)

* feat(date): add builtin function time to the date package to convert any timeable into datetime (#4749)

This feature addition now addresses the need for a function as part of the date package to be able to convert any Timeable into a datetime.

fixes: #4749

* fix: modified buildinfo

Modified buildinfo.gen.go to resolve the error with make checkgenerate

* refactor: replaced _value with _time for readability

* fix: Add documentation for location parameter

Added documentation for location parameter in the date.flux file. This fixes make checkdocs

* fix: modify buildinfo

Modified buildinfo.gen.go to resolve the error with make checkgenerate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment