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

Added offsetMinutes and managed negative offset #4

Open
wants to merge 1 commit into
base: lesson-35
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
41 changes: 24 additions & 17 deletions world_time_app/lib/services/world_time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,47 @@ import 'package:http/http.dart';
import 'dart:convert';
import 'package:intl/intl.dart';

class WorldTime {
class WorldTime{
String location; // location name for the UI
String time; // time in that location
String flag; // url to asset flag icon
String url; // location url for API endpoint
bool isDayTime; // true or false / day or night

String location; // location name for UI
String time; // the time in that location
String flag; // url to an asset flag icon
String url; // location url for api endpoint
bool isDaytime; // true or false if daytime or not

WorldTime({ this.location, this.flag, this.url });
WorldTime({ this.location, this.flag, this.url});

Future<void> getTime() async {

try{
// make the request
try {
//make the request
Response response = await get('http://worldtimeapi.org/api/timezone/$url');
Map data = jsonDecode(response.body);
print(data);


// get properties from json
//get properties from data
String datetime = data['datetime'];
String offset = data['utc_offset'].substring(1,3);
int offsetHours = int.parse(data['utc_offset'].substring(1,3));
int offsetMinutes = int.parse(data['utc_offset'].substring(4,6));

if (data['utc_offset'].substring(0, 1) == "-") {
offsetHours = -offsetHours;
offsetMinutes = -offsetMinutes;
Comment on lines +29 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`now = now.subtract(Duration(hours: offsetHours, minutes: offsetMinutes));
No need to change offset sign

}

// create DateTime object
DateTime now = DateTime.parse(datetime);
now = now.add(Duration(hours: int.parse(offset)));
now = now.add(Duration(hours: offsetHours, minutes: offsetMinutes));

// set the time property
isDaytime = now.hour > 6 && now.hour < 20 ? true : false;
// set time property
isDayTime = now.hour > 6 && now.hour < 19 ? true : false;
time = DateFormat.jm().format(now);
}
catch (e) {
print(e);
time = 'could not get time';
time = 'Could not get the requested time, sorry!';
}


}

}