Files
bnhtrade/src/bnhtrade.Core/Data/AmazonMWS/CurrentDateTime.cs

47 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
namespace bnhtrade.Core.Data.AmazonMWS
{
public class CurrentDateTime
{
public DateTime GetUtc()
{
var xmlDoc = new XmlDocument();
xmlDoc.Load("https://mws.amazonservices.com/");
DateTime returnTime = default(DateTime);
XmlNodeList elemList = xmlDoc.GetElementsByTagName("Timestamp");
for (int i = 0; i < elemList.Count; i++)
{
string attrVal = elemList[i].Attributes["timestamp"].Value;
if (!string.IsNullOrWhiteSpace(attrVal))
{
returnTime = DateTime.SpecifyKind(DateTime.Parse(attrVal), DateTimeKind.Utc);
break;
}
}
if (returnTime == default(DateTime))
{
throw new Exception("Error requesting time from Amazon");
}
returnTime = returnTime.AddTicks(-(returnTime.Ticks % TimeSpan.TicksPerSecond));
return returnTime;
}
public DateTime GetLocal()
{
return GetUtc().ToLocalTime();
}
}
}