How to get the "Real" TimeZone of browser with GWT - java

I need the real timezone ID but NOT of fake / generic timezone like Etc/GMT-xxx.
I understand that there are some solution to get the offset, or even the "fake" timezone, with the commands:
int offset = new Date().getTimezoneOffset();
String timezone = TimeZone.createTimeZone(offset).getID();
And currently our project is using this. However, this will only give you the summarized offset, but you will never know if this offset actually contains the DST (Daylight Saving Time / Summer Time) offset. Or in other words : you know the total offset is 2 hours, but it wont tell if its actually 2 + 0, or 1 + 1, and it really does matter in my use case.
Here is our use case : We have a function that allow user to upload excel file, where the function will parse the excel file and insert the records into system. Each of the record will have different Date Time. Our function have to check the below:
If the browser TimeZone (from where the user upload the file) subject to DST_offset.
If it set subject to DST, we then further decide if the Date is fall under "summer".
Our system then offset the date time and store into DB accordingly.
So, inside a batch excel file, the multiple records there some may have DST Offset while some may not.
I understand that we can use the java Calendar to achieve the above logic 1 & 2, from this link https://www.baeldung.com/java-daylight-savings. But however, the pre-requisite is : we need the real timezone ID e.g. "Europe/Rome", but not the fake or general one "Etc/GMT-xxx".
Please advise how could we get the "real" timezone ID in Smart GWT? or if any alternative to handle the mentioned use case?

I added ZoneId.systemDefault() a few days ago to https://github.com/adrianmsmith/gwt-time which hopefully should do exactly what you want. It returns the zone like “Europe/Rome”, from the browser, if the browser makes this info available (IE does not, for example). Feel free to reach out to me via my email address on my github account if you have any issues.

Related

Store customized date in database

I have an application which is used by all over world. But the application server is present in India.
One of my application features is
**create template using date
So, for that I have to store the client systems date & time.
But I need to store date time based on Client side or server side in single column in database?
What is the best approach to do this?
I think you have a few options here, depending on how you wish to delegate responsibility and how the templates will be used.
The simplest solution is simply that the client submits the date & time and you store it as is with no time zone information. This however assumes that the template would only be used in the same time zone, or that it is acceptable to have the same date & time visible everywhere according to where it was created (probably not the best approach for a worldwide system)!
Another option is that you store the value as UTC. Then you can either:
Make the client responsible for converting to their local time
zone
Store the offset / time zone separately and include it in the result
This is probably the best approach
Or another option is to store the date & time together with the time zone it was created within. Then you might have to convert between different time zones either on the server or client side if you need a different one.

change mySQL localTime to current time

I'm from Egypt and my time is +2 GMT , but my on-line server is on -5 GMT
so when calling some queries fro database to get time for 2 AM ! I got the data from Yesterday not for today
so I'm asking how to fix such problem using mySQL statement or Java code ?
for example when I called :
SELECT DAYOFWEEK(CURDATE())
It getting right value 3 , but if I called it on 3 AM local Time here in Egypt (1 GMT) it get it 2 ! .
The best way to handle time within a database is to always store the "UTC" time (even if you have to convert from local time when storing a timestamp). Then, when you retrieve the time you convert it to the "local time" of the client (browser usually in today's modern world) by applying the locale information.
If you use this process, you eliminate having to know where your server is located (and with hosting providers sometimes in locations you do not know ...).

How to manage time properly mysql

I have already a table with three time fields. One to register the hour when the user start working, when stop working and the difference. Now I want to add the day of the week(Mon-Sun) and the date. How can I properly do that, saying that then I will want to grab the hours that the user worked in the past 7 days, lets say.
I've read that timestamp give all the information but I don't know whether I can separate days from date?
**table name = date_time
date_time_id = auto increment
user_id = var
time_in = time
time_out = time
time_dif = time**
ps: I am using java(servlets) and mysql.
Thanks guys
when I start getting really silly with yime in MySql, I use unix time. MySql has built in functions for unix time conversion, too. FROM_UNIXTIME() is one of them. Here's url of a reference page I use.
I've been burned by MySql time (probably more by my own confusion) a few times, so I prefer unix time and then I can manually figure things out by factoring seconds into minutes *60 and hours *60*60 and days *24*60*60.
Change your column types to TIMESTAMP instead of TIME. A timestamp includes both a date and a time.
You should hav column datetime type So you can easily get days month week whatever required.
Here all functions listed you can use as desired with datetime field:
Date and Time Functions
Use org.joda Datetime java library. Its very easy and you can do all the calculations using timestamp.

How do i get date from database other than GMT format?

Trying to get a time stored in a datadase.
select dbtimezone from dual gives me -07:00
I am using Java program to get the Date from Oracle
Column i type of Date.
while i am fetching the time in my java program am getting it as GMT.
actually i want the time as it is there in database not converted time.
Though i can convert back to -07:00 , i am seeking another way to do because conversion always depends on the dbtimezone of the database using.
Can any one help me ?
Thanks in advance
That oracle just has one timezone can make life difficult if you deal with different timezones. I've always thought life was easier if you consider timezone a view artifact and
represent all times as UTC, then convert in the view. You put the timezone information someplace in the database and convert accordingly.
...actually getting that right can get interesting because you don't want to make the same mistake of being too general again. For example, a client may be based in a particular timezone, but have offices in many. Though an office is in a particular timezone, the activity relating to the time may involve a different timezone etc.
See java.util.TimeZone, more specifically the getOffset methods, which return the number of milliseconds to add to the UTC time to get local time. Note that it also considers the daylight saving time.

How to use Java timezone id in a Windows (non-Java) application?

I need to add timezone information to a db table with user maintained locations. The data will be accessed mostly from Java code but there is also some PL/SQL and Win32 (Delphi) code which needs to understand the timezone information.
It seems straight forward to use the id from java.util.TimeZone. Java can easily convert that (obviously), Hibernate has built-in support for it and apparently also Oracle understands those timezone ids:
select TZ_OFFSET('Pacific/Marquesas') from dual.
The problem is: the timezone ids do not seem to be compatible with the Windows Timezone DB. For example, the java.util.timezone id "Pacific/Marquesas" (-09:30) is not in the timezone picklist in Windows. The registry does not contain it at all; see
\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
Here I can only pick either -09:00 or -10:00. So, if I were to store the timezone like this, how can I get to the actual offset/DST infos in Windows (without Java)? Hopefully this does not require a mapping table which I have to keep up to date whenever it changes. Is there a globally accepted standard which works better than the java timezone id?
Update
The timezone info is used in combination with DATE columns on the database. Those columns contain local date/time values. If a location can be associated with those values, the location's timezone enables me to convert the date/time value to UTC or any other timezone whenever needed.
I realize that instead of DATE a TIMESTAMP_TZ data type or something similar would be more appropriate. However, this would require a data migration (for which the TZ is required again) and is not supported by the legacy applications which also work on the data (unless a lot of code is changed). The problem is almost the same if I had to convert the values to UTC.
Bottom line is I need to keep the DATE values in local time but I need to know for some of them which TZ that means.
I can give a little background, if not a real answer.
Many systems use the Olson implementation of timezone data. So those names work in many systems (most Unix, Java, Oracle I think). Microsoft does their own thing.
I see at the bottom of that Wikipedia link there's a reference to some mapping to the Windows world.
Good luck!
I realize this is not the best way to do it, but it might be sufficient in your case. Without knowing all the requirements I can't tell.
What do you need to use the time zone information for? Just to present the time with the correct offset and maybe also the name of the time zone?
You could continue to use Java to determine what the offset of the user is by looking up the user's selected time zone using Java. Each time the user logs in record in your database what the offset currently is. Then other apps can look at this information to determine how to format the time.
This assumes that users who regularly login are the ones that this needs to be done for. If that's not the case you could run a daily job to lookup the time zone for each user in Java and record the offset currently in effect.
Hackish, agreed, but the only other way I see is to maintain a mapping. And what happens when someone selects a time zone that you don't have a mapping for?

Categories