[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
General Programming Concepts: Writing and Debugging Programs

Time Formatting Subroutines

In addition to the strftime subroutine defined in the C programming language standard, XPG4 defines the following time formatting subroutines:

wcsftime Formats time into wide character code strings.
strptime Converts a multibyte string into an internal time format.

Examples

  1. The following example uses the wcsftime subroutine to format time into a wide character string:
    #include <stdio.h>
    #include <langinfo.h>
    #include <locale.h>
    #include <time.h>
     
    main()
    {
            wchar_t timebuf[BUFSIZE];
            time_t clock = time( (time_t*) NULL);
            struct tim *tmptr = localetime(&clock);
     
            (void)setlocale(LC_ALL, "");
     
            wcsftime(
                    timebuf,       /* Time string output buffer */
                    BUFSIZ,        /*Maximum size of output string */
                    nl_langinfo(D_T_FMT),      /* Date/time format */
                    tmptr          /* Pointer to tm structure */
            );
     
            printf("%S\n", timebuf);
    }
  2. The following example uses the strptime subroutine to convert a formatted time string to internal format:
    #include <langinfo.h>
    #include <locale.h>
    #include <time.h>
     
    main(int argc, char **argv)
    {
            struct tm tm;
     
            (void)setlocale(LC_ALL, "");
     
            if (argc != 2) {
                    ...                /* Error handling */
            }
            if (strptime(
                    argv[1],           /* Formatted time string */
                    nl_langinfo(D_T_FMT),      /* Date/time format */
                    &tm                /* Address of tm structure */
            ) == NULL) {
                    ...                /* Error handling */
            }
            else {
                    ...               /* Other Processing */
            }
    }

Related Information

National Language Support Subroutines Overview provides information about wide character and multibyte subroutines.

For general information about internationalizing programs, see National Language Support Overview for Programming and Locale Overview for Programming .

The strftime subroutine, strptime subroutine, wcsftime subroutine.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]