Rails change default timezone - ruby

I'm trying to change default timezone in Rails 3.2.8 app to GMT+04:00, but I don't know how to do it. The following solutions do not work for me:
config.time_zone = 'Moscow'
config.time_zone = "(GMT+04:00) Moscow"
config.active_record.default_timezone = 'Moscow'
config.active_record.default_timezone = :local
Also I've tried in rails console the following:
ActiveSupport::TimeZone.all.map(&:name)
which returned a list of values, including "Moscow".
Time.zone returned (GMT+00:00) UTC which is not correct, it should be (GMT+04:00) UTC. Then I changed Time.zone = "Moscow" and Time.now returned the correct value (... +0400).
So to fix it I simply used Time.now + 4.hour, but I also need datetime_select to display my local time. Time.now + 4.hour is not a correct solution.
How one can set default time zone to their local value?

All of a sudden I've localized the problem.
I run Win7 and my WEBrick server is showing the correct time, but the project itself shows a wrong one. In the same time, my production server is working correctly, so, obviously, the problem is with the OS.
UPD: The problem is fixed. I should have restarted the WEBrick server after changing the application.rb file.

Related

Does anyone know what TimezoneOffset does on LuisPredictionOptions?

I'm sending LUIS a query that is based on a time value (e.g. "what is the time 10 minutes from now" - just an example). I want the time to come back in the local timezone, so on the LuisPredictionOptions object (C#) I set the TimezoneOffset (as an example I set it to 2 hours ahead, or 120 minutes).
In Fiddler I can see when it calls the LUIS endpoint it's correctly adding "timezoneOffset=120.0".
However, the timezone comes back as UTC - it doesn't matter whether the timezoneOffset is set, or even what it is set to, the time always comes back UTC, using the builtin datetimeV2 entity.
Does anyone know what the TimezoneOffset property is for? Am I just using it incorrectly? Is there another way perhaps to get a local time from LUIS?
[Update]: Here are some examples: https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[AppId]?verbose=true&timezoneOffset=0&subscription-key=[subscriptionkey]&q=/luis/v2.0/apps/c1be57f4-3850-489e-8266-db376b82c011?timezoneOffset=120&log=true
https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[AppId]?verbose=true&timezoneOffset=0&subscription-key=[subscriptionkey]&q=/luis/v2.0/apps/c1be57f4-3850-489e-8266-db376b82c011?timezoneOffset=240&log=true
and I'm trying the following example utterance: "in 10 minutes".
When I do this, the timex is in UTC (e.g. timex=2020-01-11T16:08:25) and the "value" comes back with the same value, minus the "T", as follows: value=2020-01-11 16:08:25
I could understand perhaps if the timex is in UTC, but then possibly "value" should be adjusted by the timezoneOffset?
It looks like there's an incorrect question mark in your URL, right before timezoneOffset.
Using the same query I was able to get the expected behavior, where the returned value is different by 10 minutes.
Which SDK are you using? Perhaps you're using the V3 Runtime SDK which uses the V3 endpoint that doesn't use timeZoneOffset but instead uses datetimeReference, and need to use the V2 Runtime SDK instead.
https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[app-id]?verbose=true&timezoneOffset=10&subscription-key=[key]&q=in 10 minutes
The TimeZoneInfo class's FindSystemTimeZoneById method can be used to determine the correct timezoneOffset based on system time. An example in C# is shown below:
// Get CST zone id
TimeZoneInfo targetZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
// Get local machine's value of Now
DateTime utcDatetime = DateTime.UtcNow;
// Get Central Standard Time value of Now
DateTime cstDatetime = TimeZoneInfo.ConvertTimeFromUtc(utcDatetime, targetZone);
// Find timezoneOffset
int timezoneOffset = (int)((cstDatetime - utcDatetime).TotalMinutes);
Reference:
https://learn.microsoft.com/en-us/azure/cognitive-services/luis/luis-concept-data-alteration?tabs=V2#c-code-determines-correct-value-of-timezoneoffset

Why does Ruby Time.at() only return 01/01/2000 inside Heroku?

I've got a tiny Sinatra app running on Heroku, and it has a method that receives a POST and creates a database object with the parameters of the POST.
One of those params is a timestamp of the form "1432565475.000007". I want that as a date & time in the database, so I added a column, then in the method that handles the post I have:
e.time = Time.at params["timestamp"].to_f
The times come out correctly, but the dates are all Jan 1, 2000.
If I run irb at heroku ("heroku run irb") and try the above line of code manually it converts correctly. It's only when it's running inside my server instance that it interprets the calendar date wrong.
So then I created a view that just iterates through the db, converting the original timestamp (I also have that in a column) into dates. The idea being that this is more closely approximating the code that handles the POST:
-MyEvent.all.each do |me|
%p
=Time.at me.timestamp.to_i
And that works perfectly.
Any ideas?

Dynamically reload somefile.rb in runtime?

I have two Ruby files: exec.rb and lib.rb. lib.rb is required by exec.rb. Every time I modify lib.rb, I need to restart exec.rb.
Is it possible to have exec.rb reload lib.rb while running?
You can subscribe to file change notifications. Here's a lib for osx: rb-fsevent.
When you get notification that the file changed, you can reload it.
filename = './lib.rb' # get file name from event
load filename
You should use load instead of require, because require loads file only once and then does not load it again.
It's possible to do this using File.mtime and comparing the last-modified timestamp for lib.rb.
In your code, get the mtime when you first load the file:
last_mtime = File.mtime('lib.rb')
load 'lib.rb'
Later, in a loop as you process, check again to see if the modification time changed, and reload if necessary:
current_mtime = File.mtime('lib.rb')
if (current_mtime != last_mtime)
last_mtime = current_mtime
load 'lib.rb'
end
I've used a similar technique in the past, and it worked well. I set mine up so it only checked every five minutes, but your needs might be different.

Unable to change Sessions in Sinatra App

I am currently working with a Sinatra app, and struggling to re-set my session variable. This is not your typical "my session disappeared" issue - the problem is I can't alter or change the sessions I have set. Maybe this is a dumb question, and they aren't supposed to change, but that seems like less of a value to my project, as I need to set and then later change a session variable. General code concept below -
get '/' do
session[:data] = {:key, 'default'}
p session[:data] #{"key","default"}
end
post '/:data' do
p params[:data] #"data"
session[:data] = {:key, params[:data]}
end
get '/anotherpage' do
p session[:data] #{"key","default"} as result, even after posting to /:data
end
The session[:data] persists across various routes no problem, but I cannot update, remove, change, anything. Any ideas?

rails session_store odd behaviour

I am using active_record_store in a rails application which is storing this in session session[:email] = "email#address.com"
now this works fine in the action. but when this action gets over and is redirected to another page, which also accesses the same session[:email] I get an error
undefined method `eq' for nil:NilClass
this should probably mean that i am trying to compare values at some place i am not allowed to. but i cannot see anything like that in the code.
This looks like an old question, but I was just having the same problem and had to figure it out on my own, and thought I would post the solution up here for anyone else that runs into this. It's not very well documented, but to get this to work you have to add:
config.action_dispatch.session_store = :active_record_store
to application.rb, and
Application.config.session_store :active_record_store
to config/initializers/session_store.rb. Then, you have to do:
rake db:sessions:create
and:
rake db:migrate
Then, you have to restart your rails server. I think it was the db:sessions:create step that tripped up the original poster. Not only does that database table have to be laid out the way rails is expecting (that is, with an 'id' column, which is the actual cause of this error, I think), but also the current session has to have a valid ID. Hence the need to create the table and re-start the server, or potentially empty the table if it exists.

Resources