Where Ruby get date, time from? - ruby-on-rails

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.

Related

What is the reference for Time.localtime

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

Change jenkins timestamp according to the browser timezone

I have a really simple question and couldn't find the answer about. Is it possible to change the jenkins timestamp according to the user's browser timezone. As my Jenkins server is in UTC but some of our users are in Central timezone while other are in few different timezones. So I want that they see the task(s) timestamp according to their browser time/timezone. How we can achieve this? Thanks
One possible approach for this could be to deliver all dates in a default timezone and use momentjs or a similar library to convert the date time on the frontend side to either the browsers timezone (this could be the default) or to a manually selected one.
This would be rather easy to implement and would not require any big changes in jenkins itself, just all places that deliver times must be wrapped in some markup, e.g. a class and some data-fields (e.g. data-datetime containing an rfc3339 timestamp)
Jenkins issues JENKINS-19887 and JENKINS-1962 detail potential fixes that could be introduced, though they haven't been implemented in any version of the system yet.
If you're only looking for timestamps with the browser's time zone in your console output (with all other timestamps unmodified), Timestamper provides this functionality.

Rails 4 local Time shown according to local time zone

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.

MVC Handling Timezones and JSON Serialization - How to Convert to Specific Timezone

I've spent quite a bit of time on stackoverflow and the internet trying to determine what has been done in my situation. I am building a
centralized line of business website that could span multiple timezones. Based on some valuable information here I have a plan in place to handle this.
Timezone will be a part of a user profile
All Dates will be stored in UTC
EF sets DateTimeKind to UTC on retrieval from database
Is it possible to prevent EntityFramework 4 from overwriting customized properties?
Create some html helpers for DatePickers and DateDisplays to convert UTC time to local time (based off user timezone)
Use Model Binding to convert back to UTC on form posts.
Timezone Strategy
Now to the issue I don't know how to solve. Kendo UI uses JSON to pass information back and forth and as far as I can tell the JSON.net serializer cannot serialize a date to a specific time zone. I can serialize the date as UTC or have JSON.net automatically convert it to local time using the DateTimeZoneHandling setting but that would be local time on the server. From looking at the JSON.net code, I'm not sure I could even write a converter to do what I wanted. So far now if I send the date as UTC through JSON.net and Kendo automatically converts the time to the browser's local timezone.
Assuming User Profile timezone = Browser Timezone seems a little scary for me and I wanted to see what stackoverflow suggests I do. Thank you in advance.
Technology
ASP.net MVC 5
Entity Framework 6
Telerik Kendo UI Controls
JSON.net
Unfortunately I don't know many of the tools you are using, but I know how I would handle timezones.
I would use UTC everywhere, except when displaying times (or rather: datetimes) and in user input.
I would let the browser's JavaScript do the conversion in output, using the browser's timezone. In input, the conversion can be done either client side or server side. In the latter case the timezone must be part of the information sent from the browser to the server. The UTC timezone on the servers should be set at a level as low as possible, and, if possible, in all components (OS, web server, DB...).
A user's preferred timezone is nonetheless useful for all cases where the timezone is not known (e.g., in email communications - but remember to always have the timezone in time formats).
There are times (no pun intended...) when the above is not easy or does not apply. E.g., a schedule (calendar), to which the user attaches the timezone they have in their head, not necessarily the browser's one. Another case is when the user's timezone has to be known for some computation that can not easily be done on the browser - in this case the server will have to get the timezone from the browser first. But most of the time (still no pun intented...) you can happily do without timezones (with UTC) except immediately before showing something to the user or immediately after getting something from the user.
Regarding your last paragraph: you will very soon run into trouble if you assume "user profile timezone = browser timezone".
Some last words of advice:
"Timezone" is an ambiguous term. It may refer to a fixed offset from UTC (like UTC+1), or to the rules for calculating this offset for any instant in a given area (like the timezone of Rome, Italy). Always use the second meaning (e.g., don't use the current offset for a date in the future or past, but correctly convert datetimes back and forth), and remember that both can change during a user's session.
Be careful not to handle "pure" dates as the midnight of the same day (and not as noon either - people in Samoa will thank you), because then the date can change when the timezone changes. In other words, don't attach time information to pure dates.
P.S.: By following a link of yours, I was happy to find that I totally agree with the closing remark of this answer (by someone with more SO reputation than me)
You certainly can pass UTC to the browser and let it convert, but you have to be aware of the potential for inaccuracies. You also can convert server-side and pass a DateTimeOffset in your JSON, but you may need to render it yourself, using something like moment.js.
In the end, what you should do is entirely up to you. Many use cases are similar, but there are plenty of acceptable different paths depending on your exact needs.
Your question is very broad the way you have asked it. What parts are you most concerned with? Kendo? EF? JavaScript? Client-side time zone conversion? Formatting? Could you refine it or provide some context to the type of things your date/time values are representing? Otherwise, you may get some useful tidbits, but it will be hard to provide a definitive answer.
(Oh, and you might be interested in this.)
I was finally able to find this answer here https://stackoverflow.com/a/11968692/3412859 that will allow me to write my own JSONconverter and convert to a specific time zone before it is serialized. I now have all of the dates being converted on the server side based on the timezone setting in the user profile.
I had this problem , too.
It works for me.
I translated the time format to
Start=2016-4-9T8:30:00.000Z
End=2016-4-9T13:30:00.000Z
in background or database.
By doing this,local time zone is useful.

Timezone problems in rails3

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.

Resources