I recently asked MS Copilot to explain this MS Power Fx code (which I found online) to me:
UpdateContext ({TimerLocalStartTime: DateAdd(
Now(),
-TimeZoneOffset(Now()),
TimeUnit.Minutes
)
});
Although I thought I knew exactly what it was doing AND what it intended, I still wanted to check.
The code sets
TimerLocalStartTimeto the current UTC time
So, Copilot understood the intent of the code but it didn’t pick-up the problems I saw (not least the horribly named variable). So I asked:
If the timezone offset is negative, won’t subtracting the timezone offset actually add the time zone offset, causing unintended results?
Quoted below is the response, which I’ll break down:
Understanding TimeZoneOffset
TheĀ
TimeZoneOffsetĀ function returns the difference between the local time and UTC in minutes. This value can be positive or negative depending on your time zone relative to UTC.Handling Negative Offsets
If theĀ
TimeZoneOffsetĀ is negative, it means your local time is ahead of UTC. For example, if the offset is -60 minutes, your local time is 1 hour ahead of UTC.
So far, so good. That’s all correct, which is what you’d expect from the vendor, right?
Subtracting the Offset
When you subtract a negative offset, you’re effectively adding that time to the current time. Here’s how it works:
- Example: IfĀ
TimeZoneOffset(Now())Ā returns -60 minutes:
DateAdd(Now(), -(-60), TimeUnit.Minutes)Ā becomesĀDateAdd(Now(), 60, TimeUnit.Minutes).- This adds 60 minutes to the current local time, which is correct because your local time is ahead of UTC.
So, I am (currently) on BST, which is UTC+1. So, 15:00 local time for me is 14:00 UTC. Adding 60 minutes to 15:00 makes it 16:00, which is UTC+2. So, this is completely wrong. It would be correct if it said:
- This adds 60 minutes to the current UTC time, which is correct because your local time is ahead of UTC.
But that’s not what we want. We want to calculate UTC from the local time. Copilot continues:
Correct Behavior
The code correctly adjusts the local time to UTC by accounting for both positive and negative offsets. So, subtracting a negative offset does indeed add the time difference, aligning the local time with UTC.
Does this make sense? š
No, it doesn’t make sense.
I don’t know about you but this feels like exactly the sort of error we can expect from “vibe coders” for years to come. Working out timezone offsets is already a bit of a brain twister, confidently incorrect assertions from your “AI” assistant won’t help!
