RFC Date and Time Conversion

From Nutwiki
Jump to: navigation, search

Test Environments

Hardware Comments Nut/OS
4.8.3
Nut/OS
4.8.7
Ethernut 1.3 H
Ethernut 2.1 B
Ethernut 3.0 E OK
Binaries
Compiler: ARM-GCC 4.3.3
OK
Binaries
Compiler: ARM-GCC 4.3.3

Overview

This example demonstrates the usage of the RfcTime functions Nut/OS provides. In order to use them you have to add -lnutpro to your Makefile or you'll get an "undefined reference" error.

Source

<source lang="c">

  1. include <dev/board.h>
  2. include <io.h>
  3. include <stdio.h>
  1. define MAX_DATES 8

const prog_char *dates[MAX_DATES] = { "6 May 1949 00:00:00",

   "12 September 1958 00:00:00",
   "1 May 1964 00:00:00",
   "2 December 1968 00:00:00",
   "22 May 1973 00:00:00",
   "6 October 1973 00:00:00",
   "1 April 1976 00:00:00",
   "12 August 1981 00:00:00"

};

const prog_char *events[MAX_DATES] = { "The EDSAC performs its first calculation on.",

   "At Texas Instruments, Jack Kilby demonstrates the world's first integrated circuit, containing five components on a piece of germanium half an inch long and thinner than a toothpick.",
   "At Dartmouth College, in Hanover, New Hampshire, the BASIC programming language runs for the first time. ",
   "Douglas C. Engelbart, of the Stanford Research Institute, demonstrates his system of keyboard, keypad, mouse, and windows at the Fall Joint Computer Conference in San Francisco's Civic Center.",
   "At Xerox PARC, Bob Metcalfe invents the Ethernet computer connectivity system, describing in a memo how the technology would work.",
   "Volkswagen rents one of the largest computers in the world to be used for commercial purposes, the IBM 370/195",
   "Steve Wozniak, Steve Jobs and Ronald Wayne Found Apple Computer Inc.",
   "IBM introduces its personal computer with Microsoft's 16-bit operating system, MS-DOS 1.0"

};

int main(void) {

   unsigned long baud = 115200;
   int i = 0;
   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   puts("RTC time conversions\n");
   puts("Well, hello there. Welcome to the Ethernut history lesson.");
   NutSleep(3000);
   for (i = 0; i < MAX_DATES; i++) {
       printf("\n\t%s:\n%s\n", dates[i], events[i]);
       NutSleep(3000);
   }
   NutSleep(2000);
   puts("Those dates are strings. But thanks to the Nut/OS RfcTime functions we can handle them as numbers instead. Have a look:");
   NutSleep(3000);
   for (i = 0; i < MAX_DATES; i++) {
       printf("\n\t%d:\n%s\n", (int) RfcTimeParse(dates[i]), events[i]);
       NutSleep(3000);
   }
   NutSleep(2000);
   puts("This also works the other way round: We can convert those nice time numbers back into easliy readable strings.");
   NutSleep(3000);
   for (i = 0; i < MAX_DATES; i++) {
       time_t tmp_epoch = RfcTimeParse(dates[i]);
       tm *tmp_tm = gmtime(&tmp_epoch);
       printf("\n\t%s:\n%s\n", Rfc1123TimeString(tmp_tm), events[i]);
       NutSleep(3000);
   }
   for (;;);

} </source>

Details

<source lang="c">

  1. define MAX_DATES 8

const prog_char *dates[MAX_DATES] = { "6 May 1949 00:00:00",

   "12 September 1958 00:00:00",
   "1 May 1964 00:00:00",
   "2 December 1968 00:00:00",
   "22 May 1973 00:00:00",
   "6 October 1973 00:00:00",
   "1 April 1976 00:00:00",
   "12 August 1981 00:00:00"

};

const prog_char *events[MAX_DATES] = { "The EDSAC performs its first calculation on.",

   "At Texas Instruments, Jack Kilby demonstrates the world's first integrated circuit, containing five components on a piece of germanium half an inch long and thinner than a toothpick.",
   "At Dartmouth College, in Hanover, New Hampshire, the BASIC programming language runs for the first time. ",
   "Douglas C. Engelbart, of the Stanford Research Institute, demonstrates his system of keyboard, keypad, mouse, and windows at the Fall Joint Computer Conference in San Francisco's Civic Center.",
   "At Xerox PARC, Bob Metcalfe invents the Ethernet computer connectivity system, describing in a memo how the technology would work.",
   "Volkswagen rents one of the largest computers in the world to be used for commercial purposes, the IBM 370/195",
   "Steve Wozniak, Steve Jobs and Ronald Wayne Found Apple Computer Inc.",
   "IBM introduces its personal computer with Microsoft's 16-bit operating system, MS-DOS 1.0"

}; </source>

Before the actual program we define 2 arrays, one containing dates and the other containing events happening at those specific dates. Those will be used to demo the conversion of timestamps between the different formats.

<source lang="c">

   unsigned long baud = 115200;
   int i = 0;
   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   puts("RTC time conversions\n");
   puts("Well, hello there. Welcome to the Ethernut history lesson.");
   NutSleep(3000);

</source>

Our program starts off by registering the debug device for output.

<source lang="c">

   for (i = 0; i < MAX_DATES; i++) {
       printf("\n\t%s:\n%s\n", dates[i], events[i]);
       NutSleep(3000);
   }

</source>

Then we display our short history lesson using only the predefined strings. Nothing too special here, right?

<source lang="c">

   for (i = 0; i < MAX_DATES; i++) {
       printf("\n\t%d:\n%s\n", (int) RfcTimeParse(dates[i]), events[i]);
       NutSleep(3000);
   }

</source>

Now we do the same thing but with timestamps converted from strings to epoch time values. Epoch time values are easy to handle for programs because they are just a number: The number of seconds passed sind 1 Jan 1970 00:00:00. Now you'll probably notice one problem here: Some of the dates we used are way before 1970! That's right. Those values can NOT be converted to epoch time and will return a result of -1. For demonstration purposes we are doing this, though.

<source lang="c">

   for (i = 0; i < MAX_DATES; i++) {
       time_t tmp_epoch = RfcTimeParse(dates[i]);
       tm *tmp_tm = gmtime(&tmp_epoch);
       printf("\n\t%s:\n%s\n", Rfc1123TimeString(tmp_tm), events[i]);
       NutSleep(3000);
   }

</source>

The last run of very identical to the others. To avoid lots of parentheses it uses some substeps, though. First we convert our timestamp string into an epoch time. From there we convert it to a tm structure which in turn can easily be converted to an Rfc1123 Time String. Why would one want to do that? Well, you won't. Not the way we do it here. This part just showcases that you can convert from almost any timestamp format to another.

You'll also notice that the program now adds the weekdays that the dates were to the string. Those are calculated automatically and prove that we are indeed converting here, not just displaying the strings we already had when we started.

Output

<source lang="text"> RFC time conversions

Well, hello there. Welcome to the Ethernut history lesson.

       6 May 1949 00:00:00:

The EDSAC performs its first calculation on.

       12 September 1958 00:00:00:

At Texas Instruments, Jack Kilby demonstrates the world's first integrated circu it, containing five components on a piece of germanium half an inch long and thi nner than a toothpick.

       1 May 1964 00:00:00:

At Dartmouth College, in Hanover, New Hampshire, the BASIC programming language runs for the first time.

       2 December 1968 00:00:00:

Douglas C. Engelbart, of the Stanford Research Institute, demonstrates his syste m of keyboard, keypad, mouse, and windows at the Fall Joint Computer Conference in San Francisco's Civic Center.

       22 May 1973 00:00:00:

At Xerox PARC, Bob Metcalfe invents the Ethernet computer connectivity system, d escribing in a memo how the technology would work.

       6 October 1973 00:00:00:

Volkswagen rents one of the largest computers in the world to be used for commer cial purposes, the IBM 370/195

       1 April 1976 00:00:00:

Steve Wozniak, Steve Jobs and Ronald Wayne Found Apple Computer Inc.

       12 August 1981 00:00:00:

IBM introduces its personal computer with Microsoft's 16-bit operating system, M S-DOS 1.0 Those dates are strings. But thanks to the Nut/OS RfcTime functions we can handl e them as numbers instead. Have a look:

       -1:

The EDSAC performs its first calculation on.

       -1:

At Texas Instruments, Jack Kilby demonstrates the world's first integrated circu it, containing five components on a piece of germanium half an inch long and thi nner than a toothpick.

       -1:

At Dartmouth College, in Hanover, New Hampshire, the BASIC programming language runs for the first time.

       -1:

Douglas C. Engelbart, of the Stanford Research Institute, demonstrates his syste m of keyboard, keypad, mouse, and windows at the Fall Joint Computer Conference in San Francisco's Civic Center.

       106876800:

At Xerox PARC, Bob Metcalfe invents the Ethernet computer connectivity system, d escribing in a memo how the technology would work.

       118713600:

Volkswagen rents one of the largest computers in the world to be used for commer cial purposes, the IBM 370/195

       197164800:

Steve Wozniak, Steve Jobs and Ronald Wayne Found Apple Computer Inc.

       366422400:

IBM introduces its personal computer with Microsoft's 16-bit operating system, M S-DOS 1.0 This also works the other way round: We can convert those nice time numbers back

into easliy readable strings.
       Sun, 06 Feb 2106 06:28:15:

The EDSAC performs its first calculation on.

       Sun, 06 Feb 2106 06:28:15:

At Texas Instruments, Jack Kilby demonstrates the world's first integrated circu it, containing five components on a piece of germanium half an inch long and thi nner than a toothpick.

       Sun, 06 Feb 2106 06:28:15:

At Dartmouth College, in Hanover, New Hampshire, the BASIC programming language runs for the first time.

       Sun, 06 Feb 2106 06:28:15:

Douglas C. Engelbart, of the Stanford Research Institute, demonstrates his syste m of keyboard, keypad, mouse, and windows at the Fall Joint Computer Conference in San Francisco's Civic Center.

       Tue, 22 May 1973 00:00:00:

At Xerox PARC, Bob Metcalfe invents the Ethernet computer connectivity system, d escribing in a memo how the technology would work.

       Sat, 06 Oct 1973 00:00:00:

Volkswagen rents one of the largest computers in the world to be used for commer cial purposes, the IBM 370/195

       Thu, 01 Apr 1976 00:00:00:

Steve Wozniak, Steve Jobs and Ronald Wayne Found Apple Computer Inc.

       Wed, 12 Aug 1981 00:00:00:

IBM introduces its personal computer with Microsoft's 16-bit operating system, M S-DOS 1.0 </source>

See also