Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time Checker
#1
Is it possible to check current time with google? Then return a message with the time is different?
#2
This script will check time at http://nist.time.gov
It calls a function "NistTime" that use C# to webscrap the time.
Note:
On my machine the difference usually about 4-5 seconds and that's no biggie! :-)

Macro CheckTimeDifference
Code:
Copy      Help
out
str endTime=NistTime
;out F"NIST Time = {endTime}"
DateTime x
x.FromComputerTime
;out F"Local Time = {x.ToStr(4)}"
str startTime=x.ToStr(4)
sub.TimeDiff startTime endTime

#sub TimeDiff
function $startTime $endTime

DateTime vStart vEnd
vStart.FromStr(startTime)
vEnd.FromStr(endTime)
long vDiff=vEnd-vStart
mes F"Time difference = {TimeSpanToStr(vDiff)}"

Function NistTime
Code:
Copy      Help
;Credit goes to Nemo from Stack Overflow:
;http://stackoverflow.com/questions/6435099/how-to-get-datetime-from-the-internet/6435207#6435207

function~
CsScript x.AddCode("")
ret x.Call("Test.iNetTime")

#ret
using System;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Text.RegularExpressions;

public class Test
{
,public static string iNetTime()
,{
,,DateTime dateTime = DateTime.MinValue;

,,HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi?lzbc=siqm9b");
,,request.Method = "GET";
,,request.Accept = "text/html, application/xhtml+xml, */*";
,,request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
,,request.ContentType = "application/x-www-form-urlencoded";
,,request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No caching
,,HttpWebResponse response = (HttpWebResponse)request.GetResponse();
,,if (response.StatusCode == HttpStatusCode.OK)
,,{
,,,StreamReader stream = new StreamReader(response.GetResponseStream());
,,,string html = stream.ReadToEnd();
,,,string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
,,,double milliseconds = Convert.ToInt64(time) / 1000.0;
,,,dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();
,,}
,,return dateTime.ToString();
,}    
}
#3
Wow, what a nice code! Thanks for sharing.
#4
Omg. Thanks. This is exactly what I'm looking for.


Forum Jump:


Users browsing this thread: 1 Guest(s)