gmaps-api-net
gmaps-api-net copied to clipboard
TimeZoneRequest ToUri() converts Timestamp TotalSeconds with comma
Subj. It's wrong, Google returns 400 http code when we send a double value as a timestamp. So I added "F0" to ToString() and now it works.
Hey, can you elaborate a little more? Ie, some sample code, and then what ToUri is returning?
Better yet, if you could make a PR that adds a Test to the testing area, I can work on debugging it.
Here is the code, that I used to get the timezone of some city.
var request = new GeocodingRequest
{
Address = city
};
var response = new GeocodingService().GetResponse(request);
if (response.Status == ServiceResponseStatus.Ok)
{
var timeRequest = new TimeZoneRequest
{
Location = response.Results[0].Geometry.Location,
Timestamp = DateTime.Now
};
var timeResponse=new TimeZoneService().GetResponse(timeRequest);
if (timeResponse.Status == ServiceResponseStatus.Ok)
{
return (int)(timeResponse.RawOffSet / 3600);
}
}
It failed on the line with TimeZoneService().GetResponse. I looked at what was happening and saw that TimeZoneRequest.ToUri() converted TotalSeconds ToString with a comma inside the string. So, I had to change ToUri() method like this:
public override Uri ToUri()
{
if (Location == null) throw new InvalidOperationException("Location property is not set.");
var qsb = new QueryStringBuilder();
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
qsb.Append("location", Location.GetAsUrlParameter())
.Append("timestamp", (Timestamp.ToUniversalTime() - epoch).TotalSeconds.ToString("F0"))
.Append("language", Language);
var url = "json?" + qsb;
return new Uri(url, UriKind.Relative);
}
Now, everything works as expected.