Notifications
Clear all

[Closed] How To Get Time From The Internet With Maxscript

Hello my name is Nam , i come from Viet Nam , i am a newbie in maxscript
I create some scripts and want create a limitation , i seach internet and found a solution write by C#
https://stackoverflow.com/questions/6435099/how-to-get-datetime-from-the-internet
Can i use it in maxscript ? please help me , thank you very much *

5 Replies
 lo1

If you can use port 13 then this should work:


(
   local client = dotnetObject "System.Net.Sockets.TcpClient" "time.nist.gov" 13
   local streamReader = dotnetObject "System.IO.StreamReader" (client.GetStream())
   local response = streamReader.ReadToEnd asDotnetObject:on
   streamReader.Dispose()
   local utcDateTimeString = response.Substring 7 17 asDotNetObject:on
   local localDateTime = (dotnetClass "System.DateTime").ParseExact \
      utcDateTimeString \
      "yy-MM-dd HH:mm:ss" \
      (dotnetClass "System.Globalization.CultureInfo").InvariantCulture \
      (dotnetClass "System.Globalization.DateTimeStyles").AssumeUniversal
      
   print (localDateTime.ToString())
)

If not, translate that other answer to maxscript based on the same logic.

Thank for your quiick reply , but i can not use port 13 , is there another way ? i am a newbie in maxscript , can you help me again ? thank you very much*

i try but it not work

–the C# code
source = ” “
source += “DateTime dateTime = DateTime.MinValue;

source += “HttpWebRequest request = (HttpWebRequest)WebRequest.Create(‘ http://nist.time.gov/actualtime.cgi?lzbc=siqm9b ’);

source += “request.Method = ‘GET’;

source += “request.Accept = ‘text/html, application/xhtml+xml, /’;

source += “request.ContentType = ‘application/x-www-form-urlencoded’;

source += “request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);

source += “HttpWebResponse response = (HttpWebResponse)request.GetResponse();

source += “if (response.StatusCode == HttpStatusCode.OK)

source += “{

source += ” StreamReader stream = new StreamReader(response.GetResponseStream());

source += ” string html = stream.ReadToEnd();//<timestamp time=‘1395772696469995’ delay=‘1395772696469995’/>

source += ” string time = Regex.Match(html, @’(?<=\btime=’’)[^’’]*’).Value;

source += ” double milliseconds = Convert.ToInt64(time) / 1000.0;

source += ” dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();

source += “}

source += ” return dateTime;”

–setting up the assembly in memory
csharpProvider = dotnetobject “Microsoft.CSharp.CSharpCodeProvider”
compilerParams = dotnetobject “System.CodeDom.Compiler.CompilerParameters”
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)

 lo1

You can compile c# code outside of a method and class. There are plenty examples on this forum.