Created
February 4, 2018 22:23
Star
You must be signed in to star a gist
Converting UTC to Local Time via Azure Functions and Logic Apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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