When using the localtime method on a UTC Time object, is the time returned the local time as specified in the app configuration or the local time of the user's location?
localtime will give you the time in the current time zone of the machine running the code.
Apidock(localtime): for ruby, ruby on rails
.localtime in your app will show your app's time. To display localtime to different users you will need to add some Javascript.
This answer might help you.
How to display the time in user's timezone
Related
I am working in ruby, I have an object containing today's date, time from DB and only want time truncating the date. I can get,
DateTime.now.strftime("%H:%M")
I'm curious about where Ruby get date and time from? From internet, device or browser?
https://ruby-doc.org/core-2.2.0/Time.html#method-c-new
Unless given a time during initialization of a Time instance, Ruby uses the type from the system it is running on.
If you want the current time on a user's device in a Rails project, you'll need to capture it with Javascript and pass it back to your Rails application.
I am in the process of developing SMS based app. When my app launches I select the last-message-date-time from database and send it to the server and I get back all the messages with date-time > last-message-date-time but because the app and server may not be in the same timezone I can't get the correct result. What is the best way to fix this issue? Please help. Thank you.
This question is the follow up of update SQLite with content of push notification when the app is not running.
One of the options is to store the time stamp in UTC/GMT on server, and when you get the date from server, convert it to local date object using GMT as formate, then show the user to their local timezone using formatters. ie.
Store the date in UTC/GMT on server.
Get the date string in app, use NSDateFormatter with GMT/UTC as timezone to convert it to NSDate object in Objective-C
Use NSDateFormatter with local timezone to show it to user.
Keep in mind that NSDate holds the date as independent of timezones so when showing it to user, use any NSDateFormatter to convert it to local or any timezone.
Here's apple's guide to managing timezones: Using Time Zones
A useful discussion here: A Fast, Accurate Way to Handle Dates from Servers
Please first convert your system's timezone to your server's timezone when you select date-time from database then send it to server.
To figure out this issue. you need to follow the following steps.
Step 1 : Always keep server time zone in UTC.
Step 2 : once you need to send Date-time to server. first need to convert it to UTC and then send to server so server stored it.
Step 3 : While fetching Date-Time from server, you need convert it with respected Time Zone. where you currently are. like if i am using app from INDIA then i will add +5:30 in receiving date from server. so all country manage same.
Hope this help you
I can't seem to find if I need to have the location services prompt if I'm just using the timezone? I've turned off location services on my phone while testing and it still finds my timezone with it off. I just don't want to be rejected by apple.
I'm using this:
var timeZone = NSTimeZone.localTimeZone().name
Thanks for any help.
Keith
No. You will never get a prompt for localtimezone which is used by default when creating your NSDate objects. BTW you are not defining any timeZone like this you are only assigning the name of the local timezone to a var.
If you need some more info regarding time zones you should take a look at this link:
using NSTimeZone framework
I am working on rails 4 application. I want to show the time for comment I created. If I open the site in india then time should be shown in IST (according to indian standard) and If I am in USA so for the same comment that i made in india time should be shown according to USA time zone.
What do I need to do in my config file for development and production?
Do I need to change anything in database?
Please help me.
Rails always saves times in UTC (universal time), and the server has a setting which tells it which timezone it (the server) is running in.
To show different times to the client, Rails (which runs on the server) will need to know which time zone the client is in. This isn't in a standard request header so you will need to get them to submit the information somehow. Once you know their timezone you can ensure that you always show times to the user using their timezone - there are helpers for this.
Getting their timezone can be done explicitly, eg by giving them a timezone dropdown in their "My Account" page, and then saving that in their user record, and/or by making it more upfront and forcing them to choose one in a popup, if you don't know it.
Or, you can do it for them using Javascript, passing it through in a cookie. See this article for an example of how to do it.
http://thisbythem.com/blog/clientside-timezone-detection/
Well one solution can be to store the time zone of the user in the database, write a filter
around_action in your ApplicationController which would set the Time.zone to the time_zone from the database field.
You might want to look at Time.zone and TimeZone in the rails api
Here is a railscast , you can figure it out from the comments and the github link.
In my rails application,After login a user has to create a timesheet entry.The time of creation of the entry is currently my server time.Whereas i want it to be the time of the timezone from where the entry is made i.e if entry is made from any other country.I'm using rails 3 and after searching the web also exact solution cannot be achieved.
Thanx
You can't automatically determine the user's timezone, but you can allow them to choose their own timezone. Then you can set Time.zone = ActiveSupport::TimeZone(offset) before calling update and everything should work correctly. You could also save each user's preference for time zone in your user model.
I'm not sure if it's possible to do this in Rails as the issue is getting the physical location of a user.
It may be worth using JavaScript to populate a (hidden?) field with the current time, which should be taken from user's local machine. You could then explicitly set the created_at field to be this value.
Intrigued to know if Rails can get around this somehow...
If you want, take a look to this question : transform time into local time in Ruby on Rails
And : http://www.wetware.co.nz/blog/2009/07/rails-date-formats-strftime/
Maybe it can helps you.