<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mission Data Blog &#187; C/C++</title>
	<atom:link href="http://www.missiondata.com/blog/tag/cc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.missiondata.com/blog</link>
	<description>Louisville-based Web Development &#38; Software Engineering</description>
	<lastBuildDate>Tue, 24 Jan 2012 14:58:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Using strptime to parse ISO 8601 formated timestamps</title>
		<link>http://www.missiondata.com/blog/software-development/42/using-strptime-to-parse-iso-8601-formated-timestamps/</link>
		<comments>http://www.missiondata.com/blog/software-development/42/using-strptime-to-parse-iso-8601-formated-timestamps/#comments</comments>
		<pubDate>Sat, 15 Apr 2006 11:23:59 +0000</pubDate>
		<dc:creator>carsonm</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://blogs.missiondata.com/?p=42</guid>
		<description><![CDATA[A lot of dates that come back from XML based web services are in the ISO 8601 form. I found out recently that it isn&#8217;t straight forward to parse such a date using C functions and have the time come out in the correct timezone. It isn&#8217;t rocket science but it is a lot more [...]]]></description>
			<content:encoded><![CDATA[<p>A lot of dates that come back from XML based web services are in the ISO 8601 form. I found out recently that it isn&#8217;t straight forward to parse such a date using C functions and have the time come out in the correct timezone. It isn&#8217;t rocket science but it is a lot more convoluted than higher level languages like Java.</p>
<p>First of lets see an example of the ISO 8601 format: </p>
<p>2006-02-03T16:45:09.000Z</p>
<p>That breaks down into: </p>
<p>&lt;date&gt;T&lt;time&gt;&lt;timezone&gt;</p>
<p>Where a timezone of Z is equal to UTC. I was only interested in parsing timestamps in UTC so the following only applies to that timezone.</p>
<p>If you want to know any more about the format check out <a href="http://en.wikipedia.org/wiki/Iso8601">this page</a>.</p>
<p>The strptime function is a flexible way to turn a string into a <b>struct tm</b> given a specified format. It is like a sscanf for dates. For more information on it <a href="http://www.opengroup.org/onlinepubs/007908799/xsh/strptime.html">check here</a>. I&#8217;m not exactly sure of the portability of this function but it seems to be fairly old now so it is probably reasonably portable.</p>
<p>The format used to parse the a ISO 8601 timestamp is: %FT%T%z</p>
<p><span id="more-42"></span></p>
<p>That will give you the date in <b>struct tm</b> form. If you were to print the contents of this you would get the timestamp in UTC. For my application I needed to convert it into the local timezone for the machine it was running on. To do that I used the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/tzset.html">tzset</a> function to populate the timezone. I then use the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/mktime.html">mktime</a> function to turn the <b>struct tm</b> into a time_t (just a long that represents the number of seconds since the epoch). Now I use the timezone value to remove the correct number of seconds from the time_t to get my local time. Then to wrap it all back up I use the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/localtime.html">localtime_r</a> function to put it back into <b>struct tm</b> form.</p>
<p>Here is an example:</p>
<pre>
<code>#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;time.h&gt;

void convert_iso8601(const char *time_string, int ts_len, struct tm *tm_data)
{
  tzset();

  char temp[64];
  memset(temp, 0, sizeof(temp));
  strncpy(temp, time_string, ts_len);

  struct tm ctime;
  memset(&amp;ctime, 0, sizeof(struct tm));
  strptime(temp, "%FT%T%z", &amp;ctime);

  long ts = mktime(&amp;ctime) - timezone;
  localtime_r(&amp;ts, tm_data);
}

int main()
{
  char date[] = "2006-03-28T16:49:29.000Z";
  struct tm tm;
  memset(&amp;tm, 0, sizeof(struct tm));
  convert_iso8601(date, sizeof(date), &amp;tm);

  char buf[128];
  strftime(buf, sizeof(buf), "Date: %a, %d %b %Y %H:%M:%S %Z", &amp;tm);
  printf("%s\n", buf);
}</code>
</pre>
<p>This example compiles with: cc -o timetest timetest.c</p>
<p>Although I haven&#8217;t tested the code with other timezone encoded ISO 8601 timestamps it seems reasonable that if you have one in that type of format that you could add an extra step and convert it to UTC then into your local timezone without much hassle.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.missiondata.com/blog/software-development/42/using-strptime-to-parse-iso-8601-formated-timestamps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Howto base64 encode with C/C++ and OpenSSL</title>
		<link>http://www.missiondata.com/blog/software-development/35/howto-base64-encode-with-cc-and-openssl/</link>
		<comments>http://www.missiondata.com/blog/software-development/35/howto-base64-encode-with-cc-and-openssl/#comments</comments>
		<pubDate>Tue, 11 Apr 2006 16:26:32 +0000</pubDate>
		<dc:creator>carsonm</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://blogs.missiondata.com/?p=35</guid>
		<description><![CDATA[I&#8217;ve been doing a little C programming lately and I have found that if you have a up to date distribution of linux there are a lot of libraries out there that make doing things you do in other languages like java easier. As I have time I&#8217;m going to post some examples of what [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a little C programming lately and I have found that if you have a up to date distribution of linux there are a lot of libraries out there that make doing things you do in other languages like java easier. </p>
<p>As I have time I&#8217;m going to post some examples of what I have found. The first here is how to base64 encode a chunk of memory using OpenSLL.</p>
<p><span id="more-35"></span></p>
<pre>
<code>#include &lt;string.h&gt;

#include &lt;openssl/sha.h&gt;
#include &lt;openssl/hmac.h&gt;
#include &lt;openssl/evp.h&gt;
#include &lt;openssl/bio.h&gt;
#include &lt;openssl/buffer.h&gt;

char *base64(const unsigned char *input, int length);

int main(int argc, char **argv)
{
  char *output = base64("YOYO!", sizeof("YOYO!"));
  printf("Base64: *%s*\n", output);
  free(output);
}

char *base64(const unsigned char *input, int length)
{
  BIO *bmem, *b64;
  BUF_MEM *bptr;

  b64 = BIO_new(BIO_f_base64());
  bmem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bmem);
  BIO_write(b64, input, length);
  BIO_flush(b64);
  BIO_get_mem_ptr(b64, &amp;bptr);

  char *buff = (char *)malloc(bptr-&gt;length);
  memcpy(buff, bptr-&gt;data, bptr-&gt;length-1);
  buff[bptr-&gt;length-1] = 0;

  BIO_free_all(b64);

  return buff;
}</code>
</pre>
<p>And to compile this just use the following command:</p>
<pre>
<code>cc -o base64 base64.c -lssl</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.missiondata.com/blog/software-development/35/howto-base64-encode-with-cc-and-openssl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

