private void ButtonProofreading_Click(object sender, RoutedEventArgs e) { if (RadioButtonUpDateLocalTime.IsChecked == true) { //将本机电脑与网络时间同步 DateTime netUTCtime = NetTime.GetUTCTime(); if (!DateTime.Equals(netUTCtime, new DateTime(1970, 1, 1, 0, 0, 0, 0))) { DateTime localTime = netUTCtime.ToLocalTime(); bool setD = UpdateTime.SetDate(localTime); if (setD == true) { MessageBox.Show("本机时间已经更新为网络时间(国家授时中心获取)"); } else { MessageBox.Show("更新失败,请重试。"); } } return; } using (var client = new SshClient(ProfreadTimeReceiveConnectionInfo)) { client.Connect(); //设置vps为UTC时区 client.RunCommand("rm -f /etc/localtime"); client.RunCommand("ln -s /usr/share/zoneinfo/UTC /etc/localtime"); if (RadioButtonLocalTime.IsChecked == true) { //以本地时间为准,校正远程主机时间 //获取本地时间戳 TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0); long timeStampLocal = Convert.ToInt64(ts.TotalSeconds); string sshCmd = $"date --set=\"$(date \"+%Y-%m-%d %H:%M:%S\" -d @{timeStampLocal.ToString()})\""; client.RunCommand(sshCmd); MessageBox.Show("同步本地时间校时完毕"); } else { //以网络时间为准,校正远程主机时间 TimeSpan utcTS = NetTime.GetUTCTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0); long timeStampVPS = Convert.ToInt64(utcTS.TotalSeconds); if (timeStampVPS != 0) { string sshCmd = $"date --set=\"$(date \"+%Y-%m-%d %H:%M:%S\" -d @{timeStampVPS.ToString()})\""; client.RunCommand(sshCmd); MessageBox.Show("同步网络时间校时完毕"); } } client.Disconnect(); } }
private void ButtonTEST_Click(object sender, RoutedEventArgs e) { //NetTime netTime = new NetTime(); string netDatetime = NetTime.GetUTCTime().ToString(); MessageBox.Show(netDatetime); //NetTime netTime = new NetTime(); //UpdateTime updateTime = new UpdateTime(); //DateTime netDateTime = netTime.GetBeijingTime(); //MessageBox.Show(netDateTime.ToString()); }