Converting Olson's timezone identifier to TimeZoneConstant in GWT (client side)

We store our dates stored in milliseconds from the time of the epoch and the Olson time zone identifier for the objects that we want to display time-related data.

How can I convert Olson TZID to TimeZoneConstant to create a TimeZone and use DateTimeFormat?

// values from database String tzid = "America/Vancouver"; long date = 1310771967000L; final TimeZoneConstants tzc = GWT.create(TimeZoneConstants.class); String tzInfoJSON = MAGIC_FUNCTION(tzid, tzc); TimeZone tz = TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(tzInfoJSON)); String toDisplay = DateTimeFormat.getFormat("y/M/dh:m:sav").format(new Date(date), tz); 

Is there a MAGIC_FUNCTION? Or is there another way to do this?

+4
source share
2 answers

Running GWT.create in the TimeZoneConstants class is a bad game according to the GWT Javadoc [1]. So what I did was create a server-side class that parses / com / google / gwt / i 18n / client / constants / TimeZoneConstants.properties and creates a cache of all JSON objects for each time zone (their Olson TZID is indexed).

My site runs on jboss, so I copied TimeZoneConstants.properties to my war / WEB-INF / lib directory on my site (probably it didn’t need to be copied, since there are already GWT banks). Then I have a singleton class that does parsing when building:

 InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES_FILE); InputStreamReader isr = new InputStreamReader(inStream); BufferedReader br = new BufferedReader(isr); for (String s; (s = br.readLine()) != null;) { // using a regex to grab the id to use as a key to the hashmap // a full json parser here would be overkill Pattern pattern = Pattern.compile("^[A-Za-z]+ = (.*\"id\": \"([A-Za-z_/]+)\".*)$"); Matcher matcher = pattern.matcher(s); if (matcher.matches()) { String id = matcher.group(2); String json = matcher.group(1); if (!jsonMap.containsKey(id)) { jsonMap.put(id, json); } } } br.close(); isr.close(); inStream.close(); 

Finally, I make an RPC call to get TimeZoneInfoJSON for the client (if the server knows which TimeZoneID is interesting to me):

 getTimeZone(new PortalAsyncCallback<String>() { public void onSuccess(String tzJson) { timeZone = TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(tzJson)); } }); 

Not the most elegant solution, but it gave me a way to display dates and times for a specific time zone on DST transitions.

[1] http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/i18n/client/constants/TimeZoneConstants.html

+4
source

@thoughtcrimes, please tell us how you created the timeZoneConstants.properties file. I need to upgrade it from version 25 to the latest version of CLDR.

0
source

Source: https://habr.com/ru/post/1386616/


All Articles