strftime not working as expected

Discussion in 'C++' started by Bryan O'Malley, Feb 3, 2004.

  1. Forgive me if this is an obvious question, but I've done hours of
    searching, and I can't figure this out.

    VC++ 6 / Win32 platforms

    I'm trying to use strftime with the %x option to give me a date in the
    format specified in the users Control Panel. Though I live in the US,
    for example, I may have told windows that I want to see dates as
    dd/mm/yyyy instead of mm/dd/yyyy.

    It seems that no matter what I change -- local, format, etc., I get
    mm/dd/yy.

    This is the code:

    #include "stdafx.h"
    #include <time.h>
    #include <ctime>


    int main(int argc, char* argv[])
    {
    time_t oTime;
    tm* oDate;
    char sThis[25];

    time(&oTime);
    oDate = localtime(&oTime);

    strftime(sThis, 25, "%x", oDate);

    printf(sThis);
    return 0;
    }


    Any ideas?

    Bryan
     
    Bryan O'Malley, Feb 3, 2004
    #1
    1. Advertising

  2. "Bryan O'Malley" wrote
    > Forgive me if this is an obvious question, but I've done hours of
    > searching, and I can't figure this out.
    >
    > VC++ 6 / Win32 platforms
    >
    > I'm trying to use strftime with the %x option to give me a date in the
    > format specified in the users Control Panel. Though I live in the US,
    > for example, I may have told windows that I want to see dates as
    > dd/mm/yyyy instead of mm/dd/yyyy.
    >
    > It seems that no matter what I change -- local, format, etc., I get
    > mm/dd/yy.


    'strftime()' works with the locale set by 'setlocale()'. It knows nothing about
    the preferences you set in the control panel. If you're writing Windows-specific
    code using Windows-specific functionality (which you are), you should use
    Windows API functions (which, unfortunately for you, are not a topic for this
    newsgroup) and ideally, wrap them so that you can eventually port your code.

    Claudio
     
    Claudio Puviani, Feb 3, 2004
    #2
    1. Advertising

  3. Bryan O'Malley

    Evan Carew Guest

    Bryan,

    Well, I'm not sure if this is what you need, but the people writing the
    Boost library have nice convienient time & date manipulators for C++.
    They are date_time ant posix_time respectively and can be located at
    http://www.boost.org/libs/libraries.htm

    Hope this helps,
    Evan Carew

    Bryan O'Malley wrote:
    > Forgive me if this is an obvious question, but I've done hours of
    > searching, and I can't figure this out.
    >
    > VC++ 6 / Win32 platforms
    >
    > I'm trying to use strftime with the %x option to give me a date in the
    > format specified in the users Control Panel. Though I live in the US,
    > for example, I may have told windows that I want to see dates as
    > dd/mm/yyyy instead of mm/dd/yyyy.
    >

    [snip]
    >
    >
    > Any ideas?
    >
    > Bryan
     
    Evan Carew, Feb 3, 2004
    #3
  4. On Tue, 03 Feb 2004 23:12:45 +0000, Claudio Puviani wrote:

    > "Bryan O'Malley" wrote
    >> Forgive me if this is an obvious question, but I've done hours of
    >> searching, and I can't figure this out.
    >>
    >> VC++ 6 / Win32 platforms
    >>
    >> I'm trying to use strftime with the %x option to give me a date in the
    >> format specified in the users Control Panel. Though I live in the US,
    >> for example, I may have told windows that I want to see dates as
    >> dd/mm/yyyy instead of mm/dd/yyyy.
    >>
    >> It seems that no matter what I change -- local, format, etc., I get
    >> mm/dd/yy.

    >
    > 'strftime()' works with the locale set by 'setlocale()'. It knows
    > nothing about the preferences you set in the control panel. If you're
    > writing Windows-specific code using Windows-specific functionality
    > (which you are), you should use Windows API functions (which,
    > unfortunately for you, are not a topic for this newsgroup) and ideally,
    > wrap them so that you can eventually port your code.


    To the OP:

    IIRC, Windows programs defaults to the "C" locale, but setting the locale
    to what the user specified is trivial. After that all C/C++ functions
    should work with the new locale, the only Windows specific part is to set
    the locale at program startup (and iirc, even that can be done with
    standard only functions). Ask in a Windows group for more details, the
    come back here if you still have problems after you have set the locale
    correctly.

    You may want to test first by settign a locale explicitly for testing
    only. Also, look up the C++ locale support (Josuttis has a nice
    explanation), it is different and much better than C locale support.

    HTH,
    M4
     
    Martijn Lievaart, Feb 4, 2004
    #4
  5. Hello Bryan,

    > It seems that no matter what I change -- local, format, etc., I get
    > mm/dd/yy.


    The following code might be useful.

    Tilman

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    #include <ctime>
    #include <locale>
    #include <string>
    #include <iostream>

    void PutDate(std::eek:stream& os)
    {
    using namespace std;

    time_t t = time(0);
    tm* pTM = localtime(&t);

    string fmt = "%x";
    const char* pBeg = fmt.c_str();
    const char* pEnd = pBeg + fmt.length();

    #if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6.0 needs this...
    const time_put<char>& tp = _USE(os.getloc(), time_put<char>);
    tp.put(os, os, pTM, pBeg, pEnd);
    #else
    const time_put<char>& tp = use_facet<time_put<char> >(os.getloc());
    tp.put(os, os, os.fill(), pTM, pBeg, pEnd);
    #endif
    }

    int main()
    {
    using namespace std;
    PutDate(cout); // 02/04/04
    cout << "\n";

    locale loc("German");
    cout.imbue(loc);
    PutDate(cout); // 04.02.2004
    cout << "\n";

    return 0;
    }
     
    Tilman Kuepper, Feb 4, 2004
    #5
  6. Martijn Lievaart wrote...
    > To the OP:
    >
    > IIRC, Windows programs defaults to the "C" locale, but setting the locale
    > to what the user specified is trivial. After that all C/C++ functions
    > should work with the new locale, the only Windows specific part is to set
    > the locale at program startup (and iirc, even that can be done with
    > standard only functions). Ask in a Windows group for more details, the
    > come back here if you still have problems after you have set the locale
    > correctly.
    >
    > You may want to test first by settign a locale explicitly for testing
    > only. Also, look up the C++ locale support (Josuttis has a nice
    > explanation), it is different and much better than C locale support.
    >
    > HTH,
    > M4


    Martijn -- thanks for the tips. That helps clear things up a bit. Now I
    just need to find out how to get at what Windows says the locale should be.

    Bryan
     
    Bryan O'Malley, Feb 4, 2004
    #6
  7. Bryan O'Malley

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > IIRC, Windows programs defaults to the "C" locale, but setting the locale
    > to what the user specified is trivial.


    The standard requires that programs (on all platforms) start up with the
    "C" locale as the global default.

    > After that all C/C++ functions
    > should work with the new locale, the only Windows specific part is to set
    > the locale at program startup (and iirc, even that can be done with
    > standard only functions).


    As long as you're trying to get the behavior set by the "native
    environment", you can do it portably -- this corresponds to a locale
    with an empty string as its name. E.g.:

    std::setlocale(LC_ALL, "");

    sets the global locale to work as the user has set the environment.

    It won't affect strftime, but you can use an empty string to construct
    an std::locale as well.

    I go so far as to say that an empty string should normally be your first
    choice of locale.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Feb 12, 2004
    #7
  8. On Thu, 12 Feb 2004 12:18:35 -0700, Jerry Coffin wrote:

    > In article <>,
    > says...
    >
    > [ ... ]
    >
    >> IIRC, Windows programs defaults to the "C" locale, but setting the locale
    >> to what the user specified is trivial.

    >
    > The standard requires that programs (on all platforms) start up with the
    > "C" locale as the global default.


    I saw that later. Thanks for the corection.

    >> After that all C/C++ functions
    >> should work with the new locale, the only Windows specific part is to set
    >> the locale at program startup (and iirc, even that can be done with
    >> standard only functions).

    >
    > As long as you're trying to get the behavior set by the "native
    > environment", you can do it portably -- this corresponds to a locale
    > with an empty string as its name. E.g.:
    >
    > std::setlocale(LC_ALL, "");


    That was the trick! Thanks.

    M4
     
    Martijn Lievaart, Feb 15, 2004
    #8
  9. Bryan O'Malley

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > >> IIRC, Windows programs defaults to the "C" locale, but setting the locale
    > >> to what the user specified is trivial.

    > >
    > > The standard requires that programs (on all platforms) start up with the
    > > "C" locale as the global default.

    >
    > I saw that later. Thanks for the corection.


    I'd call it more of an amplification than a correction -- what you said
    was perfectly correct; it just didn't tell the whole story.

    [ ... ]

    > > std::setlocale(LC_ALL, "");

    >
    > That was the trick! Thanks.


    Glad to help.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Feb 17, 2004
    #9
    1. Advertising

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. Bimal
    Replies:
    1
    Views:
    479
    Tim Prince
    Aug 20, 2003
  2. Sheila King
    Replies:
    5
    Views:
    979
    Moderator, k12.ed.math
    Apr 22, 2005
  3. richard
    Replies:
    2
    Views:
    472
    richard
    Jan 13, 2006
  4. Alexandr N Zamaraev

    strftime() argument 1 must be str, not unicode

    Alexandr N Zamaraev, May 7, 2008, in forum: Python
    Replies:
    0
    Views:
    341
    Alexandr N Zamaraev
    May 7, 2008
  5. Andrii V. Mishkovskyi

    Re: strftime() argument 1 must be str, not unicode

    Andrii V. Mishkovskyi, May 7, 2008, in forum: Python
    Replies:
    3
    Views:
    412
    Andrii V. Mishkovskyi
    May 8, 2008
Loading...

Share This Page