The complaint came in on a Monday: "Rai says Saturday's sales were KES 182,000. The till says KES 214,000." Nobody had stolen anything. The daily summary job ran at five past midnight on a server set to UTC, asked the database for "yesterday", and got the UTC yesterday. In Nairobi that day had ended at 3am local time, which meant Saturday's day started at 3am too. Friday night's late sales had been counted on Saturday, and Saturday's last three hours of trading had been pushed into Sunday.
Every engineer who builds for businesses in East Africa meets this bug. Kenya is UTC+3 all year, with no daylight saving, which makes it easy to get away with sloppiness for months. Then a report misses the busiest three hours of a Saturday, and an owner stops trusting the numbers.
Store instants in UTC, think in local days#
The rule we follow is short:
- Timestamps are stored as instants. A sale happened at one moment in time. Store that moment unambiguously, either in UTC or with its offset attached.
- Business days are local. "Saturday" for a shop in Nakuru means 00:00 to 23:59 in Africa/Nairobi. Any question about a day, a week or a month is converted to a UTC range at the edge, then queried.
- Never use "today" from the server clock. Ask for today in a named zone.
In Python that last rule looks like this:
from datetime import datetime, time, timedelta
from zoneinfo import ZoneInfo
NAIROBI = ZoneInfo("Africa/Nairobi")
def business_day_range(day, tz=NAIROBI):
"""Return the UTC start and end of a local business day."""
start = datetime.combine(day, time.min, tzinfo=tz)
end = start + timedelta(days=1)
return start.astimezone(ZoneInfo("UTC")), end.astimezone(ZoneInfo("UTC"))The zone is a parameter, not a constant buried in the code. The day a client opens a branch in Kigali (UTC+2), you will be glad you did that.
A trap specific to ERPNext and Frappe: the system has its own time zone setting, and many datetime fields are stored as naive local time in that zone, not UTC. Read how your own framework stores dates before you write a single conversion. Converting a value that was already local is how you get a bug that is exactly three hours wrong in the other direction.
Schedule jobs in the zone the business lives in#
Cron on a Linux server uses the server's time zone. If the server is on UTC and
you want the morning brief at 7am Nairobi, you write 0 4 * * * and add a
comment. That works until someone moves the server, changes its zone, or copies
the line to a client in a different country.
Better: let the scheduler know the zone. Most job runners accept a time zone per schedule. Where they do not, run the job hourly and let the job decide whether it is due:
def is_due(now_utc, tz, hour):
local = now_utc.astimezone(tz)
return local.hour == hourThis also handles the case where a client has branches in two countries. Each branch has its own zone, and "7am" means 7am where the shop is.
For something like Rai's morning brief, also record that the brief for a given local date was sent. If the job runs twice, or the server restarts at 06:59, the second run sees the record and does nothing. Scheduling and idempotency are the same problem from two sides; the same thinking applies to M-PESA callbacks.
Edges that bite#
A few cases we test for every time:
- Midnight sales. A sale at 23:58 Nairobi belongs to that day. Write a test with exactly that timestamp.
- Month end. "Sales for November" in Nairobi starts at 21:00 UTC on 31 October. Rent reminders, monthly statements and invoice runs all depend on this.
- Messages to people abroad. A reminder for a landlord in London should go at a sensible hour for him, not for the agency. Store each recipient's zone if you message across borders.
- Human-entered times. A receptionist typing "10:30" means local time. Never let a form send a naive time to an API that assumes UTC.
- Logs. Write logs in UTC with the offset shown. When you compare a server log to a customer's WhatsApp screenshot, convert once and say which zone you are in.
Test with a frozen clock#
Time zone code is impossible to trust without tests, and impossible to test if it reads the real clock. Inject the clock. Every function that needs "now" takes it as an argument, or reads it through one small service you can replace in a test.
Then write the tests that describe the business, not the code:
- A sale at 23:58 on Saturday, Nairobi time, appears in Saturday's report.
- The 7am brief is due at 04:00 UTC and not at 07:00 UTC.
- A job that runs twice in the same local day sends one brief.
- A payment made at 00:10 on 1 November, Nairobi time, appears in November's statement, even though it was still 31 October in UTC.
That last one is worth writing out slowly. Everyone gets confused by month edges, including me when I sketched these tests. A test written in the business's own words is the thing that stays right after everyone has forgotten the reasoning.
What to do this week#
Search your codebase for every place that asks for "now", "today" or "yesterday" without naming a zone. Make a list. For each one, write down which day the business means. Fix the scheduled reports first, because those are the numbers owners compare against their till.




