服务器端更改客户端发送的日期/时间 [英] Server side changes date/time sent by client

查看:101
本文介绍了服务器端更改客户端发送的日期/时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临以下问题,我有一个使用angular和java的play应用程序.客户端在一个页面中选择一个日历中的日期和时间,例如 2015-04-03 15:00 ,该数据作为日期放置在javascript对象中,然后提交给我的服务器但它表明服务器正在将此日期/时间转换为节省时区的时间2015-04-03 16:00 ,而不是客户端发送的 15:00 .

将数据提交到服务器后,重新加载页面时,该数据将保留在数据库中,该页面显示的日期要少1小时

将数据发送到服务器.注意,有一个console.info()打印日期时间.它正在打印正确的日期/时间.用户选择的日期/时间.

$scope.confirmCallback = function () {
                $scope.schedule.client = $scope.client;
                $scope.schedule.type = 'CONTACT';
                console.info($scope.schedule.date); //PRINTS OK DATE/TIME
                ScheduleRepository.create($scope.schedule).then(function () {
                    Alert.success('views.schedule.message.successSaved');
                    $scope.schedule = {};
                    $scope.tableSchedules.reload();
                }, function () {

                });
            }

这是在我服务器端的接收请求的控制器上.当我检查json时,请求到达服务器的那一刻,我可以看到日期时间值不同于我发送的日期时间值.我想这与客户端和服务器端的时区有关.

@Dynamic("CREATE_SCHEDULE, EDIT_SCHEDULE")
public static Result save() {
    try {
        JsonNode request = request().body().asJson();//SHOWS DIFFERENT DATE/TIME
        ScheduleClient scheduleClient = JSONUtils.fromJson(request, ScheduleClient.class);

任何建议如何解决此问题?预先感谢

解决方案

要实现的几件事:

  • 不能通过导线发送Date对象 .它必须序列化.
  • 没有用于JSON的本机日期序列化格式,但是最佳实践惯例是发送 ISO-8601 / RFC3339 序列化的字符串.
  • JS Date对象采用其运行所在的时区.因此,如果您在其上调用toISOString(或者您的ScheduleRepository调用了它),则会使用该时区将其转换为UTC.
  • 在接收端,您的JSONUtils.fromJson调用会将字符串值反序列化为您的ScheduleClient类使用的任何对象结构.
  • 如果该对象也具有本地时间行为,它将使用服务器的本地时区.

因此,您要么是由于将本地值与UTC值进行比较,要么是由于将本地时间值与另一个时区的本地时间进行比较而看到时差.

由于没有显示代码的重要部分,因此很难给出更准确的建议.我们将需要查看Date对象的原始分配,序列化代码,通过网络发送的字符串值,反序列化代码以及要反序列化为的类结构.我们还需要一些 context 来了解您的用户是在特定的通用时刻选择日期和时间,还是在其时区本地特定的日期和时间,或者只是日历日期,要不然是啥.上下文是关键,并且您没有提供太多继续工作.

I'm facing the following problem, I have a play application with angular and java. In one page the client selects in a calendar some date and time for example 2015-04-03 15:00 this data is put in a javascript object as a Date and later this data is submit to my server but it seams the server is converting this date/time to his timezone saving 2015-04-03 16:00 instead of 15:00 as the client side sent.

After I submit the data to the server where is persisted in the database when I reload the page shows the date with 1 hour less

Sending data to server. Notice that there is a console.info() that prints the date time. It's printing the correct date/time. The date/time selected by user.

$scope.confirmCallback = function () {
                $scope.schedule.client = $scope.client;
                $scope.schedule.type = 'CONTACT';
                console.info($scope.schedule.date); //PRINTS OK DATE/TIME
                ScheduleRepository.create($scope.schedule).then(function () {
                    Alert.success('views.schedule.message.successSaved');
                    $scope.schedule = {};
                    $scope.tableSchedules.reload();
                }, function () {

                });
            }

Here is on my server side on a controller that receives the request. The moment the request gets on the server if I inspect the json I can see that the date time value is different from the one I sent. I guess is something related to timezone on client side and server side.

@Dynamic("CREATE_SCHEDULE, EDIT_SCHEDULE")
public static Result save() {
    try {
        JsonNode request = request().body().asJson();//SHOWS DIFFERENT DATE/TIME
        ScheduleClient scheduleClient = JSONUtils.fromJson(request, ScheduleClient.class);

Any suggestions how to solve this problem? Thanks in advance

解决方案

A couple of things to realize:

  • A Date object can't be sent across the wire. It has to be serialized.
  • There's no native date serialization format for JSON, but the best-practice convention is to send an ISO-8601 / RFC3339 serialized string.
  • The JS Date object takes on the time zone of where it is running. So if you call toISOString on it (or if your ScheduleRepository does), it will be converted to UTC using that time zone.
  • On the receiving end, your JSONUtils.fromJson call will deserialize the string value back to whatever object structure your ScheduleClient class uses.
  • If that object also takes on local time behavior, it will use the local time zone of the server.

So you are either seeing the time difference due to comparing local values to UTC values, or by comparing local time values to another time zone's local time.

It's difficult to give more exact advice on what you should do, as you didn't show the important parts of your code. We would need to see the original assignment of the Date object, the serialization code, the string value as sent over the wire, the deserialization code, and the class structure that was being deserialized into. We would also need some context to understand whether your user is selecting a date and time at a particular universal instant, or a particular local-by-their-timezone date and time, or just a calendar date, or what. Context is key, and you haven't provided much to go on.

这篇关于服务器端更改客户端发送的日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆