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.)
Related
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.
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.
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.
I have a gregorian calendar that serializes to the string below during a soap request
2079-07-07T00:00:00.000-07:00
The .NET webservice reads it as the string below
07/07/2079 01:00:00
Is the 07:00 causing the issue? If so how can I get rid of this?
Someone might be able to give you an affirmative answer if you tell us which time zone is configured on your system, but I would assume a mismatch in the DST rules.
The JVM comes with its own timezone and DST rule database, while Windows (and .NET) uses a different database. In theory, the two databases should of course contain the same rules, but I have ran into differences in the DST rules for historical dates. I would assume there might be differences for dates far in the future as well.
If you actually want to transport a date value (no time component) over the SOAP service, the easiest solution would be to use the appropriate XML Schema datatype instead of a datetime type.
Use a DateTimeOffset in your .net code and it will work just fine.
I want to basically monitor the server calls on java side. So is there any way I can directly obtain the timestamp when request was received .
In other words,I do not want to explicitly make Date Objects and find out the timestamp. Rather I am interested if there's any timestamp parameter utility provided with Request Object.
Something provided by ServletActionContext.getRequest() or any available solution without the need to code to create Date() objects and find out.
The reason I am saying is I have around 50-60 server calls and may be even more in future in my application so each time i need code in application layer rather use this ready-made facility.
HttpServletRequest.getDateHeader gives you a timestamp as a long value.
Although this is from the request header and not the receieved time, it might work since it provides milliseconds since the epoch.
You can also look into Filter, which allows you to
Examples that have been identified for this design are:
1. Authentication Filters
2. Logging and Auditing Filters
...
Event Listeners might be another facility, where you can hook into the processing of a servlet.
Back to your question:
ServletActionContext.getRequest() -> HttpServletRequest.getSession() ->
HttpSession.getLastAccessedTime
Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT, and marked by the time the container received the request.