Fórum Hora certa via internet em CSharp #460036
01/11/2013
0
Pessoal, como faço para obter a hora certa via internet para um programa em C# ?
(detalhe, estou no Rio de Janeiro Brasil) Caso precise pegar de algum servidor de fora, ainda posso recorrer a conversão de fuso horário.
Desde já agradeço
Ricardo
[]
Ricardo Igreja
Curtir tópico
+ 0Posts
01/11/2013
Ricardo Igreja
encontrei um que pega hora de um servidor americano.
Fiz uma compensacao de fuso retornando:
return dateTime.AddHours(-2).AddSeconds(1);
Código Original:
public static DateTime GetNistTime()
{
DateTime dateTime = DateTime.MinValue;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/timezone.cgi?UTC/s/0");
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
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().ToUpper();
string time = Regex.Match(html, @">\d+:\d+:\d+<").Value; //HH:mm:ss format
string date = Regex.Match(html, @">\w+,\s\w+\s\d+,\s\d+<").Value; //dddd, MMMM dd, yyyy
dateTime = DateTime.Parse((date + " " + time).Replace(">", "").Replace("<", ""));
}
return dateTime;
}Classe criada:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Text.RegularExpressions;
namespace EletronicWorkFlow
{
public static class DtOnline
{
public static DateTime horaCerta = GetNetworkTime();
public static void AtualizaSegundo()
{
horaCerta = horaCerta.AddSeconds(1);
}
public static DateTime getDateTime()
{
return horaCerta;
}
public static DateTime GetNetworkTime()
{
DateTime dateTime = DateTime.MinValue;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/timezone.cgi?UTC/s/0");
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
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().ToUpper();
string time = Regex.Match(html, @">\d+:\d+:\d+<").Value; //HH:mm:ss format
string date = Regex.Match(html, @">\w+,\s\w+\s\d+,\s\d+<").Value; //dddd, MMMM dd, yyyy
dateTime = DateTime.Parse((date + " " + time).Replace(">", "").Replace("<", ""));
}
return dateTime.AddHours(-2).AddSeconds(1);
}
}
}
Gostei + 0
12/11/2013
[desativado] Gonçalves
http://pcdsh01.on.br/SincronismoPublico.html
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)