NerdyHearn
Home

IPhone SMS To Gmail

Consulting

Blog
Software
Projects
Links
About
Contact

Blog
Facebook
Twitter
LinkedIn

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

SharePoint Web Services, .NET 3.5, and Authentication Issues
Updating an LDAP Property in C#
Enumerating the values of an enum in C#
ConnectionString Switcharoo
Code Analysis with NDepend V3

Comments

Currently no comments.

Add A Comment

Name:


URL:


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


Comments:


Please enter the text from the image:



NerdyHearn - Latest tech news relating to C#, SharePoint, PHP, general development, and more. LiveNation Feed - Feeds of all your local artists, concerts, and events through your favorite RSS reader SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers Tweets2Mail - Backup and search your twitter stream from Gmail TimeSinceI - Track how long you have quit smoking, how long you have been married, or how long you have done anything.