Showing posts with label snippets. Show all posts
Showing posts with label snippets. Show all posts

Thursday, March 12, 2009

C++ Snippets: Time Part 2

In my previous post about telling time in C++, I showed how to display the current Linux system time in a human-readable format. Today I want to go a little bit beyond that and explain how to customize that format.

If you read my previous post, you saw that the asctime function always returns the time in the same format (Thu Mar 12 18:30:50 2009). That's much better than displaying the time in the number of seconds since January 1, 1970, but it still leaves room for improvement.

I'm going to show you how to create a custom format to display the date and time however you choose using the strftime function from the ctime library. I want to rearrange things so the time above would be displayed as:

Thursday, March 12, 2009 06:30:50 PM

First, let's take a look at the arguments that strftime takes.

char *str - A buffer that will hold the formatted output. Strftime was originally a C function, so we can't just return a string. That's okay, though, we'll just make the buffer big enough to hold any time and date in our specified format.

size_t maxsize – The maximum number of characters that will be copied to the output buffer. This should be the same value as the size of the buffer used as the first input parameter.

const char *fmt
– Specifies exactly how we want the time to be formatted. There are some special format codes that I'll go over next, but be aware that normal text can be included as well.

struct tm *time
- The time value we want to format.

In order to display the time in the desired format, we'll use the following format specifiers:
%A - full weekday name
%B - full month name
%d - day of the month
%Y - year in (with the century)
%I - hour in 12 hour format
%M - minute as a number
%S - second as a number
%p - locale equivalent of AM/PM
We'll combine all of these format codes together, along with normal text delimiters (commas, colons, and spaces) to form one long format string.
%A, %B %d, %Y %I:%M:%S %p
Finally, here's the code:
#include <iostream>
#include <ctime>

using namespace std;

int main ()
{
struct tm * timeinfo;
char buffer [80];
time_t seconds = time(NULL);

timeinfo = localtime( &seconds );
char format[] = "%A, %B %d, %Y %I:%M:%S %p";

strftime(buffer, 80, format, timeinfo);
cout << "Local time: " << buffer << endl;

return 0;
}
After compiling the code snippet above, you should see output similar to the following:
$ ./a.out
Local time: Thursday, March 12, 2009 06:53:13 PM

Monday, February 16, 2009

C++ Snippets: Print Precision

How do you print a float or a double to its full precision? By default cout only prints six digits of precision, but you can easily change that.
#include <iostream>
#include <limits>

using namespace std;

typedef numeric_limits<double> dbl;

int main()
{
double d = 3.14159265358979;
cout.precision(dbl::digits10);
cout << "Pi: " << fixed << d << endl;
return 0;
}

Use cout's precision function to set the precision to an arbitrary length. Here I used the maximum precision of a double float from the <limits> library. When I print the value, I tell cout to use the specified precision by passing it the fixed specifier.

Compile and run the snippet above and you should see the following output.
$ ./a.out
Pi: 3.141592653589790

Sunday, February 15, 2009

C++ Snippets: Time

There's been a lot of talk about Unix time lately surrounding 1234567890 day. If you read some of the recent articles you might be left with the impression that some of us actually tell time this way. Only hard-core Unix sysadmins actually do that. The rest of us can use standard C++ library functions to get the current system time and convert it to a human-readable format.

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
tm* timeinfo;

time_t seconds = time(NULL);
timeinfo = localtime(&seconds);

cout << "Seconds since the epoch: " << seconds << endl;
cout << "Local time: " << asctime(timeinfo) << endl;
return 0;
}

Here I'm using three functions and one stuct from the C++ standard ctime library to get the time and display it. The time function gets the number of seconds since January 1, 1970 (the start of the Unix epoch) from the operating system. The localtime function converts the time in seconds to a tm structure representing the calendar date and time. Finally, the asctime function converts the tm stucture to a human-readable string format.

Compile the program above using g++, then run it to see the following output.
$ ./a.out
Seconds since the epoch: 1234712674
Local time: Sun Feb 15 10:44:34 2009
Naturally, you'll see different results depending on when you run the executable.