Update timezone in XCode debug area

Viewed 25

Screenshot

I want all dates in XCode be like "2022-08-31 00:00:00 +0000". Does anybody know how to achieve it?

1 Answers

First, let's be on the same page about what these "weird" dates actually are. Since you expected these to be 00:00:00, I'm assuming these are instants that would represent midnight, when they are in the UTC+7 timezone. However, since all Date are described in UTC (their description output is in UTC), you see 5pm instead.

I'm also going to assume a Calendar instance, with the timeZone offset of UTC+7 is involved in getting this Date, because that's usually how people get this kind of midnight Dates.

If this is just for debugging purposes, I can think of two ways you can do this.

Change the device timezone

Whatever you are running your app on, change its timezone to UTC before debugging. This will make Calendar.current automatically have a UTC timezone. Note that you can't change the simulator's timezone, and needs to change your mac's timezone instead.

Useful link: List of places that use UTC year round

Use different calendars

You can refactor your code so that all of your code uses the same calendar property to compute dates. That calendar is then conditionally compiled depending on whether you are debugging. If you are debugging, it's timezone is set to UTC.

Something like

#if DEBUG
let calendar: Calendar = {
    let c = Calendar.current
    c.timeZone = TimeZone(identifier: "UTC")
    return c
}()
#else
let calendar = Calendar.current
#endif

However, by using this method, it also makes it harder for you to test your app's actual behaviour in different timezones. When in debug mode, you would only see how it behaves in UTC.

Related