Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Offset Issues - positive/negative & hours/minutes #39

Open
wants to merge 1 commit into
base: lesson-34
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions world_time_app/lib/services/world_time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down