Ruby - how to get current user's time? - ruby

I am playing with a timezones and I have a simple registration form. after user's registration, I would like to user display his current time, how to do that?

You can not do it only with Ruby, because such information isn't available on the server side.
1) If you know the user's location, you can do geo locating and determine the timezone. But it could be a problem if ip range you got from geo database has a few timezones.
2) Another solution, you can use javascript to help. So, with javascript you can calculate timezone and set it as cookie, and then access with Ruby from the request object. (How to get timezone with Javascript)

You could ask the user for their current timezone during registration and persist it with their user details.
ActiveSupport (included as part of the Rails API) has a great way to deal with timezones, you can convert the current time to a time in a different timezone using *in_time_zone*.
As an example
Time.now.in_time_zone("America/Guyana")
Time.now.in_time_zone(8)
Where the time_zone key can be found via this API document: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
See TimeWithZone for more info, http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html#method-i-in_time_zone.

Related

Google Calendar - Find Another User's Timezone

Is it possible using the Google APIs to retrieve the timezone for another user?
For example, using the Python Client Library, it's possible to get a user's timezone with something simple like the below...
#build calendar service_calendar
service_calendar = build('calendar', 'v3', credentials=credentials)
# Get the user's timezone
timezone = service_calendar.settings().get(setting='timezone').execute()
...but this can only be used to retrieve the timezone for the credentialed user.
If I'm in a same-domain organization where things like calendar free/busy are shared, is it possible to determine the timezone of another user (whose credentials I don't have) by calling something similar?

Where should I store user's(not logged in) request data?

I want to make an alert function in my side project where users set their custom limit(especially for cryptocurrency prices), and whenever price move beyond their custom limit, users get alert from my server.(It's almost like tradingview's alert function).
I managed to get price data by using websocket. What I'm concerned about is that where should I store user's custom limit data?
I want this feature to open everybody, not just logged-in user, so I think It would be very difficult to store all custom limit data in Database. OR should I use session to store data?? I'm making this project all by my self and got no one to ask.. could anyone give me any tips/ or references? Thank you in advance.
i think you can store user's data on localStorage without login. This is a reference article on W3School : link here

is there a way to do session manage using sinatra framework with cloudfoundry?

none of the tutorials I found online seem to cover this. I mean that do but I'm not seeing what they say I should be seeing when I do the check. is this because I'm on the cloudfoundry platform? if you could direct me to some literature or give me a hint that would be great! I am trying to implement session control by some storing information in a cookie,review that cookie, and compare it to a session table in my db each time the user makes a HTTP request.
This is pretty straight forward. Just make sure you have sessions enabled in Sinatra and use the session_id as a key in your database. I created a little example at https://gist.github.com/danhigham/4943057
The example gets a tweet from twitter as json and retrieves a collection from Redis using the session_id as a key, adds the tweet to the collection as json and then stores it back in the same record.
I have also pushed the example to http://session-tweet.cloudfoundry.com

How does Codeigniter's sess_time_to_update work

based on the following question/answer
CodeIgniter session class not working in Chrome
I had the problem where people are unable to login to my website from another country which is far from the US server. After searching online I've stumbled upon a suggestion which describes how the the problem is based on the difference between the server's timezone and the user's timezone. So, by extending the session_expiration I've managed to get around the problem and people are able to log in successfully.
My questions is whether the sess_time_to_update creates a new timestamp and it will logout the user because the new timestamp is in the wrong timezone? Do I have to make the new sess_time_to_update 17+ hours so that it covers the broadest range of timezones as explained in the question that I've linked. Is there some other way of storing the session at the user's browser based on their localtime (without asking them to choose timezones in the profiles and other sorts of user unfriendly schemes). I would like to have a 2h default session expiration time + the 800sec. update time. I'm not using the database to store the session and I would rather not use it.
The sess_time_to_update setting is how often the session details (such as last activity) are updated. The default is every 5 minutes (300 seconds) and a new session ID will be generated. This would reset the expiration date based on the sess_expiration setting.
I would suggest keeping the sess_time_to_update at the default (or lower) as that would keep the user session alive longer since the session expiration would keep getting reset. The only setting that may need to remain high would be sess_expiration, that is unless you can determine the users timezone.
There are a couple of ways you could try to determine the users timezone. One would be Javascript (Example: Client Side Timezone Offsetting) or you could try using PHP's GEOIP Methods.

How can I get today's date where the user currently is?

Does Date.today() return the current date based on the server's date? If so, how can I get the correct date for a user?
You can use ajax to send back the user date created by javascript on the client machine.
The best way is to ask the user what their timezone is. You can try to determine via software without asking them, but if the user has not set up their machine correctly you'll get an incorrect response. By asking you're giving them the choice to give a correct answer, or to lie to you if they are so inclined.
You might want to ask the same question over on SO's sibling User Interface site.

Resources