您的位置:首页 > 移动开发

Tips for working with time zone in flex app

2010-06-26 20:37 495 查看
1. Understand how date and time are determined by flex (player on the client machine)
From flex doc we can see:
"The Date class lets you retrieve date and time values relative to universal time (Greenwich mean time, now called universal time or UTC) or relative to local time, which is determined by the local time zone setting on the operating system that is running Flash Player. "

This mean if one user's machine is using "Automatically adjust clock for daylight saving changes", the time displaied in flex app may be different with another user who is running the same flex app without “Automatically adjust clock for daylight saving changes” on his machine.

"Automatically adjust clock for daylight saving changes" can be checked from
Control Panel --> Date and time -->Time Zone.

2. Understand how date and time are determined by Java ( On server machine)
1). The JVM determines the correct Coordinated Universal Time (UTC) by comparing the QUTCOFFSET value to the local time for the system
The JVM returns the correct local time to the system by using the Java system property user.timezone.
The default value for QUTCOFFSET is zero (+00:00).
The QUTCOFFSET value allows the JVM to determine the correct value for the local time. For example, the value for QUTCOFFSET to specify central standard time (CST) is -6:00. To specify central daylight time (CDT), QUTCOFFSET has a value of -5:00.
The user.timezone Java system property uses UTC time as the default value. Unless you specify a different value, the JVM recognizes UTC time as the current time.
See more details in here

2). you need correct patches for Java to get correc date and time.

-- There is a patch for jdk you may want to double check:
http://sunsolve.sun.com/search/document.do?assetkey=1-26-102836-1
-- if you are using Solaris, you may need this patch
http://sunsolve.sun.com/search/document.do?assetkey=1-26-103044-1
-- if you are using 1.4.2_10 or lower version of JDK, you need this patch
http://kb.adobe.com/selfservice/viewContent.do?externalId=kb400722

3). To verify your time zone setting in JDK, read this
http://publib.boulder.ibm.com/infocenter/wasinfo/v4r0/index.jsp?topic=/com.ibm.support.was.doc/html/WebSphere_Application_Server/1173447.html
Download time.jsp from there, and run it on your server.
To change timezone setting in Java, you can use java arg:
-Duser.timezone=America/Los_Angeles
If you are using Jrun as app sever, this can be change in JRun_install/bin/jvm.config file:
java.args=-Xms32m -Xmx384m -Dsun.io.useCanonCaches=false -Duser.timezone=Etc/UCT

3. synchronize time znoe on client and server
If you want to enter time in the client’s local time - regardless of the timezone your in at the time of data entry, and keep the date in UTC zone on both server and client side, see example

Note, don’t forget you also need to set JAVA VM timezone to UTC for this example.

Flex local dates/time transfer issue

First, let me explain how Flex deals with Date transfer. Dates are transfered to/from Flex client as UTC date – no timezone information available. Transfer to the UTC/local time happens automatically on protocol level. As a result, if the server does not know client’s timezone, it can not derive the entered time – rather it operates on Global time only. It means that if I am on the East coast and entered 1PM, person in Denver will see 11AM.
Depending on the type of application it can be desirable behavior – however in most of the applications it is not. Sometimes I want to enter time in the client’s local time – regardless of the timezone I am in at the time of data entry. It means that application has to operate without timezone – or for that matter in one timezone. Usually it is solved either on the server side by keeping client timezone information in session and adjusting dates on each transfer or by communicating date as String. In either case it requires a lot of code and constant tracking of the issue.

However, it can be solved much easier with usage of transient tag/keyword during data transfer:

AS3:

package com.farata.datasource.dto
{
import flash.events.EventDispatcher;
[RemoteClass(alias="com.farata.datasource.dto.LineItemDTO")]
[Bindable(event="propertyChange")]
public dynamic class LineItemDTO extends EventDispatcher //implements IManaged
{

private var _myDate : Date;

public function get myDateUTC() : Date{
return _myDate ==null?null:new Date(_myDate.valueOf() – _myDate.getTimezoneOffset()*60000);
}
public function set myDateUTC( value : Date ):void{
var oldValue:Object = _myDate;
if (oldValue !== value) {
this._myDate = value == null?null:new Date(value.valueOf() + value.getTimezoneOffset()*60000);
}
}
[Transient]

public function get myDate() : Date{
return _myDate;
}
public function set myDate( value : Date ):void{
var oldValue:Object = this._myDate;
if (oldValue !== value) {
this._myDate = value;
dispatchUpdateEvent(“myDate”, oldValue, value);
}
}

Java:
package com.farata.datasource.dto;

import java.io.Serializable;
import java.util.*;

public class LineItemDTO implements Serializable
{

transient public java.util.Date myDate;
public java.util.Date getMyDateUTC()
{
return myDate;
}

public void setMyDateUTC(java.util.Date value)
{
this.myDate = value;
}
}

That is all – you have normal public variables on both sides, and serialization works transparently, keeping Date in UTC zone on both sides (you also need to set JAVA VM timezonne to UTC) – and now you are always in the servers timezone.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐