0

Zk 9.5 Datebox and Timezones

asked 2022-03-09 21:38:37 +0800

zkulm gravatar image zkulm
100 1

Hello All,

we upgraded to Zk 9.5.0.3 recently and since then we have a problem with the datebox and timezones:

E.g. the server stands in Portugal (UTC+0), because most users are in Portugal, but some are also in Germany (UTC+1), if they now select a date in a datebox the server will get the day before the selected 23:00 from the databox as value. Since we are only interesting in the date and not in the time we normaly just look at the date and ignore the time itself, which then leads to an error, because we have the wrong date. Is there any way to deactivate this timezone adjustment or timehandling of dateboxes in generel? Because we would like to prevent to convert the time back to the client timezone, everytime we get the value of a datebox.

(This also happens if you can only select a year in the datebox, in this case you will get the last day of the previous year 23:00 a clock)

Before we had zk version 8.5.0 running, and there this was not happening.

Best Regards,

Matthias

delete flag offensive retag edit

6 Answers

Sort by ยป oldest newest most voted
0

answered 2022-03-10 17:54:37 +0800

hawk gravatar image hawk
3048 1 5
http://hawkphoenix.blogsp... ZK Team

Could you show us how do you setup a datebox (a zul snippet or java code snippet)?

link publish delete flag offensive edit
0

answered 2022-03-10 21:26:32 +0800

zkulm gravatar image zkulm
100 1

updated 2022-03-21 09:25:26 +0800

hawk gravatar image hawk
3048 1 5
http://hawkphoenix.blogsp... ZK Team

Hello hawk,

yes sure: We have a tomcat server running on a Pc with timezone in Lisabon or we set this Java argument to fake this: -Duser.timezone=Europe/Lisbon Now we have a Browser running on a PC with German timezone. (I switched in this example to zk version 9.6.0, to have the selectLevel on the datebox)

Example zul:

<datebox id="DAY_SELECTION" lenient="false"> </datebox> <datebox id="MONTH_SELECTION" lenient="false" selectLevel="month"> </datebox> <datebox id="YEAR_SELECTION" lenient="false" selectLevel="year"> </datebox> <button label="Syout" forward="onClick=onSysout"> </button>

Example Java:

  public void onSysout (Event e) {
  Datebox day = (Datebox) contentWindow.getFellowIfAny("DAY_SELECTION");
  Datebox month = (Datebox) contentWindow.getFellowIfAny("MONTH_SELECTION");
  Datebox year = (Datebox) contentWindow.getFellowIfAny("YEAR_SELECTION");

  Date dayDate = day.getValue();
  Date monthDate = month.getValue();
  Date yearDate = year.getValue();

  System.out.println("Date Day: " + dayDate.toString());
  System.out.println("Date Month: " + monthDate.toString());
  System.out.println("Date Year: " + yearDate.toString());

  System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(dayDate));//Formatting to date, because we expect only a date here
  System.out.println(new SimpleDateFormat("yyyy-MM").format(monthDate));//Formatting to year-month, because we expect only a month here
  System.out.println(new SimpleDateFormat("yyyy").format(yearDate));//Formatting to year, because we expect only a year here
}

Now you can select a day, a month and a year in the dateboxes: image description

If you now click on the Button you will get the following Console output:

2022-03-10 13:23:34,871 | SOUT | Thread-12            | Date Day: Tue Mar 08 23:00:00 WET 2022
2022-03-10 13:23:34,871 | SOUT | Thread-12            | Date Month: Thu Jun 30 23:00:00 WEST 2022
2022-03-10 13:23:34,872 | SOUT | Thread-12            | Date Year: Sat Dec 31 23:00:00 WET 2022
2022-03-10 13:23:34,872 | SOUT | Thread-12            | 2022-03-08
2022-03-10 13:23:34,872 | SOUT | Thread-12            | 2022-06
2022-03-10 13:23:34,872 | SOUT | Thread-12            | 2022

As I said this didn't happen in zk version 8.5.0, and espiacialy if you can select only a month or year it is quite strange that the timezones affect this.

Best Regards, Matthias

link publish delete flag offensive edit
0

answered 2022-03-11 12:52:32 +0800

hawk gravatar image hawk
3048 1 5
http://hawkphoenix.blogsp... ZK Team

Currently, I can't reproduce what you see in my local environment with your code and settings. The server always prints the same date/time as the datebox input.

could you check what does the onchange request send?

image description

If enforcing the timezone of the whole application according to this doc, does it solve the problem?

link publish delete flag offensive edit
0

answered 2022-03-11 15:36:11 +0800

zkulm gravatar image zkulm
100 1

Hello hawk,

I added

   <library-property>
       <name>org.zkoss.web.preferred.timeZone</name>
       <value>GMT+1</value>
   </library-property>

to the zk.xml, but this didn't change anything.

In the onChange event it is already wrong: image description

Best Regards, Matthias

link publish delete flag offensive edit
0

answered 2022-03-11 19:04:34 +0800

MDuchemin gravatar image MDuchemin
2213 1 5
ZK Team

Hi there!

So, time is a complicated topic, since it mostly depends which time we are talking about. The main key in ZK time handling (in current ZK 9.X) is that ZK uses a "universal" time handling based on modern Java APIs (for a given definition of modern, since it's Java 8+).

MAIN ANSWER IS HERE, everything below is extra fluff because time is HARD

if your usecase is to retrieve a LocalDate, LocalTime or LocalDateTime from the user, because you want to do something relative to the user's own position in time, from ZK 9.X, you can access datebox.getValueInLocalDate(), datebox.getValueInLocalTime(), datebox.getValueInLocalDateTime()

This is useful if you don't want to synchronize events from multiple timezones, but instead care about the user's localtime in relation to the user themselves.

If the user enters 00:00:00, you will get 00:00:00 out of that value.

(also applies to cases where a selected day is one day earlier or later than the server timezone)

ADDITIONAL FLUFF ABOUT TIME AND TIME BEING HARD

When you are retrieving a Date from a time-based components (datebox, timebox), you can either retrieve a "Universal" date (i.e a date converted to the server's own timezone) or you can retrieve a "local" date (i.e. a date expressed in the user's input timezone).

User in gmt+8, server in GMT+4. datebox value is "2022-03-11 00:00:00" datebox.getValue() will give you a GMT+4 date object "GMT+4: 2022-03-10 16:00:00"

This is useful if you are trying to sync timed events between different users. For example, in a chatbox, you don't want each user message showing up "as they are displayed in each of their timezones". Instead, you'd want each user message to show as converted to the local user's timezone.

For some operations on time and what you would expect out of them, have a look to this fiddle.

Note that the server timezone and the Datebox timezone are not necessarily the same, and that in this fiddle I synchronize the user timezone to the datebox timezone, which is also not the same at either the server timezone or the browser's timezone.

Note that all of that is also relative to Server's time, which is defined by zk.xml, by JVM launch parameter, by thread local timezone, etc. There is a lot of things that will influence "which time" you are talking about when trying to set or retrieve a time :)

link publish delete flag offensive edit
0

answered 2022-03-21 16:07:59 +0800

hawk gravatar image hawk
3048 1 5
http://hawkphoenix.blogsp... ZK Team

In your screenshot, the date-time in au request is 1 hour behind 2024.01.01, I assume you set the time zone of that datebox with GMT+1 and your application time zone is GMT+2.

If you don't specify the timezone of a datebox, it will be the same time zone as your application (determined by predefined precedence, please read https://www.zkoss.org/wiki/ZKDeveloper%27sReference/Internationalization/Time_Zone#Overview)

see this zkfiddle example.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2022-03-09 21:38:37 +0800

Seen: 21 times

Last updated: Mar 21

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More