Starkers
Starkers

Reputation: 10541

Include 'UTC` in datetime

One of my assertions is failing because of this slight niggle:

  its(:register_token_created_at){ should eq DateTime.now.utc }

results in:

  expected: Fri, 28 Mar 2014 00:06:33 +0000
       got: Fri, 28 Mar 2014 00:06:33 UTC +00:00

So how should I change DateTime.now.utc for it to pass?

Upvotes: 0

Views: 55

Answers (1)

Leantraxxx
Leantraxxx

Reputation: 4596

I'm assuming that you have your test environment with the following configuration:

yourapp/config/environments/test.rb

#config.time_zone = "whatever"
config.active_record.default_timezone = :utc

Then, you can replace:

DateTime.now.utc #Fri, 28 Mar 2014 00:54:28 +0000

With:

Time.zone.now #Fri, 28 Mar 2014 00:54:51 UTC +00:00

That works for me...

Another (ugly) solution can be:

DateTime.now.utc.strftime("%a, %m %b %Y %H:%M:%S UTC %:z") #"Fri, 03 Mar 2014 00:55:46 UTC +00:00"

And then, you can compare as strings

Upvotes: 1

Related Questions