The `std/time` package in CoginitiScript introduces a set of date and time functions, enhancing the ability to handle and manipulate time data within SQL code. This package is instrumental for applications requiring time-related data processing, such as time series analysis, scheduling, or historical data tracking.
Getting Started with `std/time`
To use the `std/time` package, first import it into your CoginitiScript:
#+import "std/time"
This makes the time functions available in your script.
Key Functions and Formats
- time.NowUTC(): Retrieves the current time in UTC format.
- time.Now(tz): Fetches the current time in a specified time zone (`tz`).
- time.Format(time, format): Formats a time value into a specified string format.
Supported Time Formats
`std/time` supports a variety of formats, enabling versatile representation of date and time:
- time.IsoBasicDate Basic ISO date '20111203'
- time.IsoLocalDate ISO Local Date '2011-12-03'
- time.IsoOffsetDate ISO Date with offset '2011-12-03+01:00'
- time.IsoDate ISO Date with or without offset '2011-12-03+01:00'; '2011-12-03'
- time.IsoLocalTime Time without offset '10:15:30'
- time.IsoOffsetTime Time with offset '10:15:30+01:00'
- time.IsoTime Time with or without offset '10:15:30+01:00'; '10:15:30'
- time.IsoLocalDateTime ISO Local Date and Time '2011-12-03T10:15:30' time.IsoOffsetDateTime Date Time with Offset '2011-12-03T10:15:30+01:00'
- time.IsoZonedDateTime Zoned Date Time '2011-12-03T10:15:30+01:00[Europe/Paris]'
- time.IsoDateTime Date and time with ZoneId '2011-12-03T10:15:30+01:00[Europe/Paris]'
- time.IsoOrdinalDate Year and day of year '2012-337'
- time.IsoWeekDate Year and Week '2012-W48-6'
Example Use Case: Dynamic Table Naming
The `std/time` package can be used to dynamically name tables based on the current date, which is particularly useful for versioning data. Here's an example:
#+import "std/time"
#+src sql Foo()
#+meta {
:publication {
:type "table",
:name "published_table_" + time.Format(time.Now(), time.IsoBasicDate)
}
}
#+begin
SELECT 1 AS id, 'John' AS first_name, 'Doe' AS last_name
#+end
In this example, a new table named `published_table_` followed by the current date in ISO Basic Date format is created. This approach is beneficial for creating time-based versions of tables for tracking changes over time.