TTime formatl sample

Source: Internet
Author: User

TTime currenttime;
Currenttime. hometime ();
Tbuf <100> time;
Currenttime. formatl (time, _ L ("% Y _ % m _ % d _ % H _ % T _ % S _ % C"); // ___ 18_13_37_474750
Log (time );
Currenttime. formatl (time, _ L ("% Y % m % d % H % T % S % C"); // 181337474750
Log (time );
Currenttime. formatl (time, _ L ("% Y % m % d % H % T % S % C"); // 181337474750
Log (time );
Currenttime. formatl (time, _ L ("% Y-% m-% d % H % T % S % C"); // -- 181337474750
Log (time );
Currenttime. formatl (time, _ L ("% H: % T: % s on % E, % F % d-% * m-% Y ")); // 18:13:37 on Monday, 13-8-2007
Log (time );
Currenttime. formatl (time, _ L ("% H: % T: % s on % E, % F % d-% m-% Y"); // 18:13:37 on Monday, 13-08-2007
Log (time );
Currenttime. formatl (time, _ L ("% F % d % m % Y % H: % t"); // 13 08 2007
Log (time );
Currenttime. formatl (time, _ L ("%-B %: 0% J %: 1% T %: 2% S %: 3"); // 18:13:37
Log (time );
Currenttime. formatl (time, _ L ("% Y % m % d %/0% 1%/1% 2%/2% 3%/3"); // 2007/08/13
Log (time );
Currenttime. formatl (time, _ L ("% H % T % S % C % F % d % m % Y"); // 18133701234513082007
Log (time );
Currenttime. formatl (time, _ L ("%-B %: 0% J %: 1% T %: 2% S % + B % d % N % Y % 4% 5% 3 ")); // 7:36:35 August 06 06
Log (time );


TTime time;
Time. hometime ();
Tbuf <32> timebuf;
Time. formatl (timebuf, _ L ("% F % Y/% m/% d, % H: % T: % s"); // 2008/08/16, 21:22:22

Details:
The format string is divided into local and local irrelevant format strings, that is, it is related to the format set in the system time and date.
% Is required for all the following format characters
Local independence:
%: Because % is a parameter character, when you need to output %, escape with two consecutive %
*: After %, % indicates the abbreviation is required. For example, % * t indicates the abbreviation of minute. If not two digits, the leading 0 is removed.
C: 6-bit microseconds. % C is 6-bit by default. % * C2 indicates taking only the first two digits, and % * C4 indicates taking the first four digits.
S: 2-digit second. Add * to remove leading 0.
T: 2-digit score. Add * to remove leading 0.
H: In the 24-hour format, add * to remove the leading 0.
I: In the 12-hour format, adding or not * removes the leading 0.
F: % F indicates that all three parameters d/M/y are local independent and output according to the format string as is. % F must be used before % d, % m, and % Y.
D: 2-digit day, add * to remove leading 0
M: two-digit month, plus * to remove leading 0
Y: 4-digit year. Add * to remove the first two digits. For example, 2008 only shows 08.
E: Sunday ~ Saturday, plus * to display the abbreviation, Sun ~ SAT; in Chinese, Monday ~ On Sunday, add * to display the abbreviation, 1 ~ Day
N: name of the month. In English, January ~ December, and add * to display the abbreviation Jan ~ Dec; in Chinese, September January ~ January 1, December, with the abbreviation "*" displayed, Arabic numerals 1 ~ 12. Pay attention to ~ The numbers in December are Arabic, not Chinese characters ~ 12th
W: two digits. The week of the current date in the year. Add * to remove the leading 0.
X: Date suffix. For example, % * D % x is shown as 3rd (3), and % d % x is displayed as 02nd (2)
Z: three digits. The number of the day when the current date is in the year. Add * to remove the leading 0.
Local:
. :( Point) %. The default value is the decimal point, which indicates the delimiter between seconds and milliseconds. You can set it through tlocale: setdecimalseparator.
: :( Colon) %: gets the time Separator in the local settings, followed by a 0 ~ A number in the range of 3 to specify the date separator.
The format is as follows: S0 XX S1 YY S2 ZZ S3, where xx, YY, ZZ represents the hour, minute, and second, S0 ~ S3 represents four delimiters. %: 0 indicates the first separator, and so on. Note that the time Separator in the local settings only contains the colon and the dot. Of course, you can use tlocale: settimeseparator () to set the four separators. Both the simulator and the real machine have not been tested successfully, and the time Separator in the STANDBY state has not been changed.
/:( Slash) %/indicates to get the date Separator in the local settings, which is similar to the previous one. XX, YY, and ZZ represent the year, month, and day. The sequence can be set by tlocale: setdateformat. For separators, the local settings provide four types of characters for selection, but only valid for S1 and S2, S0 and S3 are always empty characters. You can use setdateseparator () to separate four separators (S0 ~ S3 is set separately and may be different. The test is successful on both the simulator and the real machine, and the date Separator in the STANDBY state is modified successfully. However, when the system enters the time and date setting, both the simulator and the real machine crash.
1: % 1, the first in the "year, month, and day" sequence in the local settings. The order of year, month, and day can be set through tlocale: setdateformat (). The format is as follows:
Edateamerican us format (mm/DD/YYYY) at this time % 1 is a month
Edateeuropean European format (dd/mm/yyyy) at this time % 1 is a day
Edatejapanese Japanese format (yyyy/mm/DD) at this time % 1 is a year
China uses the Japanese style
2: % 2, the second in the "year, month, and day" sequence in the local settings.
3: % 3, the third in the "year, month, and day" sequence in the local settings.
4: % 4, and % 2 redundancy. If edateamerican is set locally, % 4 is the day; % 4 of edateeuropean and edatejapanese is the month.
5: % 5, and % 3 redundancy. If edateamerican or edateeuropean is set locally, % 4 is the year; % 4 of edatejapanese is the day.
Therefore, in the local Format String (without the % F parameter), you can directly use % 1% 2% 3 when calling the year, month, and day, % m % d % Y is not required (% * m % * D % * Y is required only when the leading 0 is removed ), use %/1 for all separators (because S1 and S2 are valid locally and are the same). Of course, the order of year, month, and day is a problem. If you need to specify the order, the local irrelevant format string of % F is certainly used, which is simple and intuitive, and the format string is not easy to write incorrectly.

A: whether the local setting is 12-hour or 24-hour, % A indicates that AM/PM is always displayed. Tlocale: setampmspacebetween () is used to set whether spaces are left between the time and am/PM. Obviously, this setting affects the final formatted string. % * A will forcibly compress this space, regardless of whether space is set locally (note that the local settings mentioned here do not necessarily have the setting option in the system's time and date, for users, you only need to set several common settings. For example, the ampmspacebetween space is not set in the time and date, and the system has spaces by default ). As for whether the AM/PM in the formatted string appears before or after the time, it depends on how the format string is written: % I: % T: % S % A. 10:21:36 is displayed, % A % I: % T: % s, displaying PM 10:21:36
Extension options of:
+ Or-can be added before -,
% + A indicates to insert AM/PM to the formatted string only when tlocale: ampmsymbolposition () is set to elocaleafter;
%-A indicates to insert AM/PM to the formatted string only when tlocale: ampmsymbolposition () is set to elocalebefore.
Obviously, tlocale: ampmsymbolposition () is not set in the time and date settings.
For example: %-A % I: % T: % S % + A, the position of AM/PM in the formatted string is the same as that in the setting.
%-A % I: % T: % S % indicates that the AM/PM is inserted to the start of the formatted string only when the AM/PM flag is set.
B: The only difference with a is that AM/PM is displayed only in 12-hour mode. There are also two extension options: %-B and % + B.
J: determines whether the local setting is 12-hour or 24-hour. % J indicates that when the local setting is in the 12-hour format, the leading 0 after % J is forcibly removed, so with the % J parameter, there is no need to add.

Note:
% 1 ~ cannot be used when % F is used to indicate local independence ~ % 5 parameters
When the % F parameter is not used, the % m, % d, and % Y parameters do not have any effect, however, % * m, % * D, and % * y are used to remove leading 0 (% * Y is a two-digit year ).
The SDK indicates that % 1 ~ is separated by %/0, %/1, %/2, and %/3 ~ % 5 indicates the date (or %: 0, %: 1, %: 2, %: 3 When % H, % I, % T, and % s are separated), the four delimiters must be specified, by default, S0 and S3 are both '/0' (null characters ). I personally understand that this is done to make the format strings as standardized as possible and easy to understand. In fact, the four delimiters can be used as much as possible. The only limit is that % is used. %. it should be placed in the middle of S2 and S3. (In fact, In the end %. The default system is the DoT number. Generally, programmers will not be bored to call the tlocale: setdecimalseparator () method to change this separator)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.