Last active
June 15, 2023 21:45
-
-
Save hasimyerlikaya/cc134ea8a0743ab8ed582e232b907238 to your computer and use it in GitHub Desktop.
UTC - Local DateTime Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:json_annotation/json_annotation.dart'; | |
class UTCDateTimeConverter implements JsonConverter<DateTime, String> { | |
const UTCDateTimeConverter(); | |
@override | |
DateTime fromJson(String dateTime) { | |
return DateTime.parse(dateTime).toLocal(); | |
} | |
@override | |
String toJson(DateTime dateTime) { | |
return dateTime.toUtc().toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JsonSerializable() | |
@UTCDateTimeConverter() | |
class User { | |
String name; | |
DateTime lastLoginDate; | |
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json); | |
Map<String, dynamic> toJson() => _$UserToJson(this); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment