Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net.Http;
- using UnityEngine;
- /// Go to https://github.com/JamesNK/Newtonsoft.Json/releases and add the
- /// appropriate Newtonsoft.Json.dll to your Assets/Plugins folder.
- using Newtonsoft.Json;
- public class WorldClockTime
- {
- [JsonProperty ( "currentDateTime" )]
- public DateTime CurrentDateTime { get; set; }
- [JsonProperty ( "currentFileTime" )]
- public long CurrentFileTime { get; set; }
- }
- public class WorldTime : MonoBehaviour
- {
- static HttpClient client = new HttpClient ( );
- // Start is called before the first frame update
- void Start()
- {
- GetTime ( );
- }
- private async void GetTime()
- {
- try
- {
- var response = client.GetAsync ( "http://worldclockapi.com/api/json/utc/now" );
- var result = await response;
- var responseString = await result.Content.ReadAsStringAsync ( );
- //Log ( $"PostAsync<T> result.StatusCode : {result.StatusCode} {responseBody}" );
- if ( !string.IsNullOrEmpty ( responseString ) )
- {
- var currentTime = JsonConvert.DeserializeObject<WorldClockTime> ( responseString );
- var date = new DateTime ( currentTime.CurrentFileTime );
- Debug.Log ( $"Current UTC time is {currentTime.CurrentDateTime}.\n" +
- $"Current Local Time is {currentTime.CurrentDateTime.ToLocalTime ( )}\n" +
- $"Current UTC CurrentFileTime is {date}.\n" +
- $"Current Local CurrentFileTime is {date.ToLocalTime ( )}" );
- }
- }
- catch ( Exception e )
- {
- Debug.LogWarning ( $"Exception! {e.Message}" );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment