Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 line is 1.
  • There are three parts. Each adds commands or output to the previous one. One solution must pass all ten tests.
CommandMeaningPrints
EV src stampAn event from source src, stamped stampsrc t, then possibly OUT_OF_ORDER src line (Part 2)
OFFSET src nsSet the clock offset of source src to ns nanoseconds (Part 2)nothing
MERGERelease the buffered events in time order (Part 3)MERGE n, then n lines t src line

Stamp formats

The form tells you the unit:

FormExampleMeaning
integer followed by ns1792416600000000000nsnanoseconds since the epoch, UTC
integer followed by us1792416600000250usmicroseconds since the epoch, UTC
YYYY-MM-DDTHH:MM:SS, an optional fraction, then a zone2026-10-19T09:30:00.000250001-04:00a 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 T separates 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: .5 is 500,000,000 ns and .000001 is 1,000 ns.
  • The zone is Z, meaning UTC, or a zone offset +hh:mm or -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, so 09:30:00-04:00 is 13: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

  1. Each EV prints src t, where t is the stamp as nanoseconds since the epoch, UTC.
  2. An ns stamp is already in that form. A us stamp is multiplied by 1,000.
  3. For a calendar stamp, let days be the day count of its date. Its local reading is (days * 86400 + HH * 3600 + MM * 60 + SS) * 1000000000 plus the fraction in nanoseconds.
  4. t is the local reading corrected by the zone offset.

Limits and guarantees

  • All input is well formed. Every date exists, HH is 00 to 23, and minutes and seconds are 00 to 59. A zone offset's hh is 00 to 23 and its mm is 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 * 1000000000 overflows 32 bits, so keep every intermediate in std::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

  1. OFFSET src ns sets the clock offset of source src to ns, which may be negative.
  2. Every later event from src adds the offset: t = normalized stamp + offset. EV prints this corrected t, and every comparison uses it.
  3. A new OFFSET replaces the previous value instead of adding to it, and does not change events that already arrived.
  4. A source with no OFFSET has offset 0. An OFFSET may 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
  1. Print it if t is less than the largest corrected time among that source's earlier events. line is the event's own command number.
  2. An event whose t equals that largest time is in order.
  3. Each source is checked only against its own events.
  4. An out-of-order event does not lower the largest time seen.
  5. 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 previous MERGE (or since the start).
  • Then those n events, one per line as t src line, in increasing order of t.
  • 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 by line, which is arrival order.

Rules for the buffer:

  1. The printed events leave the buffer, so the next MERGE prints only newer arrivals. A MERGE with nothing buffered prints MERGE 0.
  2. Out-of-order events are merged like any other, at their corrected time.
  3. MERGE does not change any source's offset or largest time seen.

Tests 8–10 cover this part.

Example
Input
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
Output
EXCH 1792416600000000000
FEED 1792416600000000000
GW 1792416600000250000
EXCH 1792416600000250001
LDN 1792416600000200000
REF 1792416600000000001
FEED 1792416600000250001
Explanation

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.