Dates, locales

Discussion in 'C++' started by Micah Cowan, Nov 28, 2006.

  1. Micah Cowan

    Micah Cowan Guest

    I was surprised to discover that the following code emits the formatted
    date string uisng the default locale (C/POSIX), and I am required to
    uncomment the line that imbues the "ja_JP" locale into cout in order to
    get Japanese-language output.

    I am surprised by this, because I thought that the text ought to vary
    on the locale from which I obtained the time_put facet, rather than on
    the locale used by the output stream. Is G++ (v4.03) right, or is it
    something implementation-defined? Does this follow the spirit of the
    Standards intentions? Is there something to quote from the Standard to
    enlighten me, or other resources to help?

    Also, is this method even the preferred way to print a date to an
    output stream?

    -----
    #include <locale>
    #include <iostream>
    #include <ctime>
    #include <utility>

    #define CHAR_ARRAY_STR_END(a) ((a) + (sizeof(a)) - 1)

    using namespace std;

    int main(void)
    {
    const char format[] = "%c\n";
    const char *format_end = CHAR_ARRAY_STR_END(format);
    time_t secs = time(0);
    std::tm *t = localtime(&secs);

    const locale loc("ja_JP.utf8");
    // cout.imbue(loc);
    const time_put<char>& tp = use_facet<time_put<char> >(loc);
    tp.put(cout, cout, ' ', t, format, format_end);
    cout << flush;
    }
    -----
     
    Micah Cowan, Nov 28, 2006
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?YWxhbg==?=

    Datagrid sorting from different locales.

    =?Utf-8?B?YWxhbg==?=, Apr 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    501
    =?Utf-8?B?YWxhbg==?=
    Apr 21, 2005
  2. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,095
    Rob Schieber
    Sep 30, 2005
  3. David List
    Replies:
    0
    Views:
    391
    David List
    Aug 6, 2003
  4. Replies:
    0
    Views:
    366
  5. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    505
    Mark Schupp
    Aug 9, 2004
  6. Replies:
    1
    Views:
    387
    Jano Svitok
    Jul 17, 2007
  7. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    477
    Eric I.
    Jun 8, 2008
  8. Dr J R Stockton

    FAQ Dates; Opera Dates.

    Dr J R Stockton, Jun 18, 2007, in forum: Javascript
    Replies:
    0
    Views:
    317
    Dr J R Stockton
    Jun 18, 2007
Loading...