Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created February 4, 2018 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/39e18fac1ecedf2cf776e85d730e6633 to your computer and use it in GitHub Desktop.
Save justinyoo/39e18fac1ecedf2cf776e85d730e6633 to your computer and use it in GitHub Desktop.
Converting UTC to Local Time via Azure Functions and Logic Apps
[FunctionName("TimeZoneConverterHttpTrigger")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "convert/timezone")]HttpRequest req, TraceWriter log)
{
// Suppress Date/Time string to be converted to DateTime object.
var settings = new JsonSerializerSettings() { DateParseHandling = DateParseHandling.None };
dynamic body = JsonConvert.DeserializeObject(await req.ReadAsStringAsync().ConfigureAwait(false), settings);
var input = (string)body?.input;
var utc = DateTimeOffset.TryParse(input, null, DateTimeStyles.AssumeUniversal, out DateTimeOffset result)
? result
: DateTimeOffset.MinValue;
if (utc == DateTimeOffset.MinValue)
{
var error = new { messagae = "Invalid Date/Time" };
return new BadRequestObjectResult(error);
}
var aest = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
var output = TimeZoneInfo.ConvertTime(utc, aest);
var response = new { input = input, output = output };
return new OkObjectResult(response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment