NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
How Long For Me
SaveMySerials

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Calculating ISO 8601 Date formats in C#, C++, and Java

Thursday, December 11th, 2008

I have done a fair amount of work authenticating against the Amazon web services APIs, not to mention the APIs of other online services, and one thing that is often a difficult and tedious task to calculate is the ISO 8601 Date format that Amazon (and many other) REST APIs require in order to properly authenticate. I thought that I would share my methods of doing so in multiple different programming languages to allow many of you in the future to easily copy this code and use it in your own APIs.

Without further ado, I present my solutions:

Calculating ISO 8601 in C#:
return System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ", System.Globalization.CultureInfo.InvariantCulture);

Calculating ISO 8601 in Java:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
return df.format(new Date());

Calculating ISO 8601 in Windows C++:
SYSTEMTIME tm;
GetSystemTime(&tm);
char buf[500];
sprintf(buf, "%i-%02i-%02iT%02i:%02i:%02i.%03iZ", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond, tm.wMilliseconds);
return buf;

Calculating ISO 8601 in UNIX C++:
struct tm tm;
char tmp[120];
time_t t;
time(&t);
localtime_r(&t,&tm);
sprintf(tmp, "%i-%02i-%02iT%02i:%02i:%02i", tm.tm_year + 1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
return tmp;

I hope this helps some of you out along the way!

Timestampin' Tom Out.

Tags

CSharp CPP Java

Related Blogs

ASP.NET Best Practices
Retrieving the SID of a user or group account using the Win32 SDK and C#
Creating a reusable web client for PUT, POST, and DELETE in C#
Retrieving data from SharePoint SOAP Requests using LINQ
Comparing XML files in C# Using Microsoft's Diff and Patch Tool

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers