Gaurav Pal
Gaurav Pal

Reputation: 77

Hosting server display Universal time

public DateTime GetNetworkTime(string ntpServer) {

        //const string ntpServer = "tritonadmin.com";
        const int timeout = 2000;

        var ntpData = new byte[48];
        ntpData[0] = 0x1B; //LeapIndicator = 0 (no warning), VersionNum = 3 (IPv4 only), Mode = 3 (Client Mode)       
        try
        {
            var addresses = Dns.GetHostEntry(ntpServer).AddressList;
            var ipEndPoint = new IPEndPoint(addresses[0], 123);
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            // wait two seconds before timing out
            socket.ReceiveTimeout = timeout;

            socket.Connect(ipEndPoint);
            socket.Send(ntpData);
            var receivedBytes = 0;

            receivedBytes = socket.Receive(ntpData);
            ulong intPart = (ulong)ntpData[40] << 24 | (ulong)ntpData[41] << 16 | (ulong)ntpData[42] << 8 | (ulong)ntpData[43];
            ulong fractPart = (ulong)ntpData[44] << 24 | (ulong)ntpData[45] << 16 | (ulong)ntpData[46] << 8 | (ulong)ntpData[47];

            var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);

            var networkDateTime = (new DateTime(1900, 1, 1)).AddMilliseconds((long)milliseconds) ;
            socket.Close();


            return networkDateTime;

        }
        catch (SocketException ex)
        {
            DateTime s = new DateTime(1900, 1, 1);
            return s;
        }


    }
Above code is working fine in local host server but when i am hosting the code in windows server then it will show standard time ( Mon, 01 Jan 1900 00:00 ) and also hosting in NTP server and getting the time from own server time it will show utc time not for that system time. 

In NTP server it will show right time for some ntp servers time like pool.ntp.org. But when i enter the name of my own ntp server 'tritonadmin.com' it shows UTC time not my ntp server time.

Upvotes: 1

Views: 75

Answers (1)

Shirish
Shirish

Reputation: 1250

You have to do like below code examples for different time zones.. you have to use globalization

using System;
using System.Globalization;
using System.Threading;
public class TestClass
{
   public static void Main()
   {
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

      DateTime dt = DateTime.Now;
      Console.WriteLine("Today is {0}", DateTime.Now.ToString("d"));

      // Increments dt by one day.
      dt = dt.AddDays(1);
      Console.WriteLine("Tomorrow is {0}", dt.ToString("d"));

   }
}

Or

using System;

class Program
{
    static void Main()
    {
    TimeZone zone =  TimeZone.CurrentTimeZone;
    // Demonstrate ToLocalTime and ToUniversalTime.
    DateTime local = zone.ToLocalTime(DateTime.Now);
    DateTime universal = zone.ToUniversalTime(DateTime.Now);
    Console.WriteLine(local);
    Console.WriteLine(universal);
    }
}

by above provided code you can set particular standard time for your world wide application. for more reference related to Time Zone you can check - https://msdn.microsoft.com/en-us/library/5hh873ya(v=vs.71).aspx

Upvotes: 1

Related Questions