Subversion Repositories Kolibri OS

Rev

Rev 4872 | Rev 4921 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* NOTE:  This file defines both strftime() and wcsftime().  Take care when
  2.  * making changes.  See also wcsftime.c, and note the (small) overlap in the
  3.  * manual description, taking care to edit both as needed.  */
  4. /*
  5.  * strftime.c
  6.  * Original Author:     G. Haley
  7.  * Additions from:      Eric Blake
  8.  * Changes to allow dual use as wcstime, also:  Craig Howland
  9.  *
  10.  * Places characters into the array pointed to by s as controlled by the string
  11.  * pointed to by format. If the total number of resulting characters including
  12.  * the terminating null character is not more than maxsize, returns the number
  13.  * of characters placed into the array pointed to by s (not including the
  14.  * terminating null character); otherwise zero is returned and the contents of
  15.  * the array indeterminate.
  16.  */
  17.  
  18. /*
  19. FUNCTION
  20. <<strftime>>---convert date and time to a formatted string
  21.  
  22. INDEX
  23.         strftime
  24.  
  25. ANSI_SYNOPSIS
  26.         #include <time.h>
  27.         size_t strftime(char *<[s]>, size_t <[maxsize]>,
  28.                         const char *<[format]>, const struct tm *<[timp]>);
  29.  
  30. TRAD_SYNOPSIS
  31.         #include <time.h>
  32.         size_t strftime(<[s]>, <[maxsize]>, <[format]>, <[timp]>)
  33.         char *<[s]>;
  34.         size_t <[maxsize]>;
  35.         char *<[format]>;
  36.         struct tm *<[timp]>;
  37.  
  38. DESCRIPTION
  39. <<strftime>> converts a <<struct tm>> representation of the time (at
  40. <[timp]>) into a null-terminated string, starting at <[s]> and occupying
  41. no more than <[maxsize]> characters.
  42.  
  43. You control the format of the output using the string at <[format]>.
  44. <<*<[format]>>> can contain two kinds of specifications: text to be
  45. copied literally into the formatted string, and time conversion
  46. specifications.  Time conversion specifications are two- and
  47. three-character sequences beginning with `<<%>>' (use `<<%%>>' to
  48. include a percent sign in the output).  Each defined conversion
  49. specification selects only the specified field(s) of calendar time
  50. data from <<*<[timp]>>>, and converts it to a string in one of the
  51. following ways:
  52.  
  53. o+
  54. o %a
  55. The abbreviated weekday name according to the current locale. [tm_wday]
  56.  
  57. o %A
  58. The full weekday name according to the current locale.
  59. In the default "C" locale, one of `<<Sunday>>', `<<Monday>>', `<<Tuesday>>',
  60. `<<Wednesday>>', `<<Thursday>>', `<<Friday>>', `<<Saturday>>'. [tm_wday]
  61.  
  62. o %b
  63. The abbreviated month name according to the current locale. [tm_mon]
  64.  
  65. o %B
  66. The full month name according to the current locale.
  67. In the default "C" locale, one of `<<January>>', `<<February>>',
  68. `<<March>>', `<<April>>', `<<May>>', `<<June>>', `<<July>>',
  69. `<<August>>', `<<September>>', `<<October>>', `<<November>>',
  70. `<<December>>'. [tm_mon]
  71.  
  72. o %c
  73. The preferred date and time representation for the current locale.
  74. [tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday]
  75.  
  76. o %C
  77. The century, that is, the year divided by 100 then truncated.  For
  78. 4-digit years, the result is zero-padded and exactly two characters;
  79. but for other years, there may a negative sign or more digits.  In
  80. this way, `<<%C%y>>' is equivalent to `<<%Y>>'. [tm_year]
  81.  
  82. o %d
  83. The day of the month, formatted with two digits (from `<<01>>' to
  84. `<<31>>'). [tm_mday]
  85.  
  86. o %D
  87. A string representing the date, in the form `<<"%m/%d/%y">>'.
  88. [tm_mday, tm_mon, tm_year]
  89.  
  90. o %e
  91. The day of the month, formatted with leading space if single digit
  92. (from `<<1>>' to `<<31>>'). [tm_mday]
  93.  
  94. o %E<<x>>
  95. In some locales, the E modifier selects alternative representations of
  96. certain modifiers <<x>>.  In newlib, it is ignored, and treated as %<<x>>.
  97.  
  98. o %F
  99. A string representing the ISO 8601:2000 date format, in the form
  100. `<<"%Y-%m-%d">>'. [tm_mday, tm_mon, tm_year]
  101.  
  102. o %g
  103. The last two digits of the week-based year, see specifier %G (from
  104. `<<00>>' to `<<99>>'). [tm_year, tm_wday, tm_yday]
  105.  
  106. o %G
  107. The week-based year. In the ISO 8601:2000 calendar, week 1 of the year
  108. includes January 4th, and begin on Mondays. Therefore, if January 1st,
  109. 2nd, or 3rd falls on a Sunday, that day and earlier belong to the last
  110. week of the previous year; and if December 29th, 30th, or 31st falls
  111. on Monday, that day and later belong to week 1 of the next year.  For
  112. consistency with %Y, it always has at least four characters.
  113. Example: "%G" for Saturday 2nd January 1999 gives "1998", and for
  114. Tuesday 30th December 1997 gives "1998". [tm_year, tm_wday, tm_yday]
  115.  
  116. o %h
  117. Synonym for "%b". [tm_mon]
  118.  
  119. o %H
  120. The hour (on a 24-hour clock), formatted with two digits (from
  121. `<<00>>' to `<<23>>'). [tm_hour]
  122.  
  123. o %I
  124. The hour (on a 12-hour clock), formatted with two digits (from
  125. `<<01>>' to `<<12>>'). [tm_hour]
  126.  
  127. o %j
  128. The count of days in the year, formatted with three digits
  129. (from `<<001>>' to `<<366>>'). [tm_yday]
  130.  
  131. o %k
  132. The hour (on a 24-hour clock), formatted with leading space if single
  133. digit (from `<<0>>' to `<<23>>'). Non-POSIX extension (c.p. %I). [tm_hour]
  134.  
  135. o %l
  136. The hour (on a 12-hour clock), formatted with leading space if single
  137. digit (from `<<1>>' to `<<12>>'). Non-POSIX extension (c.p. %H). [tm_hour]
  138.  
  139. o %m
  140. The month number, formatted with two digits (from `<<01>>' to `<<12>>').
  141. [tm_mon]
  142.  
  143. o %M
  144. The minute, formatted with two digits (from `<<00>>' to `<<59>>'). [tm_min]
  145.  
  146. o %n
  147. A newline character (`<<\n>>').
  148.  
  149. o %O<<x>>
  150. In some locales, the O modifier selects alternative digit characters
  151. for certain modifiers <<x>>.  In newlib, it is ignored, and treated as %<<x>>.
  152.  
  153. o %p
  154. Either `<<AM>>' or `<<PM>>' as appropriate, or the corresponding strings for
  155. the current locale. [tm_hour]
  156.  
  157. o %P
  158. Same as '<<%p>>', but in lowercase.  This is a GNU extension. [tm_hour]
  159.  
  160. o %r
  161. Replaced by the time in a.m. and p.m. notation.  In the "C" locale this
  162. is equivalent to "%I:%M:%S %p".  In locales which don't define a.m./p.m.
  163. notations, the result is an empty string. [tm_sec, tm_min, tm_hour]
  164.  
  165. o %R
  166. The 24-hour time, to the minute.  Equivalent to "%H:%M". [tm_min, tm_hour]
  167.  
  168. o %S
  169. The second, formatted with two digits (from `<<00>>' to `<<60>>').  The
  170. value 60 accounts for the occasional leap second. [tm_sec]
  171.  
  172. o %t
  173. A tab character (`<<\t>>').
  174.  
  175. o %T
  176. The 24-hour time, to the second.  Equivalent to "%H:%M:%S". [tm_sec,
  177. tm_min, tm_hour]
  178.  
  179. o %u
  180. The weekday as a number, 1-based from Monday (from `<<1>>' to
  181. `<<7>>'). [tm_wday]
  182.  
  183. o %U
  184. The week number, where weeks start on Sunday, week 1 contains the first
  185. Sunday in a year, and earlier days are in week 0.  Formatted with two
  186. digits (from `<<00>>' to `<<53>>').  See also <<%W>>. [tm_wday, tm_yday]
  187.  
  188. o %V
  189. The week number, where weeks start on Monday, week 1 contains January 4th,
  190. and earlier days are in the previous year.  Formatted with two digits
  191. (from `<<01>>' to `<<53>>').  See also <<%G>>. [tm_year, tm_wday, tm_yday]
  192.  
  193. o %w
  194. The weekday as a number, 0-based from Sunday (from `<<0>>' to `<<6>>').
  195. [tm_wday]
  196.  
  197. o %W
  198. The week number, where weeks start on Monday, week 1 contains the first
  199. Monday in a year, and earlier days are in week 0.  Formatted with two
  200. digits (from `<<00>>' to `<<53>>'). [tm_wday, tm_yday]
  201.  
  202. o %x
  203. Replaced by the preferred date representation in the current locale.
  204. In the "C" locale this is equivalent to "%m/%d/%y".
  205. [tm_mon, tm_mday, tm_year]
  206.  
  207. o %X
  208. Replaced by the preferred time representation in the current locale.
  209. In the "C" locale this is equivalent to "%H:%M:%S". [tm_sec, tm_min, tm_hour]
  210.  
  211. o %y
  212. The last two digits of the year (from `<<00>>' to `<<99>>'). [tm_year]
  213. (Implementation interpretation:  always positive, even for negative years.)
  214.  
  215. o %Y
  216. The full year, equivalent to <<%C%y>>.  It will always have at least four
  217. characters, but may have more.  The year is accurate even when tm_year
  218. added to the offset of 1900 overflows an int. [tm_year]
  219.  
  220. o %z
  221. The offset from UTC.  The format consists of a sign (negative is west of
  222. Greewich), two characters for hour, then two characters for minutes
  223. (-hhmm or +hhmm).  If tm_isdst is negative, the offset is unknown and no
  224. output is generated; if it is zero, the offset is the standard offset for
  225. the current time zone; and if it is positive, the offset is the daylight
  226. savings offset for the current timezone. The offset is determined from
  227. the TZ environment variable, as if by calling tzset(). [tm_isdst]
  228.  
  229. o %Z
  230. The time zone name.  If tm_isdst is negative, no output is generated.
  231. Otherwise, the time zone name is based on the TZ environment variable,
  232. as if by calling tzset(). [tm_isdst]
  233.  
  234. o %%
  235. A single character, `<<%>>'.
  236. o-
  237.  
  238. RETURNS
  239. When the formatted time takes up no more than <[maxsize]> characters,
  240. the result is the length of the formatted string.  Otherwise, if the
  241. formatting operation was abandoned due to lack of room, the result is
  242. <<0>>, and the string starting at <[s]> corresponds to just those
  243. parts of <<*<[format]>>> that could be completely filled in within the
  244. <[maxsize]> limit.
  245.  
  246. PORTABILITY
  247. ANSI C requires <<strftime>>, but does not specify the contents of
  248. <<*<[s]>>> when the formatted string would require more than
  249. <[maxsize]> characters.  Unrecognized specifiers and fields of
  250. <<timp>> that are out of range cause undefined results.  Since some
  251. formats expand to 0 bytes, it is wise to set <<*<[s]>>> to a nonzero
  252. value beforehand to distinguish between failure and an empty string.
  253. This implementation does not support <<s>> being NULL, nor overlapping
  254. <<s>> and <<format>>.
  255.  
  256. <<strftime>> requires no supporting OS subroutines.
  257.  
  258. BUGS
  259. <<strftime>> ignores the LC_TIME category of the current locale, hard-coding
  260. the "C" locale settings.
  261. */
  262.  
  263. #include <newlib.h>
  264. #include <sys/config.h>
  265. #include <stddef.h>
  266. #include <stdio.h>
  267. #include <time.h>
  268. #include <string.h>
  269. #include <stdlib.h>
  270. #include <limits.h>
  271. #include <ctype.h>
  272. #include <wctype.h>
  273.  
  274. /* Defines to make the file dual use for either strftime() or wcsftime().
  275.  * To get wcsftime, define MAKE_WCSFTIME.
  276.  * To get strftime, do not define MAKE_WCSFTIME.
  277.  * Names are kept friendly to strftime() usage.  The biggest ugliness is the
  278.  * use of the CQ() macro to make either regular character constants and
  279.  * string literals or wide-character constants and wide-character-string
  280.  * literals, as appropriate.  */
  281. #if !defined(MAKE_WCSFTIME)
  282. #  define CHAR      char        /* string type basis */
  283. #  define CQ(a)     a       /* character constant qualifier */
  284. #  define SFLG              /* %s flag (null for normal char) */
  285. #  define _ctloc(x) (ctloclen = strlen (ctloc = _CurrentTimeLocale->x), ctloc)
  286. #  define TOLOWER(c)    tolower((int)(unsigned char)(c))
  287. #  define STRTOUL(c,p,b) strtoul((c),(p),(b))
  288. #  define STRCPY(a,b)   strcpy((a),(b))
  289. #  define STRCHR(a,b)   strchr((a),(b))
  290. #  define STRLEN(a) strlen(a)
  291. # else
  292. #  define strftime  wcsftime    /* Alternate function name */
  293. #  define CHAR      wchar_t     /* string type basis */
  294. #  define CQ(a)     L##a        /* character constant qualifier */
  295. #  define snprintf  swprintf    /* wide-char equivalent function name */
  296. #  define strncmp   wcsncmp     /* wide-char equivalent function name */
  297. #  define TOLOWER(c)    towlower((wint_t)(c))
  298. #  define STRTOUL(c,p,b) wcstoul((c),(p),(b))
  299. #  define STRCPY(a,b)   wcscpy((a),(b))
  300. #  define STRCHR(a,b)   wcschr((a),(b))
  301. #  define STRLEN(a) wcslen(a)
  302. #  define SFLG      "l"     /* %s flag (l for wide char) */
  303. #  ifdef __HAVE_LOCALE_INFO_EXTENDED__
  304. #   define _ctloc(x) (ctloclen = wcslen (ctloc = _CurrentTimeLocale->w##x), \
  305.               ctloc)
  306. #  else
  307. #   define CTLOCBUFLEN   256        /* Arbitrary big buffer size */
  308.     const wchar_t *
  309.     __ctloc (wchar_t *buf, const char *elem, size_t *len_ret)
  310.     {
  311.       buf[CTLOCBUFLEN - 1] = L'\0';
  312.       *len_ret = mbstowcs (buf, elem, CTLOCBUFLEN - 1);
  313.       if (*len_ret == (size_t) -1 )
  314.     *len_ret = 0;
  315.       return buf;
  316.     }
  317. #   define _ctloc(x) (ctloc = __ctloc (ctlocbuf, _CurrentTimeLocale->x, \
  318.               &ctloclen))
  319. #  endif
  320. #endif  /* MAKE_WCSFTIME */
  321.  
  322.  
  323. size_t _DEFUN (strftime, (s, maxsize, format, tim_p),
  324.         CHAR *s _AND
  325.         size_t maxsize _AND
  326.         _CONST CHAR *format _AND
  327.         _CONST struct tm *tim_p)
  328. {
  329.  
  330.   return 0;
  331. }
  332.  
  333.  
  334.