worldclock.c
#include <stdio.h>
#include "SDO.h"
#include "SCA.h"
static int utc_timezone_offset(char *city);
static int calculate_time_diff(int hour, char *city, char *otherCity);
/**
* Implementation of worldclock service.
*/
DATAOBJECT getLocalTimeForCity(DATAOBJECT request) {
SDOFACTORY df = getDataFactory();
char *requestCity = getCStringByName(request, "city");
int requestTime = getIntByName(request, "local-time");
char *otherCity = getCStringByName(request, "city-to-request-time-for");
int time_diff = calculate_time_diff(requestTime, requestCity, otherCity);
// error handling: create a SOAP fault
if (time_diff < -24 || time_diff > 24) {
int compcode, reason;
DATAOBJECT faultResponse = doAlloc(df,
"http://www.w3.org/2001/XMLSchema",
"anyType");
setCStringByName(faultResponse,
"faultstring",
"unrecognized city name");
setCStringByName(faultResponse,
"faultcode",
"soap:Client");
SCASetFaultMessage("worldclock",
"getLocalTimeForCity",
"worldclock-fault",
0,
faultResponse,
&compcode,
&reason);
return 0;
}
SDOXML xmlhlp = getXMLHelper(df);
SDOXMLDOC doc = XMLDocAlloc(xmlhlp, 0, 0, "response");
DATAOBJECT response = getXMLRootDataObject(doc);
setCStringByName(response, "city", otherCity);
setIntByName(response, "hour", time_diff);
return response;
}
static int calculate_time_diff(int hour, char *city, char *otherCity) {
return hour + utc_timezone_offset(city) - utc_timezone_offset(otherCity);
}
static int utc_timezone_offset(char *city) {
if (!strcmp("Bangalore", city)) return +5;
if (!strcmp("Berlin", city)) return +1;
if (!strcmp("San Francisco", city)) return -8;
// ...
// return 100 to indicate unknown argument city
return 100;
}