c# - Hosting server display Unversal 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 2 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 working fine in local host server when hosting code in windows server show standard time ( mon, 01 jan 1900 00:00 ) , hosting in ntp server , getting time own server time show utc time not system time.
in ntp server show right time ntp servers time pool.ntp.org. when enter name of own ntp server 'tritonadmin.com' shows utc time not ntp server time.
you have below code examples different time zones.. have 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 {0}", datetime.now.tostring("d")); // increments dt 1 day. dt = dt.adddays(1); console.writeline("tomorrow {0}", dt.tostring("d")); } }
or
using system; class program { static void main() { timezone zone = timezone.currenttimezone; // demonstrate tolocaltime , touniversaltime. datetime local = zone.tolocaltime(datetime.now); datetime universal = zone.touniversaltime(datetime.now); console.writeline(local); console.writeline(universal); } }
by above provided code can set particular standard time world wide application. more reference related time zone can check - https://msdn.microsoft.com/en-us/library/5hh873ya(v=vs.71).aspx
Comments
Post a Comment