Home / Stream/ Reference· Cribl Expressions/C.Time

C.Time – Time Methods

C.Time.adjustTZ()

Time.adjustTZ(epochTime: number, tzTo: string, tzFrom: string = 'UTC'): number | undefined

Adjusts a timestamp from one timezone to another.

Returns the adjusted timestamp, in UNIX epoch time (ms).

ParameterTypeDescription
epochTimenumberTimestamp to adjust, in UNIX epoch time.
tzTostringTimezone to adjust to (full name).
tzFrom (optional)stringTimezone of the timestamp.

Cribl relies on this list of TZ Database Time Zones.

Note that these are timezone identifiers (full names), not abbreviations such as “CET”, “PST”, “EST”, and so on.

Examples

To adjust a timestamp found in the _time field to a selected timezone (here, Australia/Melbourne), use:

C.Time.adjustTZ(_time, "Australia/Melbourne")

The method returns the timestamp in milliseconds. If you want to further operate on it using methods such as strftime(), which takes as parameter UNIX epoch time in seconds, you can divide it by 1000:

C.Time.strftime(C.Time.adjustTZ(_time, "Australia/Melbourne") / 1000, "%d %B %Y %H %M %Z" )

C.Time.clamp()

Time.clamp(date: T, earliest: T, latest: T, defaultDate?: T ): T  | undefined

Constrains a parsed timestamp to realistic earliest and latest boundaries.

Returns a JavaScript Date object or timestamp, depending on the format of the date parameter.

ParameterTypeDescription
dateDate | numberTimestamp to constrain, in UNIX epoch time (ms) or JavaScript Date format.
earliestDate | numberEarliest allowable timestamp, in UNIX epoch time (ms) or JavaScript Date format.
latestDate | numberLatest allowable timestamp, in UNIX epoch time (ms) or JavaScript Date format.
defaultDate (optional)Date | numberDefault date, in UNIX epoch time (ms) or JavaScript Date format. This date is used for values that fall outside the earliest or latest boundaries.

Examples

Suppose you have a date in a “YYYY-MM-DD” format in a time field. To make sure this date does not exceed the range of 1 Jan 2020 – 1 Jan 2025, you can use:

C.Time.clamp(time, "2020-01-01", "2025-01-01")
InputOutput
“2016-02-11”“2020-01-01” (set to the earliest value)
“2026-01-01”“2025-01-01” (set to the latest value)
“2023-12-24”“2023-12-24” (unchanged, because it fits inside the defined range)

To make sure that all dates that fall outside the bounds are changed to a specific value, add the third parameter:

C.Time.clamp(time, "2020-01-01", "2025-01-01", "2022-02-22")
InputOutput
“2016-02-11”“2022-02-22” (set to the default value)
“2026-01-01”“2022-02-22” (set to the default value)
“2023-12-24”“2023-12-24” (unchanged, because it fits inside the defined range)

C.Time.strftime()

Time.strftime(date: number | Date | string, format: string, utc: boolean = true): string | undefined

Formats a Date object or timestamp number as a time string, using d3js time format.

Returns representation of the given date in the specified format.

ParameterTypeDescription
datenumber | Date | stringDate to format, in UNIX epoch time (in seconds) or JavaScript Date format.
formatstringNew format for the date.
utc (optional)booleanWhether to output the time in UTC (true), rather than in local timezone.

Examples

To transform a _time field containing a UNIX timestamp to a string with time formatted, for example: 15 November 2023, 08:03, you can use:

C.Time.strftime(_time, "%d %B %Y, %H:%M")

Some common formats (assuming UNIX timestamp of 1702460134) are:

FormatOutput
“%Y-%m-%d” (ISO date)“2023-12-13”
“%d %B %Y %H:%M:%S”“13 December 2023 09:35:34”
“%x %X” (the locale’s date and time format)“12/13/2023 9:35:34 AM”

For detailed reference of the date and time tokens, see d3js reference or Auto Timestamp.

C.Time.strptime()

Time.strptime(str: string, format: string, utc: boolean = true, strict: boolean = false): Date

Extracts time from a string using d3js time format that defines the format of the incoming string.

Returns a parsed JavaScript Date object, or null if the specified format did not match.

ParameterTypeDescription
strstringTimestamp to parse.
formatstringFormat to parse.
utc (optional)booleanWhether to interpret times as UTC (true), rather than as local time.
strict (optional)booleanWhether to return null if there are any extra characters after timestamp.

Examples

To convert a string in the “YYYY-MM-DD” format, for example “2023-12-12”, to a Date object, use:

C.Time.strptime(F, "%Y-%m-%d")

Additionally, to interpret the time in UTC, and make sure null is returned if the timestamp contains any trailing characters, use:

C.Time.strptime(F, "%Y-%m-%d", true, true)

For detailed reference of the date and time tokens, see d3js reference or Auto Timestamp.

C.Time.timestampFinder()

Time.timestampFinder(utc?: boolean).find(str: string): IAutoTimeParser

Extracts time from the specified field, using the same algorithm as the Auto Timestamp Function and the Event Breaker Function.

Returns representation of the extracted time; truncates timestamps to three-digit (milliseconds) resolution, omitting trailing zeros.

ParameterTypeDescription
utcbooleanWhether to output the time in UTC (true), rather than in local timezone.
strstringThe field in which to search for the time.

Examples

To find timestamps in the _raw field and output them in UTC, you can use:

C.Time.timestampFinder(true).find(_raw)

You can then format the string output with strftime():

C.Time.strftime(C.Time.timestampFinder(true).find(_raw), "%d.%m.%Y, %H.%M")

C.Time.timePartition()

Time.timePartition(date: number | Date | string, level: 'd' | 'h'): string | undefined 

Provides the part of the file path prefix that contains the date.

Returns a string in the format YYYY/MM/DD for d and YYYY/MM/DD/HH for h.

ParameterTypeDescription
datenumber | Date | stringDatetime to extract the file path prefix for.
levelstringGranularity of the partitioning, d for daily, and h for hourly.

Examples

To get the file path prefix from the _time field:

C.Time.timePartition(_time)

This will return the prefix in the form of, for example: 2024/04/23.

C.Time.s3TimePartition()

Time.s3TimePartition(date: number | Date | string, level: 'd' | 'h'): string | undefined 

Provides the full S3 file path prefix for a given datetime.

Returns the file path prefix as a string.

ParameterTypeDescription
datenumber | Date | stringDatetime to extract the file path prefix for.
levelstringGranularity of the partitioning, d for daily, and h for hourly.

Examples

To get the S3 file path prefix from the _time field:

C.Time.s3TimePartition(_time)

This will return the prefix in the form of, for example: hjhg-2024/aj-04/ai-23.