日期时间时区反序列化 [英] Datetime timezone deserialization
问题描述
我已经为我的应用开发了Rest API.它以以下格式 2018-09-07T17:29:12 + 02:00
将日期发送到应用程序,我猜+2:00表示我的时区是一个对象的一部分.
I've developed a Rest API for my app. It sends to the app dates in the following format 2018-09-07T17:29:12+02:00
, where I guess +2:00 represents my timezone as part of one object.
在Flutter应用中,反序列化接收到的对象后,它会将实际接收到的DateTime对象减去两个小时.
In my Flutter app, once I deserialize the received object, it substracts two hours to the actual received DateTime object.
我要反序列化的类定义如下:
The class I'm trying to deserialize is defined as follows:
import 'package:json_annotation/json_annotation.dart';
part 'evento.g.dart';
@JsonSerializable(nullable: false)
class Evento {
final int id;
final String nombre;
final String discoteca;
final int precio_desde;
final int edad_minima;
final DateTime fecha_inicio;
final DateTime fecha_fin;
final DateTime fecha_fin_acceso;
final String cartel;
final bool incluyeCopa;
Evento(this.id, this.nombre, this.discoteca, this.precio_desde, this.edad_minima, this.fecha_inicio, this.fecha_fin, this.fecha_fin_acceso, this.cartel, this.incluyeCopa, this.num_tipos);
factory Evento.fromJson(Map<String, dynamic> json) => _$EventoFromJson(json);
Map<String, dynamic> toJson() => _$EventoToJson(this);
}
推荐答案
DateTime
只能表示本地时间和UTC时间.
DateTime
can only represent local time and UTC time.
它支持解析时区偏移,但将其标准化为UTC
It supports timezone offset for parsing, but normalizes it to UTC
print(DateTime.parse('2018-09-07T17:29:12+02:00').isUtc);
打印 true
.
然后只能使用 toLocal()
或 toUtc()
这篇关于日期时间时区反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!