Discuss this article in the Nuts & Volts forums at http://forum.nutsvolts.com.
SMILEY’S WORKSHOP
How Time Data is Stored
Back in Workshop 48 (July 2012) when we studied
data structures and enums, we chose to use time data for
our examples. While helping us to understand how to use
data structures, the examples weren't how the data is
actually structured in the C standard library, so let's look
at how the big boys do it.
First, we will look at the central timekeeping data
structure tm which is located in time.h:
struct tm {
int tm_sec; /* seconds after the minute [0-61]
int tm_min; /* minutes after the hour [0-59]
int tm_hour; /* hours since midnight [0-23] */
int tm_mday; /* day of the month [1-31] */
int tm_mon; /* months since January [0-11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday [0-6] */
int tm_yday; /* days since January 1 [0-365] */
int tm_isdst; /* Daylight Savings Time flag */
};
That just about covers everything we'll need to
express a date or time.
Now that we have a structure to hang things on, we
need a compact way to store a date and time. Our
structure has nine ints — most of which are far larger than
necessary for storing a particular value. For instance,
month only has values of 0 to 11 which could easily be
stored in four bits. Since an int is usually 16 bits on a
microcontroller, that's a lot of wasted space.
We use ints because they are the
unit of data exchange in most
microcontrollers, so it is easier for the
device to deal with ints rather than
other data types that aren't physically
present on the machine.
Of course, early microcontrollers
were four-bit machines so an int was
perfect. Devices in use today are
usually 16 bits, but can easily be 32 or
even 64 bits.
■ FIGURE 3. Arduino proto shield alarm clock kit.
Item
1a, 1b
2
3
4
5
6a, 6b
7
8
9a, 9b
10
11
12
13
14
15
16
17
18
19
20
21
February 2013 59
Description
Two proto shield PCBs
One mini breadboard
Four-pin female header
10-pin shield header
Eight-pin shield header
Six-pin shield header
10-pin male header
Eight-pin male header
One DS1307 RTC IC
One 32.768 kHz watch crystal
One breadboard battery PCB
One battery holder 12 mm coin
One battery CR1220
One two-pin 90 degree male header
One capacitor 0.1 µF
Two resistors 2.2K ohm
One 10K ohm pull-up resistor
One pushbutton
One piezo buzzer
Two feet of uncut jumper wire
Table 1. Bill of Materials.