QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › SQL in QA work

What should you pay attention to when working with dates and time zones?

In PostgreSQL a point in time is stored in timestamptz, and a calendar day exists only relative to a zone, so one moment can fall on different days. A day report uses a half-open range in the right zone, not equality or BETWEEN. An hour or two of difference between the UI and the database is not always a bug; the zone is clarified first. Daylight-saving days are tested separately.

SQL · QA practice

The report "for 30 September" differs by one transaction between the database and the UI. A bug or time zones?

The database has two transactions for 30 September, while the report in the UI has one. The tester files a bug and the developer answers that the report is built in Warsaw time. Who is right depends on the zone the time is stored in, the zone it is shown in and the day boundaries the report was counted with. This lesson is about how time works in PostgreSQL, why day boundaries differ between zones and which cases deserve separate tests.

How it works and why

TIMESTAMP WITH TIME ZONE (timestamptz) in PostgreSQL stores a point in time: internally it is UTC, and on output it is converted to the session zone (the TimeZone setting). The TIMESTAMP type without a zone stores only "wall-clock" time with no zone, and without an agreement on the zone it was written in, it means nothing. Hence the rule: keep a point in time in timestamptz, while a calendar day exists only relative to a zone. The same moment may fall into different days.

Three transactions in two zones

In UTC
idUTC timedate
a09-30 21:3009-30
b09-30 22:3009-30
c10-01 10:0010-01
In Warsaw (UTC+2)
idtimedate
a09-30 23:3009-30
b10-01 00:3010-01
c10-01 12:0010-01
Figure 1. Transaction b belongs to 30 September in UTC and to 1 October in Warsaw.

A report for 30 September in UTC contains a and b (two), and in Warsaw only a (one). This is not a bug but different day boundaries. It is a bug when the report does not say which zone it is built in, or when the UI and the export use different zones.

Example: the right day range

The condition created_at = '2026-09-30' looks for exactly midnight, so it finds almost nothing. The range is written half-open, with the bounds in the needed zone:

SHOW timezone;  -- the session zone

SELECT COUNT(*)
FROM transactions
WHERE created_at >= TIMESTAMP '2026-09-30' AT TIME ZONE 'Europe/Warsaw'
  AND created_at <  TIMESTAMP '2026-10-01' AT TIME ZONE 'Europe/Warsaw';
-- on the data above: 1 (only a)

-- the date of a transaction in the needed zone
SELECT id, (created_at AT TIME ZONE 'Europe/Warsaw')::date AS day
FROM transactions;

TIMESTAMP '…' AT TIME ZONE 'Europe/Warsaw' turns wall-clock time into a point in time, and created_at AT TIME ZONE 'Europe/Warsaw' does the reverse: it shows the moment as wall-clock time in Warsaw. Comparing two timestamptz values always goes by moments, so the session zone changes only how the result looks, not which rows are selected.

The switch to and from summer time breaks the assumption "a day has 24 hours":

SELECT (TIMESTAMP '2026-10-26' AT TIME ZONE 'Europe/Warsaw')
     - (TIMESTAMP '2026-10-25' AT TIME ZONE 'Europe/Warsaw');
-- 1 day 01:00:00, i.e. 25 hours: 25 October 2026 is the autumn switch

In spring, on 29 March 2026, the day lasts 23 hours and local time between 02:00 and 03:00 does not exist. In autumn the time 02:30 occurs twice. Adding INTERVAL '1 day' to a timestamptz counts a calendar day by the session zone, while INTERVAL '24 hours' is exactly 24 hours, so on the switch day the results differ.

Limits and alternatives

If a column has the type timestamp without a zone, first find out which zone the application fills it in: often it is the server zone, and then on a distributed system data silently mixes. That is a question for the architecture, not for the query. Users from different countries need a zone per user, because "today" in Warsaw and in New York are different days. NOW() returns the start time of the current transaction and does not change inside it, while clock_timestamp() shows real time. For tests with time it is better to control the application's clock than to wait for a real midnight.

Common mistakes

  • = '2026-09-30' instead of a range: finds only rows exactly at 00:00.
  • BETWEEN '2026-09-30' AND '2026-09-30 23:59:59': loses the moment between 23:59:59 and midnight; a half-open range is safer.
  • Treating a difference of an hour or two between the UI and the database as a bug without asking about the zone.
  • Skipping the daylight saving switch days in tests of reports and limits.
  • Comparing dates as strings: '10-01' and '1-10' do not sort as dates.

Check yourself

Transactions on 30 September definitely exist, but WHERE created_at = '2026-09-30' returns 0 rows. Why?

Answer

A literal with no time means 00:00:00, and equality finds only rows exactly at that moment. A half-open range is needed: >= '2026-09-30' AND < '2026-10-01' in the needed zone.

How many hours does the day of 25 October 2026 have in Warsaw and what to test on that day?

Answer

25 hours: the time 02:30 occurs twice. Test "per day" reports and limits, the order of events with the same wall-clock time, and calculations that add days and hours.

In an interview. Junior: With dates I find out which zone the time is stored in and which zone the UI shows: a difference of an hour or two is often not a bug. I write day ranges as "from the start of the day inclusive to the next day exclusive". Middle/Senior: I tell timestamptz and timestamp apart: the first stores a moment, the second only wall-clock time. I build day boundaries in the user's or the report's zone, test the daylight saving switch days separately (23 and 25 hours) and understand that the session zone changes the look, not the selection of rows.

More articles in this chapter