Preventing Date and Time from Changing with Timezone

I have been working on a travel app that records transactions. I was surprised recently to see the transaction entry date and time changing with travel across one or more time zone. For example, if I record a lunch expense in Frankfurt that occurred on May 12 at 18:05 (6:05 pm) Central European Summer Time. Then later upon return to the United States west coast (Pacific Daylight Time), I view the transaction in my travel app the transaction is displayed as occurring on May 12 at 09:05 in the morning. 

I would like to maintain the original transaction date and time (May 12 18:05) no matter where the user happens to be located.

I'm guessing that I somehow need to save off the transaction timezone and apply that to the date / time upon display of the transaction. How do I do this?

I'm using DatePicker to get the current date:

DatePicker("", selection: $entryDT, in: ...Date())
                .datePickerStyle(CompactDatePickerStyle())

I'm using the iOS 15 date formatting technique to display the date and time:

let hDate = item.entryDT ?? Date()
    Text(hDate.formatted(.dateTime.day().month(.wide).year().hour().minute()))

What you have is an absolute time of transaction (UTC).

Then, on display it converts it to the local time zone.

To avoid this, specify UTC everywhere for instance.

But what do you do if user creates a new transaction in a different time zone ?

See the use case:

  • User creates a transaction at 10 am in France.
  • With Eurostar under channel, he/she is in UK 20' later. It is now 9:20 locally.
  • What should be the time of the new transaction : 9:20 and the first 10:00 ? Which means second will seem earlier than the first.
  • #1: 10:00
  • #2: 9:20

Nonsense and even pretty dangerous.

With the present situation, transactions would appear as:

  • #1: 9:00
  • #2: 9:20

When back in France:

  • #1: 10:00
  • #2: 10:20

Which makes more sense.

So you'd better be very clear with your spec before trying to Preventing Date and Time from Changing with Timezone

Preventing Date and Time from Changing with Timezone
 
 
Q