Avatar

Handling TimeZone

0

Hello,

 

I'm getting information about exception events through the GetFeed Method, everything is ok but when I get fromDate with GetFrom () it is in UTC time how can I convert to my time zone or device's time zone ?.

I'm working in C # and it's my first dealing with geotab SDK, I really appreciate your help with this.

Hans Dubon

Please sign in to leave a comment.

3 comments

0
Avatar

To be safe you should use ActiveFrom and ActiveTo properties instead of GetFrom/GetTo, those methods are not a supported API and might be removed or changed without warning.

Dealing with time zones in .Net is explained in detail here.

Steve Hansen 0 votes
0
Avatar

Thanks a lot, I thing i get it well, the goal is send a string whith the exception event info using POST, this is part of my code, really using ActiveTo instead of GeFrom make easy handle the timezone.  Sorry i don´t know how to paste the code properly but if you have time to check it I'll apreciate any coment about it

 

{
API api = new API("xxxxxxxxxxxxxxx@xxxxx.xxxx", "xxxxxxxx", null, "db", "server");
api.Authenticate();


Id idBoton = Id.Create("a8O9vTF8zP0-_hSkRSJZhYQ");

 

//OBTENEMOS TODAS LAS excepciones
long? token = null;
while (true)
{
FeedResult<ExceptionEvent> excepciones = api.Call<FeedResult<ExceptionEvent>>("GetFeed", typeof(ExceptionEvent), new
{
fromVersion = token,

}



);
token = excepciones.ToVersion;

foreach (ExceptionEvent exception in excepciones.Data)
{
if (exception.Rule.Id == idBoton)
{
//this condition is because I getting events that start two or more weeks ago but still are active
if (exception.GetFrom() > DateTime.UtcNow.AddMinutes(-2))
{
LogRecordSearch logRecordSearch = new LogRecordSearch
{
DeviceSearch = new DeviceSearch(exception.Device.Id),
FromDate = exception.ActiveFrom,
ToDate = exception.ActiveTo,
};


IList<LogRecord> logs = api.Call<IList<LogRecord>>("Get", typeof(LogRecord), new { resultsLimit = 1, search = logRecordSearch });
string fecha = exception.ActiveFrom.Value.AddHours(-6).ToString("dd/MM/yyyy");
string hora = exception.ActiveFrom.Value.AddHours(-6).ToString("HH:mm:ss");
var latitude = logs[0].Latitude;
var longitud = logs[0].Longitude;
var velocidad = logs[0].Speed;

String toDMS = ("dispositivo=" + GetDevice(exception.Device) + "&fecha=" + fecha + "&tiempo=" + hora + "&latitud=" + latitude + "&longitud=" + longitud + "&regla=" + GetRule(exception.Rule) + "&velocidad=" + velocidad + "&direccion=N/A&diagnostico=" + GetDiagnostic(exception.Diagnostic) + ":" + GetRule(exception.Rule).Condition.Value);

//PostDMS(toDMS);
Console.WriteLine(toDMS);
}
}

}
//I saw in devChannel an example and it has this sleep line ¿it is necesary?
Thread.Sleep(1000);
}

Rule GetRule(Rule rule)
{
if (rule == null)
{
return null;
}
Id id = rule.Id;

IList<Rule> returnedRule = api.Call<IList<Rule>>("Get", typeof(Rule), new { search = new RuleSearch(id) });
if (returnedRule.Count == 0)
{
return null;
}
rule = returnedRule[0];

return rule;
}

 

Device GetDevice(Device device)
{
if (device == null || device is NoDevice)
{
return NoDevice.Value;
}
Id id = device.Id;

IList<Device> returnedDevices = api.Call<IList<Device>>("Get", typeof(Device), new { search = new DeviceSearch(id) });
if (returnedDevices.Count == 0)
{
return null;
}
device = returnedDevices[0];

return device;
}

 

Diagnostic GetDiagnostic(Diagnostic diagnostic)
{
if (diagnostic == null || diagnostic is NoDiagnostic)
{
return NoDiagnostic.Value;
}
Id id = diagnostic.Id;

IList<Diagnostic> returnedDiagnostic = api.Call<IList<Diagnostic>>("Get", typeof(Diagnostic), new { search = new DiagnosticSearch(id) });
if (returnedDiagnostic.Count == 0)
{
return null;
}
diagnostic = returnedDiagnostic[0];

return diagnostic;
}

}

 

 

Hans Dubon 0 votes
0
Avatar

Reading the code what you are doing looks OK. .Net has some methods which can do the time conversion based on time zone which is nice as I think it accounts for daylight savings or and future changes to time zone offsets etc. Thread sleep is OK (better if it all this was async of course). Really just want to wait some period of time before next call to GetFeed (the interval). Could of things to consider are:

  1. Caching of diagnostic, rules and devices. These don't change frequently and is a good candidate for temporary caching. There is an example of this in the .Net data feed example.
  2. Also consider using web hook notification template. It's a notification of POST or GET to an endpoint when an exception occurs with tokenized parameters.
Steve Hansen 0 votes