Time.Use_Zone Is Not Working as Expected

Time.use_zone is not working as expected

This should work ok:

Time.use_zone('Hawaii') do
p Time.zone.now
end

Time.use_zone is ignored during active record query

Concerning this specific query you shouldn't be too worried. The SELECT query converted your Time.zone.now to UTC, but so does everything else that heads to your database.

This happens because Rails converts all datetimes to store (and lookup) in the database to UTC by default. When your time is 12:33PM it's 19:33 UTC.

Format date in a specific timezone

As pointed out in Manto's answer, .utcOffset() is the preferred method as of Moment 2.9.0. This function uses the real offset from UTC, not the reverse offset (e.g., -240 for New York during DST). Offset strings like "+0400" work the same as before:

// always "2013-05-23 00:55"
moment(1369266934311).utcOffset(60).format('YYYY-MM-DD HH:mm')
moment(1369266934311).utcOffset('+0100').format('YYYY-MM-DD HH:mm')

The older .zone() as a setter was deprecated in Moment.js 2.9.0. It accepted a string containing a timezone identifier (e.g., "-0400" or "-04:00" for -4 hours) or a number representing minutes behind UTC (e.g., 240 for New York during DST).

// always "2013-05-23 00:55"
moment(1369266934311).zone(-60).format('YYYY-MM-DD HH:mm')
moment(1369266934311).zone('+0100').format('YYYY-MM-DD HH:mm')

To work with named timezones instead of numeric offsets, include Moment Timezone and use .tz() instead:

// determines the correct offset for America/Phoenix at the given moment
// always "2013-05-22 16:55"
moment(1369266934311).tz('America/Phoenix').format('YYYY-MM-DD HH:mm')

Delayed Job converting time, giving weird mocha expectation error

I thought I had the answer to this when I first saw the question. But I don't anymore, so here is a guess:

Since Time.zone.now, DateTime.now and Time.now are slightly different from each other, and Zone is something Rails has created (I think), could it be that ruby somewhere in your testing framework misses out on the zone-thingy? Time.zone.now.to_datetime has rescued me from similar stuff before. It lets you use zone, and you get the same format as DateTime.now witch lacks DateTime.zone.now.

Try: data = {:value => 0.856, :timestamp => Time.zone.now.to_datetime}

Parsing time offset in Go using time package

The func(Time) Zone returns the second argument offset which is the seconds east of UTC. So your offset of -0700 is returned as -25200 which is - (7 * 60 * 60)

Format date in JS using Moment.js

From the doc here:
http://momentjs.com/docs/#/displaying/format/

You should use HH:mm. Then your time will have right format.

Further information, the time might be incorrectly as you expected because timezone. Momentjs auto display time in your system timezone.

Update:
To display time in specified Timezone, use "zone" method:

console.log(moment(order.start_time).zone('+00').format("HH:mm"));


Related Topics



Leave a reply



Submit