The string format of the date and time can vary considerably with the preference of each user. The strftime(3) function makes it easier for the C programmer to implement custom date and time formats. Its synopsis is as follows:
#include <time.h> size_t strftime(char *buf, size_t maxsize, const char *format, const struct tm *timeptr);
The arguments buf and maxsize specify the receiving buffer and its maximum size, respectively. The argument format specifies a printf(3)-like format string. The last argument, timeptr, points to a struct tm structure that will supply all of the input date and time values. The final output string size is returned, excluding the null byte.
If the output buffer is not large enough, the value maxsize is returned, indicating that maxsize characters were placed into the buffer. However, since there is no room for the null byte when this happens, do not expect one to be there.
The format specifiers are quite different from the sprintf(3) variety. Table 11.2 lists the format specifiers that are supported. Notice that each specifier starts with the percent character (%) and is followed by a letter. All other text in the format string is copied verbatim, in the same way that sprintf(3) does. To include a percent character, use two successive percent characters.
To enable you to try out the strftime(3) C function, it has been included in the class DTime as the method DTime::strftime(). This is shown in Listing 11.7.
A C++ function operator<<() was implemented in Listing 11.7 to make it possible to display this DTime class using the overloaded C++ << operator. Line 25 checks to see if there is a current time for the object and, if so, the DTime::ctime() method is called to format a date/time string (line 28). This string is then sent to the output stream.
Listing 11.8 shows a main program that will instantiate a DTime class and then invoke some operations on it.
Compiling and running this program yields output similar to the following:
$ make cc -c -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE -Wall gettime.cc cc -c -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE -Wall ctime.cc cc -c -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE -Wall asctime.cc cc -c -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE -Wall localtime.cc cc -c -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE -Wall mktime.cc cc -c -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE -Wall strftime.cc cc -c -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE -Wall main.cc cc -o dtime gettime.o ctime.o asctime.o localtime.o mktime.o strftime.o main.o -lstdc++ $ ./dtime Local UNIX Epoch time is 'Wed Dec 31 19:00:00 1969' Current time is 'Sun May 7 22:05:54 2000' 30 days from now is 'Tue Jun 6 22:05:54 2000' That date is 6/7/2000 2:5:54 UTC The 1st is 'Mon May 1 00:00:00 2000'in this month which is the same as Monday May 01, 2000 at 12:00 AM $
The first line of program output states your local time for UNIX Epoch Time. The example output was produced in the EST zone. Yours will differ if you are in a different time zone. This is accomplished in lines 13 and 14 of main.cc, shown in Listing 11.8. Line 13 sets the UNIX Epoch Time, which is the value (time_t)(0).
The next line of output beginning with Current time is is produced by lines 17 and 18. Line 17 sets the current date and time by calling obj.getTime().
Line 21 adds 30 days to the current time in obj using the overloaded += operator. Then the object is directed to cout in line 22 to display the date.
Line 25 establishes UTC values in the struct tm members that DTime inherits. Lines 26–29 access the structure members to send a manually formatted UTC time to cout.
Line 32 obtains the current time again for object obj. Lines 33–35 establish the first of the current month at midnight. Method DTime::mktime() is invoked at line 36, and then the object is sent to cout in line 37, displaying what the first of the current month is.
The last test in lines 39 and 40 tests the strftime(3) function by calling on the method DTime::strftime().
Some of the format specifiers that strftime(3) supports format according to a locale. An example of this is the %A specifier (the full weekday name).
In the UNIX context, the locale represents the language and cultural rules that are used on a particular host system. It defines the language used for certain messages and the lexicographic conventions for date and time. Locale also establishes the character set that is to be used.
The locale setting will determine whether, for example, your system uses the English names for the days of the week or French names. The names of the months are also affected by locale. Lexicographical conventions such as the %X specifier dictate whether the time should be shown in 12- or 24-hour format, for example.
For more information about locale, view the man(1) page for mklocale(1) under FreeBSD.
3.138.174.195