Getting UTC Time in ISO 8601 Format

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"


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s