I don't understand this behaviour (or the doc) of this:
https://play.golang.org/p/vz2UTz-3Yy
On the playground, it return the expected results:
t = 2015-06-01 00:00:00 +0000 UTC
t.Location() = UTC
parsed = 2015-06-01 00:00:00 +0000 UTC
parsed.Location() = UTC
On my mac, I get:
t = 2015-06-01 00:00:00 +0000 +0000
t.Location() =
parsed = 2015-06-01 00:00:00 +0000 +0000
parsed.Location() =
The problem is, if I create a date with
time.Date(2015, time.June, 01, 00, 0, 0, 0, time.UTC)
the 2 times are different, because one has a location ("UTC"), and the other not (or "Local"). I'm bit lost here.
Thanks
When parsing a time with a zone offset like -0700, if the offset corresponds to a time zone used by the current location (Local), then Parse uses that location and zone in the returned time. Otherwise it records the time as being in a fabricated location with time fixed at the given zone offset. [time.Parse]
t.Location (a name) is only set when the local offset matches the offset that is in the parsed date string. You probably have a different time zone set.
So the offset is recorded but the location is not looked up.
Related
Say this is the current server time in UTC 2021-07-30 00:33:25 UTC, which in local time is just 2021-07-29 5:33 PM
25 hours from that time is 2021-07-31 01:33:25 UTC, which in local time is 2021-07-30 6:33 PM
The stored time being compared is saved as 2021-07-30T19:00:00+00:00, which in local time should be 2021-07-30 7:00 PM
The problem is that the stored time should be 2021-07-31T02:00:00+00:00 to equal 2021-07-30 7:00 PM,
but I'm not sure of the best approach to get that time.
Here's how I get the time being sent to my rails backend
import * as moment from 'moment-timezone';
...
const start_time = moment
.tz(
listingAvailability.listing_availability_ranges_attributes[0]
.start_time,
'YYYY-MM-DD hh:mm a'
)
.tz('America/Phoenix')
.toISOString(true)
I fixed it with
moment.utc(moment(listingAvailabilityTime).utc().local()).format();
I have an Event model which has start and end date/times and an associated time zone.
I need the date times submitted to be stored in the correct version of UTC based off of the time zone entered.
Example, if the user submits 2014-04-24 7PM EST
The database should be:
2014-04-24T23:00:00.000Z
Not this:
2014-04-24T19:00:00.000-07:00
Ok, I know rails stores everything in UTC, but I need to store the UPDATED UTC time based off the time zone - since my form is 3 fields, x2 date/time & x1 time zone
This is my model validation I thought would work properly:
def calc_utc_based_off_timezone
time_zone = self.time_zone
puts "start date before assignment: "
puts self.start_date
self.start_date = self.start_date.in_time_zone(time_zone)
self.end_date = self.end_date.in_time_zone(time_zone)
puts "start date after assignment: "
puts self.start_date
end
Entered in my form:
2014-04-24 - 19:00
2014-04-26 - 19:00
est
Output:
start date before assignment:
2014-04-24 19:00:00 UTC
start date after assignment:
2014-04-24 19:00:00 UTC
What I'm expecting:
start date before assignment:
2014-04-24 19:00:00 UTC
start date after assignment:
2014-04-24 23:00:00 UTC <-- updated UTC to reflect the time zone
Any help would be appreciated, thanks.
There is support for Time objects with Time Zones in the ActiveSupport::TimeWithZone class.
As long as you set the Time zone before giving the params to the new object function Event.new(params[:event]) rails will apply the offset that is set, and interpret as if its in that time zone.
# example params like the request
params = {
time_zone:"EST",
event:{
start_date:"2014-04-24 - 19:00",
end_date:"2014-04-26 - 19:00"}
}
Time.zone = params[:time_zone] # pulled from a time_zone_select field
#event = Event.new(params[:event])
if you have separate time and date fields, just combine them into whichever field holds the datetime in the database.
params[:event][:start_date] = "#{params[:event][:start_date]} #{params[:event][:start_time]}"
params[:event][:end_date] = "#{params[:event][:end_date]} #{params[:event][:end_time]}"
#event = Event.new(params[:event])
The timezones offset will be automatically applied and you should get the following if you inspect the object.
> puts #event.start_date
2014-04-25 00:00:00 UTC
> puts #event.end_date
2014-04-27 00:00:00 UTC
Here is a great guide that deals with Rails and Time Zones - The Exhaustive Guide to Rails Time Zones
You're right, there is something in Rails ActiveSupport for this,
Time.parse("2014-04-24 - 19:00").in_time_zone
Thu, 24 Apr 2014 23:00:00 UTC +00:00
Time.parse("2014-04-24 - 19:00").getutc
2014-04-24 23:00:00 UTC
You can also specify the detail and format (like iso8601) of your response by passing additional arguments like this:
> Time.parse("2014-04-24 - 19:00").getutc.iso8601(2)
"2014-04-24T23:00:00.00Z"
> Time.parse("2014-04-24 - 19:00").getutc.iso8601(3)
"2014-04-24T23:00:00.000Z"
Documentation for ActiveSupport::TimeWithZone: http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html#method-i-getutc
I am working on a project which requires me to create a Date value by setting it to a particular timezone .
I have used the following code
Date formatter1 = new Date(HttpDateParser.parse("2013-08-02 11:00:00"));
System.out.println("Date dd formatter1"+formatter1);
Result as follows:
Fri Aug 02 16:30:00 Asia/Calcutta 2013
After parsing, it is giving me time according to device time zone ...
adding 5:30 automatically if device time zone is set to India Kolkata.
I want result to be as :
Fri Aug 02 11:00:00 Asia/Calcutta 2013
I mean it should not add the offset as reference to GMT .
How could I work upon this code to set the data required to me as per the Timezone and not change it internally ?
One problem is that your original date string:
2013-08-02 11:00:00
does not include time zone information. So, it is being interpreted as a GMT time. Which then means that, displayed in Calcutta time, it will be
Fri Aug 02 16:30:00 Asia/Calcutta 2013
You want to specify that 11:00 is already in Calcutta time. To do that, use one of the formats defined in the HttpDateParser documentation:
// we make sure to specify time zone information "+05:30"!
long timeSinceEpoch = HttpDateParser.parse("2013-08-02T11:00:00+05:30");
Date date = new Date(timeSinceEpoch);
System.out.println("Date: " + date);
// use this to slightly change the date formatting ... same time zone
String pattern = "yyyy-MM-dd hh:mma";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
String formattedDate = formatter.formatLocal(timeSinceEpoch);
System.out.println("SimpleDateFormat: " + formattedDate);
Note: that in addition to adding "+5:30" to the time string, you have to replace a space after the date with a 'T'.
This code will output:
[0.0] Date: Fri Aug 02 11:00:00 Asia/Calcutta 2013
[0.0] SimpleDateFormat: 2013-08-02 11:00am
if the device's time zone is actually set to Calcutta (Kolkata +5.5).
References
Have a look at this answer on Stack Overflow.
and maybe this one, too.
How do I get the number of milliseconds, in UTC time, since the epoch?
Just like myDate.getTime() returns the milliseconds in local time, I need this in universal (UTC) time.
Any ideas?
The ActionScript 2 Date object has a getTimezoneOffset method that will give you the difference between UTC and the local timezone in minutes.
var utc = myDate.getTime() + (myDate().getTimezoneOffset() * 60000);
I've got a quick-add action in my events controller, as the client really only schedules events at three different time slots in a given day. Date and Time are working fine with the default form, but trying to set the values by hand are giving me some trouble.
def quick_add #params are date like 2012-04-29, timeslot is a string
timeslot = params[:timeslot].to_sym
date = params[:date].to_date
#workout = Workout.new do |w|
w.name = 'Workout!'
w.date = date
case timeslot
when :morning
w.time = Time.local(w.date.year, w.date.month, w.date.day, 6)
when :noon
w.time = Time.local(w.date.year, w.date.month, w.date.day, 12)
when :evening
w.time = Time.local(w.date.year, w.date.month, w.date.day, 18, 15)
else
w.time = Time.now
end
end
The events are getting created, the dates are correct, but times are:
Morning: 2000-01-01 10:00:00 UTC
Expected: 2012-05-02 06:00:00 UTC -400
Noon: 2000-01-01 16:00:00 UTC
Expected: 2012-05-02 12:00:00 UTC -400
Evening: 2000-01-01 22:15:00 UTC
Expected: 2012-05-02 18:15:00 UTC -400
It's worth noting that running the commands in rails console seems to get the results I'd expect.
Time values are stored in UTC/GMT (+0) time zone as an integer so that no time zone data has to be stored with it. Rails always stores times in the database as UTC times. When you read them out, you'll want to convert them back to your local time zone again.
You can use time.getlocal to convert it to your local time zone.