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
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

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


Calling .NET WebService From PHP
Creating an Organizational Chart using C#: Part 1
Fixing "Error in loading DLL" in SharePoint
Validate a Windows Username and Password against Active Directory

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. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations