1

I have this Time class extensions inside lib/core_extensions/time/formatter.rb

module CoreExtensions
  module Time
    module Formatter
      def to_formatted_utc
        puts({a: self.utc, b: utc})
        utc.strftime("%H:%M")
      end

      def to_formatted_tz
        in_time_zone.strftime("%H:%M")
      end
    end
  end
end

I'm not sure why, does it doesn't do what I expected,

# Expected results is return 15:51

Time.zone.now                        # => Sat, 13 Mar 2021 22:51:29.217722286 +07 +07:00
Time.zone.now.utc                    # => 2021-03-13 15:51:29.217722286 UTC
Time.zone.now.utc.strftime("%H:%M")  # => 15:51

# Actual results when using to_formatted_utc

Time.zone.now.to_formatted_utc  # => 22:51

So I tried to debug it using byebug, and I see that utc didn't change the timezone to UTC format, so it's stays on +07:00 format, but when I tried to use Time.zone.now.to_formatted_tz it's add another 7 hours to the time. But it shouldn't do like that, as if I tried do this

Time.zone.now.in_time_zone  # it stays to +07:00, and the hours didn't changed

Why does this happen? and how to make it works like what I expected?

Thank you

Ruby version: 3.0.0

RoR version: 6.1.1

Edit:

Just found out that when I tried to byebug inside the method and execute self

it shows this

Time.zone.now                   # => Sat, 13 Mar 2021 22:51:29.217722286 +07 +07:00
Time.zone.now.to_formatted_utc  # => 2021-03-13 22:51:29.217722286 UTC

so the +07:00 format changed to UTC but without subtracting the 7 hours, how to fix this?

Sampat Badhe
  • 8,710
  • 7
  • 33
  • 49

0 Answers0