A class that represents a string built up from a specific date and time

You're viewing an older version of this page (#113). View the current version.

View versions (2)

Interface

Overview

This class exposes methods that make it easy to append characters, normal integers, large integers as doubles (fractional part is discarded) and strings to a single string.

The format string

Some of the characters in the format string are considered special and will be replaced as specified by the tables below. If a character not in the tables is used, it will be used verbatim.

If one of the format characters is required to be used verbatim, it must be escaped with a backslash. Note that the backslash itself will have to be escaped with another backslash. So the character will have two backslashes in front of it in source code. Examples follow after the tables of format characters.

Format string examples

The format strings are given as seen in source code

<table> <tr> <td><strong>Format string</strong></td> <td><strong>Result</strong></td> </tr> <tr> <td>"l"</td> <td>Monday</td> </tr> <tr> <td>"l dS of F Y h:i:s A"</td> <td>Monday 28th of March 2005 02:25:22 PM</td> </tr> <tr> <td>"l \\t\\h\jS"</td> <td>Monday the 28th</td> </tr> <tr> <td>"F j, Y, g:i a"</td> <td>March 29, 2005, 1:19 am</td> </tr> <tr> <td>"m.d.y"</td> <td>03.29.05</td> </tr> <tr> <td>"j, n, Y"</td> <td>29, 3, 2005</td> </tr> <tr> <td>"Ymd"</td> <td>20050329</td> </tr> <tr> <td>"h-i-s, j-m-y, it is w Day z"</td> <td>01-21-55, 29-03-05, 2131 2155 2 Tueam05 87</td> </tr> <tr> <td>"\\i\\t \\i\\s \\t\\h\jS \\d\\a\\y."</td> <td>it is the 29th day.</td> </tr> <tr> <td>"D M j G:i:s T Y"</td> <td>Tue Mar 29 1:24:52 GMT Daylight Time 2005</td> </tr> <tr> <td>"H:m:s \\m \\i\\s\\ \\m\\o\
\\t\\h"</td> <td>01:03:15 m is month</td> </tr> <tr> <td>"H:i:s"</td> <td>01:25:40</td> </tr> </table>

None of the append functions affix a NULL character to the end of the string as it is expected that multiple append calls will be made at a time before the string is displayed and hence it is unnecessary to add a NULL each time a new entity is appended.

The memory dynamically allocated to the enclosed string is never freed by the class and must be freed by external code. This can be done with a statement such as:

delete[] dateString.getString();

Example 1

DateTime dt;
DateString dateString(20);

// Fill dt with a specific date and time
dateString.fill("h:i:s, jS F Y", dt, Units::Time::st_None);
dateString.trimAndEnd();
char* pStr = dateString.getString();

cout<<pStr;

delete[] pStr;
GPL Licence — free for non commercial use. See Licence details.

Members of DateString

CLASS MEMBER

m_length

The length in characters of the string (the index of the NULL character). It is always less than m_maxLength

CLASS MEMBER

m_maxLength

The maximum number of characters (including the NULL character) that can be inserted

CLASS MEMBER

m_pStr

The character buffer

CLASS METHOD

DateString

Author

Keith Athaide

CLASS METHOD

trim

It is recommended that repeated calls not be made to this function. This function should only be called just before the built up string is to be processed by external code.

Author

Keith Athaide

CLASS METHOD

end

Call this function before the string is to be displayed or processed.

Author

Keith Athaide

CLASS METHOD

getString

Unless the function end() were called, the returned string will not be NULL terminated.

The returned pointer should not be stored so as to always pointed to the formatted string, as successive calls to fill() or to trim() could cause the pointer to become invalid.

Returns

The date string
Author

Keith Athaide

CLASS METHOD

fill

Fills the string based on the given format, date and region

For convenience, the format characters have been arranged by category.

<table border="0"> <caption>Year format specifiers</caption> <tr> <td><strong>Option</strong></td> <td><strong>Description</strong></td> <td><strong>Examples</strong></td> </tr> <tr> <td>L</td> <td>Whether it's a leap year.</td> <td>1 if it is a leap year, 0 otherwise.</td> </tr> <tr> <td>Y</td> <td>A full numeric representation of a year, 4 digits. If needed, BC will be suffixed.</td> <td>4713BC, 1999, 2005 </td> </tr> <tr> <td>y</td> <td>A two digit representation of a year. If needed, BC will be suffixed.</td> <td>13BC, 99 or 05 </td> </tr> </table> <br> <table> <caption>Month format specifiers</caption> <tr> <td><strong>Option</strong></td> <td><strong>Description</strong></td> <td><strong>Examples</strong></td> </tr> <tr> <td>F</td> <td>A full textual representation of a month.</td> <td>Jan through Dec </td> </tr> <tr> <td>m</td> <td>Numeric representation of a month, with leading zeros.</td> <td>01 through 12 </td> </tr> <tr> <td>M</td> <td>A short textual representation of a month consisting of three letters.</td> <td>Jan through Dec </td> </tr> <tr> <td>n</td> <td>Numeric representation of a month, without leading zeros.</td> <td>1 through 12 </td> </tr> <tr> <td>t</td> <td>Number of days in the given month.</td> <td>28 through 31 </td> </tr> </table> <br> <table> <caption>Day format specifiers</caption> <tr> <td><strong>Option</strong></td> <td><strong>Description</strong></td> <td><strong>Examples</strong></td> </tr> <tr> <td>d</td> <td>Day of the month, 2 digits with leading zeros.</td> <td>01 through 31 </td> </tr> <tr> <td>j</td> <td>Day of the month without leading zeros.</td> <td>1 through 31 </td> </tr> <tr> <td>S</td> <td>English ordinal suffix for the day of the month, 2 characters. Works well with j</td> <td>st, nd, rd or th </td> </tr> <tr> <td>z</td> <td>The day of the year (starting from 0).</td> <td>0 through 365 </td> </tr> </table> <br> <table> <caption>Week format specifiers</caption> <tr> <td><strong>Option</strong></td> <td><strong>Description</strong></td> <td><strong>Examples</strong></td> </tr> <tr> <td>D</td> <td>A textual representation of a day, three letters.</td> <td>Sun through Sat </td> </tr> <tr> <td>l (lowercase 'L')</td> <td>A full textual representation of the day of the week.</td> <td>Sunday through Saturday </td> </tr> <tr> <td>w</td> <td>Numeric representation of the day of the week.</td> <td> 0 (for Sunday) through 6 (for Saturday)</td> </tr> <tr> <td>W</td> <td>ISO-8601 week number of year, weeks starting on Monday.</td> <td>42 </td> </tr> </table> <br> <table> <caption>Time format specifiers</caption> <tr> <td><strong>Option</strong></td> <td><strong>Description</strong></td> <td><strong>Examples</strong></td> </tr> <tr> <td>a</td> <td>Lowercase Ante meridiem and Post meridiem</td> <td>am or pm </td> </tr> <tr> <td>A</td> <td>Uppercase Ante meridiem and Post meridiem</td> <td>AM or PM </td> </tr> <tr> <td>B</td> <td>Swatch Internet time</td> <td>000 through 999 </td> </tr> <tr> <td>g</td> <td>12-hour format of an hour without leading zeros.</td> <td>1 through 12 </td> </tr> <tr> <td>G</td> <td>24-hour format of an hour without leading zeros.</td> <td>0 through 23 </td> </tr> <tr> <td>h</td> <td>12-hour format of an hour with leading zeros</td> <td>01 through 12 </td> </tr> <tr> <td>H</td> <td>24-hour format of an hour with leading zeros.</td> <td>00 through 23 </td> </tr> <tr> <td>i</td> <td>Minutes with leading zeros.</td> <td>00 to 59 </td> </tr> <tr> <td>s</td> <td>Seconds, with leading zeros.</td> <td>00 through 59 </td> </tr> <tr> <td>U</td> <td>Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)</td> <td>1112053296 </td> </tr> </table> <br> <table> <caption>Timezone related specifiers</caption> <tr> <td><strong>Option</strong></td> <td><strong>Description</strong></td> <td><strong>Examples</strong></td> </tr> <tr> <td>I (uppercase 'i')</td> <td>Whether or not the date is in daylights savings time.</td> <td>1 if Daylight Savings Time, 0 otherwise.</td> </tr> <tr> <td>O</td> <td>Difference to Greenwich time (GMT) in hours.</td> <td>+0200 , -1300 </td> </tr> <tr> <td>T</td> <td>Timezone setting of this machine.</td> <td>EST, MDT </td> </tr> <tr> <td>Z</td> <td>Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.</td> <td>-43200 through 43200 </td> </tr> </table> <br> <table> <caption>Standard format specifiers</caption> <tr> <td><strong>Option</strong></td> <td><strong>Description</strong></td> <td><strong>Examples</strong></td> </tr> <tr> <td>c</td> <td>ISO 8601 date</td> <td><tt>2005-03-29T00:47:51+0100</tt></td> </tr> <tr> <td>r</td> <td>RFC 2822 formatted date</td> <td><tt>Tue, 29 Mar 2005 00:47:51 +0100</tt></td> </tr> </table>

Parameters

format
Constant
dt
[in] The date and time to use
region
[in] The current region
Author

Keith Athaide

CLASS METHOD

resize

The member m_maxLength is automatically updated by this function to reflect the new size.

Returns

true if the character buffer was successfully resized, false otherwise
Author

Keith Athaide

CLASS METHOD

resizeIfNecessary

Checks the length of the character buffer and ensures that 'numCharsToBeAdded' plus the NULL character can be inserted into the character buffer. If the character buffer is not large enough, it is resized to twice its size.

Returns

true if the character buffer is large enough, false otherwise
Author

Keith Athaide

CLASS METHOD

append

This function accepts a double only to get numbers greater than a 32 bit integer can provide. The fractional part of the passed in number is discarded.

If a negative number is supplied, a '-' character is prefixed. No prefix is inserted for a positive number.

Author

Keith Athaide

CLASS METHOD

append

If a negative number is supplied, a '-' character is prefixed. No prefix is inserted for a positive number.

Author

Keith Athaide

CLASS METHOD

append

Parameters

pStrToAppend
Constant
Author

Keith Athaide

CLASS METHOD

append

Author

Keith Athaide