DateTimeGenerator¶
Generates datetimes for keys and variables with fixed or random values, strict min/max bounds, optional weighted sampling, native offset arithmetic, deterministic seeding, and epoch conversions. This document covers capabilities, API and usage for EE.
Modes¶
- Custom value:
value='<date string>'parsed withinput_format. - Random window:
random=True(orrandom_mode=True) with optionalmin/max. - Current time: omit the above → uses the current system time.
- Offset arithmetic:
offset=...with optionalmax_offsetandreference.
Defaults when no bounds are provided: start=1970-01-01 00:00:00, end=2020-01-01 00:00:00 (50-year span).
Core Parameters¶
min/max(str): Optional bounds parsed usinginput_format(default%Y-%m-%d %H:%M:%S).value(str): Fixed datetime value usinginput_format.random/random_mode(bool): Enable random sampling.input_format(str): Parse format formin,max,value.seed(int): Deterministic RNG seed; same seed + same params → same sequence.
Offset Arithmetic¶
offset(str): Relative duration applied to the base datetime.max_offset(str): Optional upper bound for a sampled offset window.reference(str): Optional lookup-only reference resolved against the current record. Supported forms are bare identifiers likeissue_at_raw,this.<field>,this.parent.<field>, androot.<field>.
Offset examples:
30d-2h1y3mo2w3d1h30m
Offset contract:
offsetonly: resolve one datetime relative to the base.offset + max_offset: sample uniformly between the two resolved datetimes.referenceomitted: use the injected setup clock/current datetime as the base.reference='issue_at_raw': resolve against the current scope before eachgenerate()call.reference='this.parent.issued_at': resolve against the direct parent frame in nested generates.reference='root.issued_at': resolve against the flattened root frame.- Bare
parent.<field>, calls, subscripts, and arbitrary expressions are rejected as invalid datetime references.
Examples:
1 2 3 4 | |
Nested lookup examples:
1 2 | |
For broader nested lookup rules in script=... expressions, see Variable Scoping in Nested Generates. DateTimeGenerator(reference=...) intentionally supports only the lookup-only subset above.
Raw Datetime Rule¶
Datetime arithmetic is raw-only.
- Use variables or unformatted datetime values for downstream arithmetic.
- Once
outDateFormathas been applied, the downstream value is a string. - DATAMIMIC does not auto-parse formatted strings back into datetime values.
- If you accidentally call datetime helper methods on a formatted value, the runtime raises a typed script error and points back to the raw-value rule.
Preferred pattern:
1 2 3 | |
Bounds are respected down to the second. When the sampled day equals the min/max day, hour/minute/second are clamped into the legal sub‑range.
Time Weights (intra‑day)¶
hour_weights(list[24] of float)minute_weights(list[60] of float)second_weights(list[60] of float)
Rules:
- Length must match (24/60/60). Values non-negative and sum > 0 when provided.
- If omitted and no other day-level weighting is used, distribution within the day is uniform.
- Generator constructor parsing preserves quoted weight expressions and sugar strings as written, so declarations such as
month_weights='[0,0,1]+[0]*9', minute_weights='[1 if m in (0,15,30,45) else 0 for m in range(60)]', and
months='3,7-9' remain valid.
Day Selection Weights (inter‑day)¶
month_weights(list[12] of float)weekday_weights(list[7] of float) – Monday=0, Sunday=6dom_weights(list[31] of float) – Day‑of‑month 1..31
How it works:
- The generator enumerates all eligible days in the [min, max] window.
- Per‑day weight = month_weight × weekday_weight × dom_weight.
- A month is drawn proportional to the sum of its days’ weights; then a day is drawn using that month’s day weights.
Validation:
- Correct lengths, non‑negative numbers, and non‑zero total weight are enforced.
- If no day is eligible → clear ValueError.
Sugar (friendly shortcuts)¶
Only applies when explicit weights for that dimension are not provided.
months/months_exclude: restrict months.- Examples:
months='3,7-9',months_exclude='2,4'. - Quarters:
months='Q1'(Jan–Mar),months='Q2,Q4'(Apr–Jun, Oct–Dec). weekdays: names, ranges, presets (Mon=0, Sun=6).- Names/ranges:
weekdays='Mon-Fri',weekdays='Fri-Mon'(wrap‑around supported). - Presets:
weekdays='Weekend',weekdays='Weekdays'/Business/Workdays,weekdays='All'. dom: day‑of‑month filter.- Examples:
dom='1-5,15,31',dom='last'(last day of each month). hours_preset:office(09–17 high),night(22–05 high),flat(no bias).minute_granularity/second_granularity(int): keep only ticks divisible by granularity (e.g.,15→ 0,15,30,45).
Precedence: explicit weights (*_weights) override sugar for that dimension.
Epoch Support¶
There are two ways to work with epoch time:
1) Via key formatting (preferred in XML):
- inDateFormat="epoch" converts a constant epoch (seconds/millis/micros/nanos) to datetime.
- outDateFormat="epoch|epoch_millis|epoch_micros|epoch_nanos|%Nf" converts a datetime to the requested format.
2) Via Python API (generator options):
- as_epoch=True with epoch_unit='seconds'|'milliseconds' returns integer epoch from .generate(); .generate_date() returns date based on the epoch.
Determinism & Performance¶
- Deterministic sampling uses a private RNG with
seed. - Fast path (Rust) is used when there is no
seedand no weights/sugar. A small buffer is prefilled for throughput. - Runtime flags:
rust_fast_generators(default true),rust_fast_buffer_size(default 8192). - Weighted/sugar/deterministic cases run in Python with O(1) per sample after a one‑time day distribution build.
Error Handling¶
- Invalid lengths, negative values, or all‑zero weights →
ValueError. - Offset durations that would exceed Python's supported datetime range →
ValueError. - Sugar that yields no eligible days in the window (e.g.,
months='2'over a January range) →ValueError. - Invalid granularity (≤0 or >60) →
ValueError. - Unknown
hours_preset→ValueError.
XML Examples¶
Formatting and epoch conversions:
1 2 3 4 5 6 7 8 9 | |
Uniform window:
1 2 3 4 5 | |
Time sugar and granularity:
1 2 3 | |
Weighted month/weekday/dom selection:
1 2 3 4 5 | |
Sugar filters:
1 2 3 | |
Offset and reference arithmetic:
1 2 3 4 5 6 7 8 | |
Biased intra‑day and sampled seconds/minutes:
1 2 3 4 5 6 7 8 | |
Notes on XML weight expressions:
- Due to XML parsing, pass list expressions as strings (quote the expression) when they contain commas.
- Simple patterns like [0]*12 work quoted or unquoted. Complex list comprehensions must be quoted, as above.
Python API Examples¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Edge Cases & Tips¶
- Leap years: DOM weights and
dom='last'handle February 29 correctly. - Weekday wrap: ranges like
Fri-Monwrap across week boundaries. - Bounds + granularity: When granularity excludes most ticks, sampling remains within the min/max time range of the chosen day.
- Precedence: Explicit weights override sugar. Combine month/weekday/dom carefully to avoid eliminating all days.
random_modeis an alias ofrandomfor compatibility.