khiops icon indicating copy to clipboard operation
khiops copied to clipboard

Feature Request: Timestamp Conversion Rules for Numeric and String Formats

Open n-voisine opened this issue 9 months ago • 2 comments

Description

It would be beneficial to have built-in Rules for converting timestamps between string format ("YYYY-MM-DD HH:MM:SS") and numeric format using different epoch origins (1970-01-01 or 2000-01-01). Proposed Features

String to Numeric Conversion

    A function that converts a timestamp string ("YYYY-MM-DD HH:MM:SS") into a numeric value.

    Allow selection of the epoch origin:

        UNIX time (seconds since 1970-01-01 00:00:00 UTC)

        Alternative epoch (seconds since 2000-01-01 00:00:00 UTC)

Numeric to String Conversion

    A function that converts a numeric timestamp (based on 1970 or 2000) back to a readable "YYYY-MM-DD HH:MM:SS" format.

    Support for selecting the reference epoch.

Example 1: Convert string to numeric timestamp

numeric_ts_unix = convert_to_numeric_unix("2025-03-27 12:00:00") # → 1743086400 numeric_ts_2000 = convert_to_numeric_2000("2025-03-27 12:00:00") # → 789000000

Example 2: Convert numeric timestamp back to string

timestamp_str_unix = convert_to_string_unix(1743086400) # → "2025-03-27 12:00:00" timestamp_str_2000 = convert_to_2000(789000000) # → "2025-03-27 12:00:00"

n-voisine avatar Mar 28 '25 08:03 n-voisine

The DiffTimestamp and AddSeconds timestamps rules should meet this need. cf. https://khiops.org/api-docs/kdic/timestamp-rules/

Examples

In the following example:

  • conversions from timestamp to a numeric value are obtained using the DiffTimestamp rule.
  • conversions from numeric value to a timestamp are obtained using the AddSeconds rule.
  • the RefDateUnix and RefDate2000 are temporary variables, used for readability reasons.
  • the BuildTimestamprule is used for illustration purposes using a constant timestamp value, but a Timepstamp variable could be used directly (ex: DiffTimestamp(MyTimestamp, RefDateUnix)
  • the obtained numerical values are the correct ones (see https://www.epochconverter.com/)
Unused  Timestamp RefDateUnix = BuildTimestamp(BuildDate(1970, 1, 1), BuildTime(0, 0, 0)); 
Unused  Timestamp RefDate2000 = BuildTimestamp(BuildDate(2000, 1, 1), BuildTime(0, 0, 0)); 
	Numerical numeric_ts_unix = DiffTimestamp(BuildTimestamp(BuildDate(2025, 3, 27), BuildTime(12, 0, 0)), RefDateUnix); // -> 1743076800
	Numerical numeric_ts_2000 = DiffTimestamp(BuildTimestamp(BuildDate(2025, 3, 27), BuildTime(12, 0, 0)), RefDate2000); // -> 796392000
	Timestamp timestamp_str_unix = AddSeconds(RefDateUnix, 1743076800); // -> 2025-03-27 12:00:00
	Timestamp timestamp_str_2000 = AddSeconds(RefDate2000, 796392000); // -> 2025-03-27 12:00:00

marcboulle avatar Mar 28 '25 09:03 marcboulle

A transfomer en Q&A

marcboulle avatar Apr 16 '25 09:04 marcboulle