Skip to content

Date and Time Generators

DATAMIMIC provides powerful generators for creating date and time values in various formats. These generators allow you to generate dates, times, and timestamps with customizable input and output formats.

DateTimeGenerator

The DateTimeGenerator generates date values that include both date and time components. It allows you to specify input and output formats using inDateFormat and outDateFormat attributes respectively. The DateTimeGenerator now also supports epoch time conversion (in both seconds and milliseconds).

Example Usage

The following example demonstrates how to use the DateTimeGenerator to generate various date and time formats, including conversions from and to epoch time:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<generate name="InOutDatetimeFormat" count="10" >
    <key name="datetime_with_in" constant="2021-02-01 12:08:04.376188" inDateFormat="%Y-%m-%d %H:%M:%S.%f"/>
    <key name="datetime_with_out" generator="DateTimeGenerator" outDateFormat="%d.%m.%Y %H:%M:%S.%f"/>
    <key name="date_with_in_out" constant="2022-02-01" inDateFormat="%Y-%m-%d" outDateFormat="%d.%m.%Y"/>
    <key name="day_out" generator="DateTimeGenerator" outDateFormat="%d"/>
    <key name="month_out" generator="DateTimeGenerator" outDateFormat="%m"/>
    <key name="year_out" generator="DateTimeGenerator" outDateFormat="%Y"/>
    <key name="time_out" generator="DateTimeGenerator" outDateFormat="%H:%M:%S"/>
    <key name="epoch_in_seconds" constant="1692967891" inDateFormat="epoch" outDateFormat="%Y-%m-%d %H:%M:%S"/>
    <key name="epoch_in_milliseconds" constant="1692967891000" inDateFormat="epoch" outDateFormat="%Y-%m-%d %H:%M:%S"/>
    <key name="to_epoch" generator="DateTimeGenerator" outDateFormat="epoch"/>
    <key name="to_epoch_millis" generator="DateTimeGenerator" outDateFormat="epoch_millis"/>
    <key name="to_epoch_micros" generator="DateTimeGenerator" outDateFormat="epoch_micros"/>
    <key name="to_epoch_nanos" generator="DateTimeGenerator" outDateFormat="epoch_nanos"/>
    <key name="to_datetime_3millis" generator="DateTimeGenerator" outDateFormat="%Y-%m-%d %H:%M:%S.%3f"/>
    <key name="to_datetime_6millis" generator="DateTimeGenerator" outDateFormat="%Y-%m-%d %H:%M:%S.%f"/>
</generate>

Explanation

  • datetime_with_in: Takes a constant date and time value in the format "%Y-%m-%d %H:%M:%S.%f"
  • datetime_with_out: Generates a date and time value using the DateTimeGenerator and outputs it in the format "%d.%m.%Y %H:%M:%S.%f".
  • date_with_in_out: Takes a constant date value and converts it from the input format "%Y-%m-%d" to the output format "%d.%m.%Y".
  • day_out: Generates just the day component of the current date using the format "%d".
  • month_out: Generates just the month component of the current date using the format "%m".
  • year_out: Generates just the year component of the current date using the format "%Y".
  • time_out: Generates the current time using the format "%H:%M:%S".
  • epoch_in_seconds: Takes a constant epoch value in seconds (1692967891) and converts it into a human-readable datetime format ("%Y-%m-%d %H:%M:%S").
  • epoch_in_milliseconds: Takes a constant epoch value in milliseconds (1692967891000) and converts it into a human-readable datetime format ("%Y-%m-%d %H:%M:%S").
  • to_epoch: Converts the generated date and time to epoch time in seconds.
  • to_epoch_millis: Converts the generated date and time to epoch time in milliseconds.
  • to_epoch_micros: Converts the generated date and time to epoch time in microseconds.
  • to_epoch_nanos: Converts the generated date and time to epoch time in nanoseconds.
  • to_datetime_3millis: Generates a date and time value with a precision of milliseconds with 3 digits ("%Y-%m-%d %H:%M:%S.%3f").

Epoch Time Support

The DateTimeGenerator now supports the conversion of epoch time both as input and output:

  • Input as Epoch:
  • If inDateFormat is "epoch", the input is treated as an epoch time in seconds or milliseconds. The generator will detect the format based on the input value (if it's longer than 10 digits, it's treated as milliseconds).

  • Output as Epoch:

  • If outDateFormat is "epoch", the generator will return the date as the number of seconds since the Unix epoch (January 1, 1970).
  • If outDateFormat is "epoch_millis", the generator will return the date as the number of milliseconds since the Unix epoch.
  • If outDateFormat is "epoch_micros", the generator will return the date as the number of microseconds since the Unix epoch.
  • If outDateFormat is "epoch_nanos", the generator will return the date as the number of nanoseconds since the Unix epoch.

Example Use Cases

  1. Converting Epoch to Human-Readable Date:
  2. You can take an epoch value (in seconds or milliseconds) and convert it to a formatted date string using the inDateFormat="epoch" and a desired outDateFormat.

  3. Converting Date to Epoch:

  4. You can take a standard datetime value and convert it to epoch time by specifying outDateFormat="epoch" or outDateFormat="epoch_millis".

These extended capabilities provide greater flexibility for handling timestamps and date formats, making it easier to work with both standard date formats and Unix epoch time.