C#代码帮助从网站获取时间和日期 [英] C# code help for getting time and date from a site

查看:78
本文介绍了C#代码帮助从网站获取时间和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我真的需要你的帮助.

我已经编写了一个C#程序,我想将其连接到Internet并从诸如time-a.nist.gov之类的站点获取当前时间和日期,因为我不需要本地系统的时间和日期. /> 当我使用此程序时,我不知道在控制台的主要方法中以及在使用此代码时应该写什么内容.

出现以下错误:

Hi everyone,

I really need your help.

I have writen a C# program, and I would like to connect it to the internet and get current time and date from a site like time-a.nist.gov, because I don''t need local system time and date.
When I use this program, I don''t know what should writen in main method of my console and when I use this code.

The following error appeared:

Error     The type or namespace name 'SPOT' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)


你能帮我解决吗?

请帮助我,并给我您的建议.

我的程序在这里:


Can you please help me to solve it?

Please help me and give me your suggestion.

My program is here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
 
namespace MFToolkit.Net.Ntp
{
 
      class Program
      {
 
            static void Main(string[] args)
            {
 

                  Microsoft.SPOT.ExtendedTimeZone.SetTimeZone(TimeZoneId.Berlin);
                  Microsoft.SPOT.Hardware.Utility.SetLocalTime(GetNetworkTime());
            }
 
      }
 
      /// <summary>
      /// Static class to receive the time from a NTP server.
      /// </summary>
      public class NtpClient
      {
            /// <summary>
            /// Gets the current DateTime from time-a.nist.gov.
            /// </summary>
            /// <returns>A DateTime containing the current time.</returns>
            public static DateTime GetNetworkTime()
            {
                  // return GetNetworkTime("time-a.nist.gov");
                  return GetNetworkTime(" time-nw.nist.gov");
                  //nist1-ny.ustiming.org 
                  return GetNetworkTime(" 64.90.182.55 ");
            }
 
            /// <summary>
            /// Gets the current DateTime from <paramref name="ntpServer" />.
            /// </summary>
            /// <param name="ntpServer">The hostname of the NTP server.</param>
            /// <returns>A DateTime containing the current time.</returns>
            public static DateTime GetNetworkTime(string ntpServer)
            {
                  IPAddress[] address = Dns.GetHostEntry(ntpServer).AddressList;
 
                  if (address == null || address.Length == 0)
                        throw new ArgumentException("Could not resolve ip address from '" + ntpServer + "'.", "ntpServer");
 
                  IPEndPoint ep = new IPEndPoint(address[0], 123);
 
                  return GetNetworkTime(ep);
            }
 
            /// <summary>
            /// Gets the current DateTime form <paramref name="ep" /> IPEndPoint.
            /// </summary>
            /// <param name="ep">The IPEndPoint to connect to.</param>
            /// <returns>A DateTime containing the current time.</returns>
            public static DateTime GetNetworkTime(IPEndPoint ep)
            {
                  Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 
                  s.Connect(ep);
 
                  byte[] ntpData = new byte[48]; // RFC 2030
                  ntpData[0] = 0x1B;
                  for (int i = 1; i < 48; i++)
                        ntpData[i] = 0;
 
                  s.Send(ntpData);
                  s.Receive(ntpData);
 
                  byte offsetTransmitTime = 40;
                  ulong intpart = 0;
                  ulong fractpart = 0;
 
                  for (int i = 0; i <= 3; i++)
                        intpart = 256 * intpart + ntpData[offsetTransmitTime + i];
 
                  for (int i = 4; i <= 7; i++)
                        fractpart = 256 * fractpart + ntpData[offsetTransmitTime + i];
 
                  ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);
                  s.Close();
 
                  TimeSpan timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
 
                  DateTime dateTime = new DateTime(1900, 1, 1);
                  dateTime += timeSpan;
 
                  TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
                  DateTime networkDateTime = (dateTime + offsetAmount);
 
                  return networkDateTime;
 
                  //System.Threading.Thread.Sleep(1000);
 
            }
      }
}

推荐答案

检查这些内容以获取上网时间:

http://dotnet-snippets.com/dns/simple-network- time-ntp-protocol-client-SID571.aspx [ ^ ]

白天,Internet时间服务类 [
Check these for getting time from the internet :

http://dotnet-snippets.com/dns/simple-network-time-ntp-protocol-client-SID571.aspx[^]

Daytime, Internet Time Service Class[^]


您错过了引用,您必须将其添加到项目中

为什么收到错误消息:类型或名称空间"<命名空间名称=">''在类或名称空间``< parent名称空间=">''中不存在(您是否缺少程序集引用?)" [ Microsoft.SPOT命名空间 [ http://www.christec.co.nz/blog/archives/category/net-微型框架 [ ^ ]

谢谢Mehdi
You missed the reference, you must add that in your project

Why did I receive the error: "The type or namespace ''<namespace name="">'' does not exist in the class or namespace ''<parent namespace="">'' (are you missing an assembly reference?)"[^]

EDIT
--------------
Take a look at this
Microsoft.SPOT Namespace[^]
http://www.christec.co.nz/blog/archives/category/net-micro-framework[^]

Thanks Mehdi


这篇关于C#代码帮助从网站获取时间和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆