WorldTimeAPI Not working

So I’m using this script to get the real world time from the internet:

using UnityEngine;
using System;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine.Networking;

public class WorldTimeAPI : MonoBehaviour {
#region Singleton class: WorldTimeAPI

    public static WorldTimeAPI Instance;

    void Awake ( ) {
        if ( Instance == null ) {
            Instance = this;
            DontDestroyOnLoad ( this.gameObject );
        } else {
            Destroy ( this.gameObject );
        }
    }

#endregion

    //json container
    struct TimeData {
        //public string client_ip;
        //...
        public string datetime;
        //..
    }

    const string API_URL = "http://worldtimeapi.org/api/ip";

    [HideInInspector] public bool IsTimeLodaed = false;

    private DateTime _currentDateTime = DateTime.Now;

    void Start ( ) {
        StartCoroutine ( GetRealDateTimeFromAPI ( ) );
    }

    public DateTime GetCurrentDateTime ( ) {
        //here we don't need to get the datetime from the server again
        // just add elapsed time since the game start to _currentDateTime

        return _currentDateTime.AddSeconds ( Time.realtimeSinceStartup );
    }

    public IEnumerator GetRealDateTimeFromAPI ( ) {
        UnityWebRequest webRequest = UnityWebRequest.Get ( API_URL );
        Debug.Log ( "getting real datetime..." );

        yield return webRequest.SendWebRequest ( );

        if ( webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError) {
            //error
            Debug.Log ( "Error: " + webRequest.error );

        } else {
            //success
            TimeData timeData = JsonUtility.FromJson<TimeData> ( webRequest.downloadHandler.text );
            //timeData.datetime value is : 2020-08-14T15:54:04+01:00

            _currentDateTime = ParseDateTime ( timeData.datetime );
            IsTimeLodaed = true;

            Debug.Log ( "Success." );
        }
    }
    //datetime format => 2020-08-14T15:54:04+01:00
    DateTime ParseDateTime ( string datetime ) {
        //match 0000-00-00
        string date = Regex.Match ( datetime, @"^\d{4}-\d{2}-\d{2}" ).Value;

        //match 00:00:00
        string time = Regex.Match ( datetime, @"\d{2}:\d{2}:\d{2}" ).Value;

        return DateTime.Parse ( string.Format ( "{0} {1}", date, time ) );
    }
}


/* API (json)
{
    "abbreviation" : "+01",
    "client_ip"    : "190.107.125.48",
    "datetime"     : "2020-08-14T15:544:04+01:00",
    "dst"          : false,
    "dst_from"     : null,
    "dst_offset"   : 0,
    "dst_until"    : null,
    "raw_offset"   : 3600,
    "timezone"     : "Asia/Brunei",
    "unixtime"     : 1595601262,
    "utc_datetime" : "2020-08-14T15:54:04+00:00",
    "utc_offset"   : "+01:00"
}
We only need "datetime" property.
*/

Now I’m not an expert at how this works, but say I need something that is dependent on the real world time, because I don’t want people changing the time on their device, when I need that time I call GetRealDateTimeFromAPI then I call GetCurrentDateTime, it’s not working. I thought this may be a pretty popular script so I wanted to see if anybody could help.

Gotta debug it like any other problem. You’ve already seen my Debug.Log() blurb many times, if you like I can attach it again. :slight_smile:

Here’s how to debug network stuff:

Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

https://discussions.unity.com/t/831681/2

https://discussions.unity.com/t/837672/2

And setting up a proxy can be very helpful too, in order to compare traffic:

https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity

I know how to debug problems. A lot of people use this world time api so I wanted to see if somebody who knew about it would help me out with it.

You would get a LOT more people help you if you at least found out:

  • is the request even COMPLETING!? (use Debug.Log)

  • is ANY data coming out of it? (print the string!)

  • is the CORRECT data coming back? (eg, close to what you asked for)

  • are you parsing it correctly?

I mean that’s a minimum, like a bare minimum of troubleshooting, and yet you mentioned NOTHING about that, except to say “it doesn’t work.”