banner 728x90

Getting UTC Time in ISO 8601 Format

In Mobile Web Development 47 views
banner 728x90
Getting UTC Time in ISO 8601 Format

Working on integrating with Amazon S3 services today, which uses a RESTful API. So far the hardest part is generating the login credentials. Amazon does this quite a bit different than most services. Instead of just sending an API Key and Token, you use several pieces of information, including the actual REST URL you are calling, to generate a “String to Sign” which is passed as an SHA256 Hash to Amazon. This is a very secure method as authorizing an API, since any changes to the call would invalidate the hash since the call URL is part of the hash.

I am sure I will be sharing more info on my S3 integration in the future, as well as general Amazon AWS API integration. But for now I wanted to share my solution for one of the first hurdles I hit. Part of the “String to Sign” is the current UTC time in ISO 8601 format.  Again this is to insure that if any of the content is changed, the hash will no longer match.

So below is my function, along with a sample call, to return a DateTime in as a UTC time in ISO 8601 format.

The first thing the function does is determine the offset between local time of the machine and UTC. It does this Using the DateTimeLocalToUTC wx function. Taking the UTC time and subtracting the local time gives us a duration variable containing the offset. Now it is just a matter of adding that offset to the DateTime passed in. Using the ..Minute and ..InMinutes properties makes this simple!

Now that the current UTC time is determined, a couple of simple DatetoString and TimetoString functions formats it in the ISO8601 format, thank you wikipedia for the formating info!

FormattedDateTime is string = UTCISO8601(SysDateTime())

PROCEDURE UTCISO8601(LOCAL CurrentTimeStamp is DateTime)

LocalTimeStamp is DateTime = SysDateTime()
UTCTimeStamp is DateTime = DateTimeLocalToUTC(LocalTimeStamp)
UTCOffset is Duration = UTCTimeStamp -LocalTimeStamp

CurrentTimeStamp..Minute += UTCOffset..InMinutes
TimeStamp is string = DateToString(CurrentTimeStamp..Date,"YYYYMMDD") + "T" + TimeToString(CurrentTimeStamp..Time,"HHMMSS") + "Z"

RESULT(TimeStamp)

banner 728x90
author
Author: 
    Script & Style Show: Episode 1: Marc Grabanski
    Script & Style Show: Episode 1: Marc Grabanski
    During this second episode of the Script
    AWS AppSync region expansion and new functions for API keys
    AWS AppSync region expansion and new functions for API keys
    AWS AppSync, a managed GraphQL service with
    What is the best time to involve your mobile app users?
    What is the best time to involve your mobile app users?
    When talking to customers about mobile analysis,
    How to make a Twitter bot with Node.js
    How to make a Twitter bot with Node.js
    Twitter bots have been in the news

    Leave a reply "Getting UTC Time in ISO 8601 Format"

    Must read×

    Top