iamshaunjp / flutter-beginners-tutorial

All course files for the Flutter Beginners playlist on The Net Ninja YouTube channel.
1.75k stars 1.23k forks source link

Wrong time for UTC -ve values #47

Open Nissimarcus11 opened 3 years ago

Nissimarcus11 commented 3 years ago

Showing wrong time for New York

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

      // get properties from json
      String datetime = data['datetime'];
      String offset = data['utc_offset'].substring(1,3);
      String sign = data['utc_offset'].substring(0,1);
      print("sign is $sign");

      // create DateTime object
      DateTime now = DateTime.parse(datetime);
      now = sign == '+'? now.add(Duration(hours: int.parse(offset))):now.subtract(Duration(hours: int.parse(offset)));

      // set the time property
      isDaytime = now.hour > 6 && now.hour < 20 ? true : false;
      time = DateFormat.jm().format(now);
    }

Add ternary operator to read UTC value and add/substract based on that