From 67a52b4cfd0c773332c06652b385e15d26e0be1d Mon Sep 17 00:00:00 2001 From: Shyam Sundar Bharathi <77499506+Shyam-Sundar-Bharathi@users.noreply.github.com> Date: Wed, 14 Apr 2021 22:00:18 +0530 Subject: [PATCH] Update world_time.dart --- world_time_app/lib/services/world_time.dart | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/world_time_app/lib/services/world_time.dart b/world_time_app/lib/services/world_time.dart index 1c697b9..781bb13 100644 --- a/world_time_app/lib/services/world_time.dart +++ b/world_time_app/lib/services/world_time.dart @@ -21,11 +21,24 @@ class WorldTime { // get properties from json String datetime = data['datetime']; - String offset = data['utc_offset'].substring(0,3); - - // create DateTime object + + //decoding offset sign, hours and minutes + String offset_sign = data['utc_offset'].substring(0,1); + String offset_hours = data['utc_offset'].substring(1,3); + String offset_minutes = data['utc_offset'].substring(4,); + + //parsing DateTime DateTime now = DateTime.parse(datetime); - now = now.add(Duration(hours: int.parse(offset))); + + //adding or subtracting hours and minutes offset depending on +/- sign of offset respectively + if(offset_sign=='+') + { + now = now.add(Duration(hours: int.parse(offset_hours),minutes: int.parse(offset_minutes))); + } + else if(offset_sign=="-") + { + now = now.subtract(Duration(hours: int.parse(offset_hours),minutes: int.parse(offset_minutes))); + } // set the time property isDaytime = now.hour > 6 && now.hour < 20 ? true : false;