인터넷 시간 추가
This commit is contained in:
@@ -37,6 +37,7 @@ namespace upper_limit_crawler
|
|||||||
SetDoubleBuffered(tbLog);
|
SetDoubleBuffered(tbLog);
|
||||||
|
|
||||||
UlUtil.Init(tbLog);
|
UlUtil.Init(tbLog);
|
||||||
|
//UlUtil.GetServerTime();
|
||||||
|
|
||||||
btApply_Click(null, null);
|
btApply_Click(null, null);
|
||||||
|
|
||||||
@@ -67,7 +68,7 @@ namespace upper_limit_crawler
|
|||||||
|
|
||||||
private bool IsOnTime()
|
private bool IsOnTime()
|
||||||
{
|
{
|
||||||
DateTime CurTime = DateTime.Now;
|
DateTime CurTime = UlUtil.GetCurTime();
|
||||||
if (CurTime.DayOfWeek == DayOfWeek.Sunday || CurTime.DayOfWeek == DayOfWeek.Saturday)
|
if (CurTime.DayOfWeek == DayOfWeek.Sunday || CurTime.DayOfWeek == DayOfWeek.Saturday)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|||||||
110
UlUtil.cs
110
UlUtil.cs
@@ -17,22 +17,128 @@ namespace upper_limit_crawler
|
|||||||
static CPUTILLib.CpCybos m_Util = new CPUTILLib.CpCybos();
|
static CPUTILLib.CpCybos m_Util = new CPUTILLib.CpCybos();
|
||||||
static TextBox m_tbLog = null;
|
static TextBox m_tbLog = null;
|
||||||
static string m_strLogFileName;
|
static string m_strLogFileName;
|
||||||
|
static TimeSpan m_TimeDiff = TimeSpan.Zero;
|
||||||
|
|
||||||
public static void Init(TextBox tbLog)
|
public static void Init(TextBox tbLog)
|
||||||
{
|
{
|
||||||
m_tbLog = tbLog;
|
m_tbLog = tbLog;
|
||||||
|
|
||||||
|
DateTime time = GetFastestNISTDate();
|
||||||
|
m_TimeDiff = time - DateTime.Now;
|
||||||
|
|
||||||
if (Debugger.IsAttached == true)
|
if (Debugger.IsAttached == true)
|
||||||
m_strLogFileName = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName+"\\";
|
m_strLogFileName = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName+"\\";
|
||||||
m_strLogFileName += "log\\";
|
m_strLogFileName += "log\\";
|
||||||
if (Directory.Exists(m_strLogFileName) == false)
|
if (Directory.Exists(m_strLogFileName) == false)
|
||||||
Directory.CreateDirectory(m_strLogFileName);
|
Directory.CreateDirectory(m_strLogFileName);
|
||||||
m_strLogFileName += "log-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
m_strLogFileName += "log-" + GetCurTime().ToString("yyyy-MM-dd") + ".txt";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DateTime GetCurTime()
|
||||||
|
{
|
||||||
|
return DateTime.Now + m_TimeDiff;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void GetServerTime()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string strURL = string.Format("http://www.timeapi.org/utc/now?format={0}", (@"\Y-\m-\d \H:\M:\S"));
|
||||||
|
Uri uri = new Uri(strURL);
|
||||||
|
WebRequest wReq = (HttpWebRequest)WebRequest.Create(uri); // WebRequest 객체 형성 및 HttpWebRequest 로 형변환
|
||||||
|
wReq.Method = "GET"; // 전송 방법 "GET" or "POST"
|
||||||
|
|
||||||
|
using (HttpWebResponse wRes = (HttpWebResponse)wReq.GetResponse())
|
||||||
|
{
|
||||||
|
Stream respPostStream = wRes.GetResponseStream();
|
||||||
|
StreamReader readerPost = new StreamReader(respPostStream, Encoding.GetEncoding("EUC-KR"), true);
|
||||||
|
|
||||||
|
string resResult = readerPost.ReadToEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (WebException ex)
|
||||||
|
{
|
||||||
|
if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
|
||||||
|
{
|
||||||
|
var resp = (HttpWebResponse)ex.Response;
|
||||||
|
Trace(string.Format("인터넷 시간 동기화 실패 ({0})", resp.StatusCode));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DateTime GetFastestNISTDate()
|
||||||
|
{
|
||||||
|
var result = DateTime.MinValue;
|
||||||
|
// Initialize the list of NIST time servers
|
||||||
|
// http://tf.nist.gov/tf-cgi/servers.cgi
|
||||||
|
string[] servers = new string[] {
|
||||||
|
"time-c.nist.gov",
|
||||||
|
"nist1-macon.macon.ga.us",
|
||||||
|
"time.nist.gov",
|
||||||
|
"time-nw.nist.gov",
|
||||||
|
"nist-time-server.eoni.com"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Try 5 servers in random order to spread the load
|
||||||
|
Random rnd = new Random();
|
||||||
|
foreach (string server in servers.OrderBy(s => rnd.NextDouble()).Take(5))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Connect to the server (at port 13) and get the response
|
||||||
|
string serverResponse = string.Empty;
|
||||||
|
using (var reader = new StreamReader(new System.Net.Sockets.TcpClient(server, 13).GetStream()))
|
||||||
|
{
|
||||||
|
serverResponse = reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a response was received
|
||||||
|
if (!string.IsNullOrEmpty(serverResponse))
|
||||||
|
{
|
||||||
|
// Split the response string ("55596 11-02-14 13:54:11 00 0 0 478.1 UTC(NIST) *")
|
||||||
|
string[] tokens = serverResponse.Split(' ');
|
||||||
|
|
||||||
|
// Check the number of tokens
|
||||||
|
if (tokens.Length >= 6)
|
||||||
|
{
|
||||||
|
// Check the health status
|
||||||
|
string health = tokens[5];
|
||||||
|
if (health == "0")
|
||||||
|
{
|
||||||
|
// Get date and time parts from the server response
|
||||||
|
string[] dateParts = tokens[1].Split('-');
|
||||||
|
string[] timeParts = tokens[2].Split(':');
|
||||||
|
|
||||||
|
// Create a DateTime instance
|
||||||
|
DateTime utcDateTime = new DateTime(
|
||||||
|
Convert.ToInt32(dateParts[0]) + 2000,
|
||||||
|
Convert.ToInt32(dateParts[1]), Convert.ToInt32(dateParts[2]),
|
||||||
|
Convert.ToInt32(timeParts[0]), Convert.ToInt32(timeParts[1]),
|
||||||
|
Convert.ToInt32(timeParts[2]));
|
||||||
|
|
||||||
|
// Convert received (UTC) DateTime value to the local timezone
|
||||||
|
result = utcDateTime.ToLocalTime();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
// Response successfully received; exit the loop
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Ignore exception and try the next server
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetCurTimeString()
|
public static string GetCurTimeString()
|
||||||
{
|
{
|
||||||
return DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
|
return GetCurTime().ToString("yyyy-MM-dd hh:mm:ss");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WebLog(string strURL, string strMsg)
|
public static void WebLog(string strURL, string strMsg)
|
||||||
|
|||||||
Reference in New Issue
Block a user