From string to different time zone not working in ruby - ruby

I have field in my table as "start_time" as string. E.g Value is "2014-02-07T02:00:00Z". This is provided by API.
We want to show this time as per the client time zone (May EST, Central, IST, Pacific time). For testing In my system I set time zone as (Estern Time (US&Canada))
I tried
start_at = "2014-02-07T02:00:00Z"
start_time = Time.parse(start_at.chop!)
gm_start_time = Time.gm(start_time.strftime('%Y'), start_time.strftime('%b'), start_time.strftime('%d').to_i, start_time.strftime('%H'), start_time.strftime('%M'), start_time.strftime('%S'))
event_start_time = gm_start_time.localtime.strftime('%m/%d/%Y %H:%M')
Result as:
2014-02-07 02:00:00 -0500 ==> start_time
2014-02-06 21:00:00 -0500 ==> gm_start_time
"02/06/2014 21:00" ==> event_start_time
Here I am trying is
convert the string to Time object
Converting that time to GMT
From GMT trying to find equivalent "loca ltime"
If central time is 2014-02-07T02:00:00Z, then local time should 2014-02-07T03:00:00Z
Why I am getting Feb 6th 21 hours. This is -5 hours from expected result. My timezone also -5 hours.
(Time is 24 hours format)
Anyone have any idea on what I am doing wrong?

I think you have some issues with understanding how to handle the time zones. The Z letter at the end of the timestamp indicates that the time is UTC, so apparently your API provides you the time in UTC. When you remove it by using chop! (note that you could simply use chop anyway), Ruby cannot tell anymore what the time zone is, so it's going to use your local time zone by default (I live in UTC+08:00):
1.9.3-p484 :006 > Time.parse("2014-02-07T02:00:00Z")
=> 2014-02-07 02:00:00 UTC
1.9.3-p484 :007 > Time.parse("2014-02-07T02:00:00")
=> 2014-02-07 02:00:00 +0800
Using Time.gm then becomes unnecessary since you already have the time in UTC (assuming the API always sends you timestamps in UTC).
Also, what you are exactly trying to achieve is a bit confusing because you have at least one typo in your post (unless you're trying to obtain a local time in Europe).

Related

Microsoft Flow for datetime between 2 times

I am looking to create a microsoft flow to check if an email was received between 18:00 and 18:15 Pacific Time. Ideally this would be a single formula instead of comparing time >= 18:00 and time <= 18:15
Currently I have:
convertTimeZone(triggerOutputs()?['body/receivedDateTime'], 'UTC', 'Pacific Standard Time', 'HH:mm')
to convert the datetime from UTC to PST in HH:mm format. My next thought was to subtract a time (18:00) from this and get the difference in minutes, then checking that this value is between 0 and 15
I've broken out my answer but you can do what you need with it. Personally, I think this is the most transparent and easiest approach but it's up to you.
Flow
To break it down ...
Firstly, the first step is me merely initialising a string that can be used to get the time from.
Next, I transform the minutes into an integer using the following expression (change out the locale as needed) ...
int(formatDateTime(parseDateTime(variables('Received DateTime'), 'en-AU', 'yyyy-MM-dd HH:mm'), 'HHmm'))
Finally, I do the comparison using this expression ...
and(greaterOrEquals(variables('Time Integer'), 1800), lessOrEquals(variables('Time Integer'), 1815))
Invalid
Valid

Ruby - Get timezone offset from latitude and longitude for specific date and time

I am using the timezone gem to obtain the time zone from latitude and longitude. I need the time zone for some solar calculations. The API I use requires the timezone offset (without the daylight). For example now (2021/10/12 12:37) in UK with BST time the timezone offset for the API would be zero. With timezone though I would get 3600
tz = Timezone.lookup(51.51, 0.1)
=> #<Timezone::Zone name: "Europe/London">
tz.utc_offset
=> 3600
That's easy. I can check for daylight saving and correct.
(tz.dst?) ? (tz.utc_offset) : (tz.utc_offset - 3600)
I can do the same for another time (I do all the calculations in 2010) as long as it is in my locale timezone.
If I try, say, Melbourne, I am not able to create a time in that location and check the daylight saving. I am sure I am doing something wrong. Am I overthinking it?
For example:
tz = Timezone.lookup(-37.814, 145)
=> #<Timezone::Zone name: "Australia/Melbourne">
tz.utc_offset
=> 39600 ((10 + 1)h)
This is now (2021/10/12 12:37), but for my API to work this should be 10 because daylight should not be included.
My idea was to take a reference time, 2010/01/01 01:00 and make all the calculations for that time. The problem is that I don't know how I can create a time in a specific time zone and check whether that time is a daylight time.
I have tried
reftime = tz.time(Time.new(2010,1,1,1,0))
but if do
reftime = tz.time(Time.new(2010,1,1,1,0))
reftime.dst?
I get false, but it should be true.
How can I check whether a specific date in a specific location is in daylight saving mode?
Melbourne has started daylight saving at 2021-10-03 03:00:00. So it's using AEDT but not AEST.

Heroku Scheduler timezone?

What's the Heroku timezone for Scheduler? It says "UTC" but I'm not sure if that's correct. My tasks are starting at the wrong time despite converting the timezone correctly.
Thoughts?
This question's a bit old, but yes, Heroku Scheduler definitely uses UTC even if you specify a timezone in your app. I tested it with this code:
task :send_test_email => :environment do
if Date.today.strftime("%A").downcase == 'monday'
UserMailer.send_test_email(Time.now.strftime("%Z")).deliver
end
end
I'm in Pacific time zone. I set Heroku Scheduler to run this every 10 minutes, and this email was able to send on Sunday night PST, which is Monday in UTC time. Also, the string I passed to the mailer,Time.now.strftime("%Z"), is set as the subject of the email which indeed read, "UTC"
One potential source of confusion is Daylight Saving Time. For example, UTC is always 8 hours ahead of Pacific Standard Time (PST - used for winter months), and 7 hours ahead of Pacific Daylight Time (PDT - used for summer months). So if your tasks are off by an hour, it could be you measured from the wrong UTC zone.

TimeZoneInfo Class and Daylight saving time

I am trying to get the Timezone of the device (windows phone). I used this class and the property BaseUtcOffset. I live In Jordan, and it was suppose to give me +3 hours, but instead it gave me +2. i think its the daylight saving time, but i have no idea how to use it, any ideas?
var x = TimeZoneInfo.Local.BaseUtcOffset; // x.Hours = 2
the correct timezone from timeanddate.com
You should use GetUtcOffset().
The BaseUtcOffset property returns the difference between UTC and the time zone's standard time; the GetUtcOffset method returns the difference between UTC and the time zone's time at a particular point in time.
That's the right response. The timezone is 2 hours ahead of UTC. Local time is 3 hours ahead of UTC.
You might want to look at GetUtcOffset() or IsDaylightSavingsTime().

Get a Time object representing a given time on a given date in Ruby

What's the easiest way to give a Time object representing a given time (say 5pm) on a given date (say 1/1/2010) in a given time zone (say EST)?
I'm a big fan of Time.parse, although Time.mktime is also an option:
Time.parse("1/1/2010 5:00pm EST") # => Fri Jan 01 17:00:00 -0500 2010
Time.mktime(2010, 1, 1, 17, 00) # => Fri Jan 01 17:00:00 -0500 2010
Note that Time.mktime always renders results using the local timezone, so Time.parse is more flexible in that regard.

Resources