Time that does not depend on user preference - java

Is there a way to get today's date and time such that it corresponds to the real world date/time and is not affected if the user has changed the date/time settings on their phone/web browser?
If not, is using a server time the best way to correctly determine today's date on the phone? Or are there other best practices?

if you don't want to use a server time, u can parse the return of gettime() link

Server time suits most of the needs. Then if your server's time is messed up then you will be in problem.
Alternatively, you can use some third party web service to provide you with the time.
For example
https://developers.google.com/maps/documentation/timezone/
Google being a reputed company, the time returned can be trusted to be correct.

I'd never trust a user's device to retrieve time related information.
Get the UTC time from the server and if required, display it to the user converted to his time zone. Here's a so question on how to convert UTC time to local time with JS
This way your stored time will always be ok and in same "format". The only thing that could happen is that the ends up seeing a "wrong" time, if he faked his location / time zone settings. But I wouldn't mind that.

Related

Spring Boot 2 get client timezone [duplicate]

How to get client/request timezone in jsp?
Unfortunately this information is not passed in HTTP headers.
Usually you need cooperating JavaScript to fetch it for you.
Web is full of examples, here is one http://www.coderanch.com/t/486127/JSP/java/Query-timezone
you cannot get timezone, but you can get current time from client side.i.e. through javascript and than post back. On server side, you can convert that time to GMT/UTC. The UTC shows the TimeZone.
If you just need the local timezone in order to display local times to the user, I recommend representing all times in your service in UTC and rendering them in browsers as local times using Moment.js.
My general rule is to handle and store times in UTC everywhere except at the interface with the user, where you convert to/from local time. The advantage of UTC is that you never have to worry about daylight-saving adjustments.
Note that if you want to show the age of something (e.g. "posted 3 hours ago") you just need to compare the UTC timestamp with the current UTC time; no need to convert to local times at all.
Best solution for me is sending date/time as a string, and then parse with server's timezone to get a timestamp. Timestamps are always UTC (or supposed to be) so you will not need client's TimeZone.
For example, sending "10/07/2018 12:45" can be parsed like:
SimpleDateFormat oD = new SimpleDateFormat();
oD.applyPattern("dd/MM/yyyy HH:mm");
oD.setTimeZone(TimeZone.getDefault()); // ;)
Date oDate = oD.parse(request.getParameter("time"));
Obviously you can set your specific date/time format.

Is there any way to code the time limit?

I'm working on quiz type app in which I want to give user only few turns to give an answer and after exceeding that count he have to wait 10 hours of time limit to try again.
So is there any class/method that may help me to set that time limit in Android?
You can simply store the date where the user starts to take the quizz on the application, and compare the current date with it when needed, the problem is that the user may change the datetime of its device to bypass the limit, so is better to check server side for current dates.
Save on database or SharedPreferences and compare latter, the server side is for getting the current real date toa void user changing it device date to bypass your validation.

vertex 3.x fetch timezone from http request

I am using vertex 3.x. I have a requirement to access timezone from HttpServerRequest object to provide timezone based data to user.
It doesn't have one, basically. There's nothing in a regular HTTP request to identify the time zone.
Your options are:
Use an IP geocoding API to guess at the user's location, followed by a location-to-timezone conversion (e.g. through another API)
Use Javascript to detect the time zone - there are various libraries available to do this, usually resulting in an IANA time zone ID such as "Europe/London"
Probably in conjunction with the first two, offer the users a choice so they can confirm their actual time zone
Note that detecting location from IP address can be fraught with issues due to proxies which are often employed by large corporations.
Also note that even once you've got the same time zone ID as the browser, it's entirely possible that your copy of time zone data on the server will be different to the time zone data in the browser - it changes reasonably regularly. (You'd be fairly unlucky to hit a problem, so long as you keep your data up-to-date, but you should be aware of it.)

Can't get current time forecast using forecast.io API

I'm building a mega simple weather Android app using the forecast.io service.
Using the default call of
https://api.forecast.io/forecast/<my_key>/37.8136,144.9631?units=auto
Always seems to give me the forecast at a time that I don't care for - ie, its currently 2.40PM here, and it keeps giving me 3.40AM forecast.
So I then try to use the time param and yet it still gives me 3.40AM, not 2.40PM
https://api.forecast.io/forecast/<my_key>/37.8136,144.9631,1451619638?units=auto
I've validated 1451619638 as my current unix time via the helpful site http://www.epochconverter.com/
Any pointers as to why I can't seem to get MY local current time?
The API returns the timezone and then the offset from GMT. Check those.
The idea of the first one is if you do not provide the time, you will get a current forecast, and all you will have to do is convert it into your timezone. So if they are doing GMT and you are GMT - 8, you simply subtract 8 from the time value.
Both fields are there:
latitude: The requested latitude.
longitude: The requested longitude.
timezone: The IANA timezone name for the requested location (e.g. America/New_York). This is the timezone used for text forecast summaries and for determining the exact start time of daily data points. (Developers are advised to rely on local system settings rather than this value if at all possible: users may deliberately set an unusual timezone, and furthermore are likely to know what they actually want better than our timezone database does.)
offset: The current timezone offset in hours from GMT.
I have been using this API for a while.

How do I get zone offset for a location?

I have an API written in Spring. For the date and time properties, I use the Java Time API (more specifically LocalDateTime) and an Android client which is heavily reliant on time related information.
When the client issues a request, it can send the city where the user is located.
How can I obtain a ZoneOffset from the information in the request so the date and time in the response are appropriate to the user's location?
If you know the capital of the state you are in, you can use it's TimeZone as these are available. The trick is knowing which state or province you are in.
If you know the lat/lot you can work out the likely difference from GMT and whether you are in day light saving but this would be approximate.

Categories

Resources