Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/utc-property-dates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-ruby": patch
---

`Time`, `ActiveSupport::TimeWithZone` and `DateTime` values in event, identify and group properties and in transactional email `template_data` are now sent in UTC with a `Z` suffix, for example `2026-11-23T05:00:15.123Z` instead of `2026-11-23T10:30:15.123+05:30`. The moment in time doesn't change, and PostHog filters and breakdowns behave the same, but a `before_send` hook or downstream export that compares the raw string, or an email template that prints the raw variable (for example `{{ variables.renews_at }}`), sees a different value. `DateTime` values no longer go through ActiveSupport's `to_time`, so Rails 7.x no longer prints its `to_time_preserves_timezone` deprecation warning, and the result no longer depends on the Rails version, that setting or the server's `TZ`. `Date` values are unchanged.
4 changes: 2 additions & 2 deletions lib/posthog/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def isoify_dates!(hash)
def datetime_in_iso8601(datetime)
case datetime
when Time
time_in_iso8601 datetime
time_in_iso8601 datetime.getutc
when DateTime
time_in_iso8601 datetime.to_time
datetime.new_offset(0).strftime('%Y-%m-%dT%H:%M:%S.%LZ')
when Date
date_in_iso8601 datetime
else
Expand Down
4 changes: 2 additions & 2 deletions spec/posthog/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def shutdown
ENV['TZ'] = previous_tz
end

it 'does not normalize arbitrary Time properties to UTC' do
it 'normalizes Time properties to UTC' do
property_time = Time.new(2024, 7, 16, 13, 30, Rational(123, 1000), '+09:30')

client.capture(
Expand All @@ -497,7 +497,7 @@ def shutdown
properties: { caller_time: property_time }
)

expect(client.dequeue_last_message[:properties][:caller_time]).to eq('2024-07-16T13:30:00.123+09:30')
expect(client.dequeue_last_message[:properties][:caller_time]).to eq('2024-07-16T04:00:00.123Z')
end

[
Expand Down
55 changes: 55 additions & 0 deletions spec/posthog/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'spec_helper'
require 'open3'

module PostHog
describe Utils do
Expand Down Expand Up @@ -46,6 +47,60 @@ module PostHog
end
end

describe '.datetime_in_iso8601' do
it 'converts a DateTime with an offset to UTC with 3 truncated fractional digits' do
value = DateTime.new(2026, 11, 23, 10, 30, 15.123999r, '+05:30')

expect(PostHog::Utils.datetime_in_iso8601(value)).to eq('2026-11-23T05:00:15.123Z')
end

it 'converts a Time with an offset to UTC' do
value = Time.new(2026, 11, 23, 10, 30, 15.123456r, '+05:30')

expect(PostHog::Utils.datetime_in_iso8601(value)).to eq('2026-11-23T05:00:15.123Z')
end

it 'converts a TimeWithZone to UTC' do
value = Time.new(2026, 11, 23, 10, 30, 15.123456r, '+05:30').in_time_zone('Asia/Kolkata')

expect(PostHog::Utils.datetime_in_iso8601(value)).to eq('2026-11-23T05:00:15.123Z')
end

it 'writes UTC DateTime and Time values with Z' do
expect(PostHog::Utils.datetime_in_iso8601(DateTime.new(2013, 1, 1))).to eq('2013-01-01T00:00:00.000Z')
expect(PostHog::Utils.datetime_in_iso8601(Time.utc(2013))).to eq('2013-01-01T00:00:00.000Z')
end

it 'keeps a Date as a calendar date' do
expect(PostHog::Utils.datetime_in_iso8601(Date.new(2026, 11, 23))).to eq('2026-11-23')
end

{
'plain Ruby' => '',
'ActiveSupport' => "require 'active_support/all'\n"
}.each do |label, preamble|
it "writes the same UTC string under #{label} regardless of the process TZ" do
script = <<~RUBY
require 'date'
#{preamble}require 'posthog'
values = [
DateTime.new(2026, 11, 23, 10, 30, 15.123456r, '+05:30'),
Time.new(2026, 11, 23, 10, 30, 15.123456r, '+05:30')
]
puts values.map { |value| PostHog::Utils.datetime_in_iso8601(value) }.join(',')
RUBY

%w[UTC America/New_York Asia/Tokyo].each do |tz|
stdout, stderr, status = Open3.capture3({ 'TZ' => tz }, RbConfig.ruby, '-Ilib', '-e', script)

expect(status).to be_success, stderr
expect(stderr).not_to include('DEPRECATION WARNING')
expect(stdout.strip).to eq('2026-11-23T05:00:15.123Z,2026-11-23T05:00:15.123Z')
end
end
end
end

describe '.get_by_symbol_or_string_key' do
it 'returns value for symbol key when present' do
hash = { test_key: 'symbol_value', 'test_key' => 'string_value' }
Expand Down
Loading