Timestamp Normalizer
Prerequisites: Time, Timestamps, and Clocks, Integer Ticks and Price Representation
A strategy trading AAPL at the open receives events from several sources, each with its own timestamp format. To compare events, normalize every timestamp to the representation from Time, Timestamps, and Clocks: an std::int64_t count of nanoseconds since the Unix epoch, 1970-01-01T00:00:00 UTC.
- Exchange feed: writes a calendar date and time of day in its local time zone.
- Feed handler: writes integer nanoseconds since the epoch.
- Order gateway: writes integer microseconds.
What to implement
process_commands(commands)returns the printed lines in order.- Each command is one line. Commands are numbered from 1 in input order; a command's number is its
line. - On stdin, the first line is the number of commands, and each remaining line is one command. The count line is not a command, so the first command's
lineis 1. - There are three parts. Each adds commands or output to the previous one. One solution must pass all ten tests.
| Command | Meaning | Prints |
|---|---|---|
EV src stamp | An event from source src, stamped stamp | src t, then possibly OUT_OF_ORDER src line (Part 2) |
OFFSET src ns | Set the clock offset of source src to ns nanoseconds (Part 2) | nothing |
MERGE | Release the buffered events in time order (Part 3) | MERGE n, then n lines t src line |
Stamp formats
The form tells you the unit:
| Form | Example | Meaning |
|---|---|---|
integer followed by ns | 1792416600000000000ns | nanoseconds since the epoch, UTC |
integer followed by us | 1792416600000250us | microseconds since the epoch, UTC |
YYYY-MM-DDTHH:MM:SS, an optional fraction, then a zone | 2026-10-19T09:30:00.000250001-04:00 | a local date and time of day, with the zone written in the stamp |
- The integer forms may be negative, for instants before 1970.
- Calendar fields have fixed width with leading zeros, and the letter
Tseparates the date from the time. - The fraction is optional. When present, it is a
.followed by 1 to 9 digits, the leading digits of the part of a second:.5is 500,000,000 ns and.000001is 1,000 ns. - The zone is
Z, meaning UTC, or a zone offset+hh:mmor-hh:mm: how far the local clock is ahead of UTC (+) or behind it (-). - Converting to UTC subtracts a
+zone offset and adds a-one, so09:30:00-04:00is13:30:00Z. Every calendar stamp carries its own zone offset, so no time-zone database is needed.
The calendar
- Converting a date needs the number of days from 1970-01-01 to that date, negative for earlier dates.
- Use the Gregorian calendar. The months have 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, and 31 days, and February has 29 in a leap year.
- A year is a leap year when it is divisible by 4, except that a year divisible by 100 must also be divisible by 400. So 2000 and 2024 are leap years, and 1900 and 2100 are not.
- There are no leap seconds: every day has exactly 86,400 seconds.
- Use your own integer arithmetic, not a date library.
Part 1: Normalize every stamp
- Each
EVprintssrc t, wheretis the stamp as nanoseconds since the epoch, UTC. - An
nsstamp is already in that form. Ausstamp is multiplied by 1,000. - For a calendar stamp, let
daysbe the day count of its date. Its local reading is(days * 86400 + HH * 3600 + MM * 60 + SS) * 1000000000plus the fraction in nanoseconds. tis the local reading corrected by the zone offset.
Limits and guarantees
- All input is well formed. Every date exists,
HHis 00 to 23, and minutes and seconds are 00 to 59. A zone offset'shhis 00 to 23 and itsmmis 00 to 59. - Calendar stamps have years from 1900 to 2199.
- Every stamp denotes an instant from 1900-01-01T00:00:00Z to 2199-12-31T23:59:59.999999999Z, which is -2,208,988,800,000,000,000 to 7,258,118,399,999,999,999 nanoseconds.
- Every clock offset is between -1015 and 1015 nanoseconds.
- Every value, a corrected time included, therefore fits in a signed 64-bit integer, whose limit is about 9.22 × 1018. A product such as
days * 86400 * 1000000000overflows 32 bits, so keep every intermediate instd::int64_t. - There are at most 105 commands. Source names are 1 to 8 uppercase letters or digits.
Tests 1–4 cover this part. They contain only EV commands, and each source's times never decrease, so no OUT_OF_ORDER line appears.
Part 2: Clock offsets and the order check
Each source's clock drifts from the reference clock, and a sync process such as NTP or PTP measures by how much.
Clock offsets
OFFSET src nssets the clock offset of sourcesrctons, which may be negative.- Every later event from
srcadds the offset:t = normalized stamp + offset.EVprints this correctedt, and every comparison uses it. - A new
OFFSETreplaces the previous value instead of adding to it, and does not change events that already arrived. - A source with no
OFFSEThas offset 0. AnOFFSETmay name a source that has not sent an event yet.
Order check. Each source is expected to stamp its events in non-decreasing time order. After printing an event's src t line, print:
OUT_OF_ORDER src line
- Print it if
tis less than the largest corrected time among that source's earlier events.lineis the event's own command number. - An event whose
tequals that largest time is in order. - Each source is checked only against its own events.
- An out-of-order event does not lower the largest time seen.
- A step backward caused by a new offset counts like any other, since it is the wall clock moving back under a correction, as in the article.
Tests 5–7 cover this part. They contain no MERGE.
Part 3: Merge the sources
Every event is buffered as it arrives, together with its corrected t. MERGE prints:
MERGE n t src line
n= the number of events that arrived since the previousMERGE(or since the start).- Then those
nevents, one per line ast src line, in increasing order oft. - Equal
t: ordered by source name, compared character by character in ASCII order. Digits come before letters, and a name comes before any longer name that starts with it (CME<CME2<EXCH). - Same source and equal
t: ordered byline, which is arrival order.
Rules for the buffer:
- The printed events leave the buffer, so the next
MERGEprints only newer arrivals. AMERGEwith nothing buffered printsMERGE 0. - Out-of-order events are merged like any other, at their corrected time.
MERGEdoes not change any source's offset or largest time seen.
Tests 8–10 cover this part.
7 EV EXCH 2026-10-19T09:30:00-04:00 EV FEED 1792416600000000000ns EV GW 1792416600000250us EV EXCH 2026-10-19T09:30:00.000250001-04:00 EV LDN 2026-10-19T14:30:00.0002+01:00 EV REF 2026-10-19T13:30:00.000000001Z EV FEED 1792416600000250001ns
EXCH 1792416600000000000 FEED 1792416600000000000 GW 1792416600000250000 EXCH 1792416600000250001 LDN 1792416600000200000 REF 1792416600000000001 FEED 1792416600000250001
The date 2026-10-19 is day 20,745 after 1970-01-01. The first stamp's local reading is 20745 * 86400 + 9 * 3600 + 30 * 60 = 1,792,402,200 seconds.
The zone -04:00 means the local clock is four hours behind UTC, so adding 14,400 seconds gives 1,792,416,600 seconds, 13:30:00 UTC.
The feed handler wrote the same instant directly in nanoseconds, so lines 1 and 2 print the same t. The gateway's microsecond stamp gains three zeros.
The LDN clock is one hour ahead of UTC, so its hour is subtracted, and its four-digit fraction .0002 is 200,000 ns. The REF stamp is already in UTC and is 1 ns after 13:30:00.