r/iOSProgramming • • 3d ago

App Saturday Couples therapist, not a developer. Built my first iOS app, and the bug that hurt most was a timezone

I'm a couples therapist in Istanbul. Anthea is a small daily app for couples: each partner answers the same five short questions on their own, and when both have answered they get a short reflection on the two views side by side. On day 7 there's a weekly reading. iPhone, iPad, Apple Watch, 6 languages.

https://apps.apple.com/app/id6785014222

Tech stack

SwiftUI + Swift, split into 6 local SPM packages (features, networking, UI/design system, core models, analytics, a watch bridge) Firebase: Firestore, Cloud Functions in TypeScript (europe-west3), Auth, App Check RevenueCat for subscriptions OpenAI API for the reflections, only after the user gives consent. Generation runs server side in a Firestore trigger once both partners have answered WidgetKit, a Live Activity, and a watchOS app + widget Challenge: the timezone bug

Every couple shares a "journey" doc with a start day, and everything (which day you're on, when the next questions unlock) is calculated from it. When I wrote the pairing function I just used Istanbul time, because that's where I am and all my first users were here.

Then people in the US started using it. For them the "day" was rolling over in the early afternoon. Someone who opened the app the next evening had already skipped past day 2 without ever seeing it. And day 2 is exactly where I lose people, so this was a bad place for a silent bug.

Fix:

the app writes the user's own IANA timezone into their journey when two people pair with a code, the function takes the code creator's timezone, falls back to the partner's, and validates it before using it. If both are invalid it falls back to the old default instead of crashing 11 existing couples were already on the wrong timezone. I migrated them by hand and kept the old value in a backup field in case something went wrong Lesson for me: store the timezone with the data from day one, never assume the server's or your own.

AI disclosure: AI-assisted

I'm a therapist and a designer, not a developer. Most of the Swift and TypeScript was written with Claude Code under my direction. The questions, the clinical framework, the design and every screen decision are mine. I tested the questions with couples in the therapy room long before this was an app.

Happy to answer anything, and I'd really like feedback on the onboarding if anyone tries it. Also curious: how do you handle timezones for shared state between two users in different places?

0 Upvotes

22 comments sorted by

4

u/ThanhDongNguyen 3d ago

Storing the IANA timezone at pairing time fixes the main bug, but it will drift when someone travels or moves. A partner who flies from Istanbul to New York for a week will still get the day rollover on Istanbul time.

I think a simpler approach is to let the client decide the day. When the user answers, send a local date key like "2026-11-21" computed with Calendar.current and TimeZone.current. The Cloud Function then only checks that both partners have an answer for that key, plus a sanity check that the key is within one day of the server's UTC date. Then DST and travel are handled by the device, and you don't need another manual migration later.

1

u/Shot_Bat2938 3d ago

ha, i didn't think about travel at all. and long distance couples are exactly the ones who fly a lot. you're right, the tz is saved once and never updated. letting the phone decide the day makes more sense, thanks

2

u/MobileXapps 3d ago

Nice fix. One case to check is partners in different timezones, which is common with long distance couples. With the creator's zone, the other partner's day can unlock at 3am or mid afternoon for them. One way around it is to unlock per person at their own local midnight, and generate the reflection once both have answered the same journey day number rather than the same calendar date.

Also worth a test around DST. Turkey doesn't change clocks anymore but the US and EU do, so anything computed as start plus N times 86400 seconds will be an hour off twice a year. Calendar date math avoids it.

1

u/Shot_Bat2938 2d ago

good point, i didn't think about long distance couples at all and they're probably the ones who need this most. unlocking per person at their own midnight makes sense, going to change it. thanks

2

u/laplace_apps 3d ago

Day-rollover bugs bite almost everyone building shared features. Storing timestamps in UTC, letting each device compute 'today' in its current timezone, and unlocking the reflection once both answers exist (whoever's day it is) covers most of the travel and long-distance cases.

1

u/Shot_Bat2938 3d ago

that's the third person here saying the same thing, so i guess the answer is pretty clear lol. device decides the day, reflection opens when both answers are in. thanks

2

u/SnooRegrets4647 2d ago

How are you testing the rollover now, just changing the clock on the device? My app runs on days and months too, launched Wednesday, and Dec 1 will be its first month change.

1

u/Shot_Bat2938 2d ago

honestly no proper tests yet. i have a debug menu that shifts the journey start date back so i can jump to any week/day, and the day calc takes "now" as a parameter so you can feed it any date. changing the device clock didn't help me much because the server decides the day. for your dec 1, i'd pass a few fixed dates in (nov 30 23:59, dec 1 00:01, dec 31 to jan 1) and check the output, more reliable than the clock. good luck with the first month change

2

u/DimensionMindless336 2d ago

Been down this exact road in my own app, and the thing that bit me hardest wasn't the offset — it was DST. Once you store an IANA zone and compute "day N" with date components, the first week of November or March quietly becomes a 23- or 25-hour day. If any part of the unlock math is startDate + 86400*N you'll drift an hour off local midnight exactly when people are most likely to notice. Always go through Calendar.date(byAdding:value:to:) with the journey's calendar, never raw seconds.

Second flag: you've got two clocks here — the client's local day and the server side. Your Cloud Functions run on UTC regardless of europe-west3 being in the config. Event-driven generation (both answered → trigger) is fine, but if the day-7 weekly reading or any unlock ever becomes a scheduled function, that cron fires on UTC midnight while your users think in Istanbul or New York time. The zone has to be carried into anything time-scheduled.

On the rollover question below: don't test it by changing the device clock. Pull the date math into a function that takes a Calendar as a parameter — Calendar is a value type, so you can set timeZone on a copy and pair it with a fixed reference Date — then unit-test the boundaries directly (Nov 1 and Mar 8 in US zones, plus the Istanbul/NY pair for your real couples). I keep a table of (zone, "now", expected day) cases; it caught two off-by-ones the simulator clock never would have shown.

And I'd second the "per-person local midnight, shared day number" point above: for a shared journey the day index belongs to the couple, not to either phone's calendar date. Anchor it to an instant at pairing, increment it with the journey's calendar, and both people's "day 3" is the same journey day even when their local dates differ — which is what makes "both answered the same day" unambiguous in the first place.

1

u/Shot_Bat2938 2d ago

wow ok, thanks for taking the time to write all this, really. the test table idea is good, i'll do that. didn't know about the utc cron thing either

2

u/ug00gu 2d ago

One more to watch after timezones: daylight saving. Adding 24 hours to get the next day lands on the wrong date twice a year. We build the next date from year, month, day + 1 instead, and convert both dates to UTC midnight before counting the days between them.

1

u/Shot_Bat2938 2d ago

daylight saving really is confusing. some countries don't change clocks at all and others do, which makes it hard to even think it through. and honestly it's a bit ironic, i'm trying to help people find a shared language and i keep getting stuck on something as basic as time :)

1

u/ug00gu 1d ago

Ha, fair. The trick that saved me is to stop thinking in hours at all. If the app only ever asks the phone what the date is, the clock changes take care of themselves.

1

u/Shot_Bat2938 1d ago

That's the part I got right by luck then :) Days are counted by calendar date, never by hours. Only difference is I ask the server instead of the phone, since a couple has two phones.

2

u/mrdzyn 2d ago

Timezone bugs in "daily" apps are basically a rite of passage. What's worked for me is deciding explicitly whose day it is and storing a plain date key like 2026-11-22, computed with the user's calendar and timezone when they answer, instead of comparing raw timestamps on the server. With couples it gets extra fun when partners are in different zones, so it helps to pick one rule and keep that logic in a single place. A few tests that force different TimeZones across midnight and DST changes will catch it if it ever sneaks back in.

1

u/Shot_Bat2938 2d ago

Thanks, "whose day is it" is a really clear way to put it. I went with one rule for the couple: the day follows the timezone of the partner who created the code. I don't have tests for midnight and DST yet, you're right, I'll add them.

1

u/EducatorAdmirable484 2d ago

The day math is covered above, so one thing from the widget side (I ship a lock-screen widget, this is the bug I fight most): if today's question ever shows on a Lock Screen widget or a Live Activity, compute the day when the widget renders, not when the timeline entry is built. WidgetKit reloads are budgeted, so nothing guarantees a refresh at the user's local midnight, and the failure looks like yesterday's question still sitting on the lock screen at 8am while the app is already on today's. Keep the raw start instant and each person's zone in the shared App Group, resolve the day inside the widget's view, and fall back to the app's last computed value - the worst case then is a stale question, not a day count that quietly disagrees with the app.

1

u/Shot_Bat2938 2d ago

yeah widgets really are a pain with updates. in my first app i wanted to play a video in one and was pretty disappointed when i found out you can't 🙂 widgets are nice but i'm honestly not sure people actually use them. i barely use them myself, so maybe i'm just assuming nobody else does either 🙂 anyway, i'll look into your suggestion, thanks for this

1

u/Kitchen_Cable6192 2d ago

On the testing question from further down, the thing that made this stop being scary for me was injecting the calendar and timezone too, not just "now". Your day calc taking a Date parameter is most of the way there. If the function signature is basically dayIndex(start: Date, now: Date, calendar: Calendar) and you build the calendar with an explicit timeZone in tests, you can write a table of cases in XCTest/Swift Testing without ever touching the device clock:

  • the night before and after the US DST change (Nov 1 / Mar 8 this cycle) and the EU one, which is on a different weekend

  • extreme offsets like Pacific/Kiritimati (+14) and Pacific/Pago_Pago (-11) paired together, so the two partners are literally on different calendar dates

  • one partner changing timezone mid-journey (travel case people mentioned)

  • 23:59:59 vs 00:00:00 at both ends

Then use calendar.dateComponents([.day], from: calendar.startOfDay(for: start), to: calendar.startOfDay(for: now)) rather than dividing seconds by 86400, and most of the DST weirdness just goes away.

I build MoveManifest (a moving inventory app, Swift on iOS and Kotlin on Android, full disclosure it's mine) and move dates/delivery windows gave me the same headache. On the Kotlin side the equivalent is passing a java.time.Clock around and using Clock.fixed(...) with a ZoneId in tests, same idea. Having both platforms run the identical case table caught a couple of mismatches I'd never have found by hand.

2

u/Shot_Bat2938 2d ago

the two partners on opposite ends of the world case is a good one, i hadn't thought about that. thanks, and good luck with movemanifest

1

u/sweetassapps 3d ago

That’s not really a tech stack lol