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 |
|
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¶
- Converting Epoch to Human-Readable Date:
-
You can take an epoch value (in seconds or milliseconds) and convert it to a formatted date string using the
inDateFormat="epoch"
and a desiredoutDateFormat
. -
Converting Date to Epoch:
- You can take a standard datetime value and convert it to epoch time by specifying
outDateFormat="epoch"
oroutDateFormat="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.