Skip to content

Instantly share code, notes, and snippets.

@QuiltMeow
Last active September 1, 2024 02:44
Show Gist options
  • Select an option

  • Save QuiltMeow/5fdaa6c910d1017604b6d7fd638ddc4b to your computer and use it in GitHub Desktop.

Select an option

Save QuiltMeow/5fdaa6c910d1017604b6d7fd638ddc4b to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Json;
using System.Text;
namespace HTTPTimeClient
{
public class WorldTime
{
public string utc_offset
{
get;
set;
}
public string timezone
{
get;
set;
}
public int day_of_week
{
get;
set;
}
public int day_of_year
{
get;
set;
}
public string datetime
{
get;
set;
}
public string utc_datetime
{
get;
set;
}
public long unixtime
{
get;
set;
}
public int raw_offset
{
get;
set;
}
public int week_number
{
get;
set;
}
public bool dst
{
get;
set;
}
public string abbreviation
{
get;
set;
}
public int dst_offset
{
get;
set;
}
public string dst_from
{
get;
set;
}
public string dst_until
{
get;
set;
}
public string client_ip
{
get;
set;
}
}
public static class Program
{
private static readonly bool USE_PROXY = false;
private const string PROXY_HOST = "YOUR_PROXY_HOST";
private const int PROXY_PORT = 443;
private const string PROXY_USER_NAME = "YOUR_PROXY_USER_NAME";
private const string PROXY_PASSWORD = "YOUR_PROXY_PASSWORD";
private const string API_URL = "https://worldtimeapi.org/api/timezone/Asia/Taipei";
public static string getCurrentTimeJSON()
{
using (WebClient client = new WebClient())
{
if (USE_PROXY)
{
client.Proxy = new WebProxy(PROXY_HOST, PROXY_PORT)
{
Credentials = new NetworkCredential(PROXY_USER_NAME, PROXY_PASSWORD)
};
}
return client.DownloadString(API_URL);
}
}
public static T deserializeJSON<T>(string json) where T : class
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
using (Stream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
return serializer.ReadObject(ms) as T;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetSystemTime(ref SYSTEMTIME st);
public static void Main()
{
Console.Title = "系統時間同步工具";
try
{
string json = getCurrentTimeJSON();
WorldTime worldTime = deserializeJSON<WorldTime>(json);
foreach (PropertyInfo property in typeof(WorldTime).GetProperties())
{
object value = property.GetValue(worldTime);
string data = "null";
if (value != null)
{
data = value.ToString();
}
Console.WriteLine($"{property.Name} : {data}");
}
DateTime dateTime;
if (!DateTime.TryParse(worldTime.datetime, out dateTime))
{
throw new InvalidDataException("無效時間資料");
}
Console.WriteLine($"本地時間 : {dateTime}");
DateTime utcDateTime = dateTime.ToUniversalTime();
Console.WriteLine($"UTC 時間 : {utcDateTime}");
SYSTEMTIME st = new SYSTEMTIME()
{
wYear = (short)utcDateTime.Year,
wMonth = (short)utcDateTime.Month,
wDay = (short)utcDateTime.Day,
wHour = (short)utcDateTime.Hour,
wMinute = (short)utcDateTime.Minute,
wSecond = (short)utcDateTime.Second
};
SetSystemTime(ref st);
Console.WriteLine("系統時間同步完成");
}
catch (Exception ex)
{
Console.Error.WriteLine($"同步時間時發生例外狀況 : {ex.Message}");
}
Console.Write("請按任意鍵繼續 ...");
Console.ReadKey(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment