C++ equivalent for strftime()?

Discussion in 'C++' started by Marcus Kwok, Sep 26, 2005.

  1. Marcus Kwok

    Marcus Kwok Guest

    I have a simple LogFile class that I use to log messages, and each
    message has the current date and time prepended to the message.
    Currently, I am using strftime() to format the date, and placing the
    result into a character array. Of course, there is now the issue of
    whether the length of the string is longer than the character array.

    Is there an equivalent way to do this so that it is more C++-like? For
    example, something that will place the result into a std::string or
    something, so I don't have to worry about if timeStringLength will be
    large enough.


    void LogFile::logEntry(const char* message)
    {
    // Output date and time in ISO 8601:2004 format, i.e., YYYY-MM-DD HH:MM:SS
    const char* timeStringFormat = "%Y-%m-%d %H:%M:%S";
    const int timeStringLength = 20;
    char timeString[timeStringLength];

    // get current date and time
    time_t t = time(0);
    tm *curTime = localtime(&t);

    strftime(timeString, timeStringLength, timeStringFormat, curTime);
    logFile << timeString << '\t' << message << '\n';
    flush(logFile);
    }

    --
    Marcus Kwok
     
    Marcus Kwok, Sep 26, 2005
    #1
    1. Advertising

  2. On Mon, 26 Sep 2005 19:08:21 +0000 (UTC)
    (Marcus Kwok) wrote:

    > Is there an equivalent way to do this so that it is more C++-like?


    Is there any good reason that prevents you from creating the char array
    dynamically with new or simply casting strftimes's first argument to
    string?

    best regards / Gruß
    Moritz Beller
    --
    web http://www.4momo.de
    mail momo dot beller at t-online dot de
    gpg http://gpg.notlong.com
     
    Moritz Beller, Sep 26, 2005
    #2
    1. Advertising

  3. Marcus Kwok

    Marcus Kwok Guest

    Moritz Beller <> wrote:
    > Is there any good reason that prevents you from creating the char array
    > dynamically with new or simply casting strftimes's first argument to
    > string?


    Well, the problem with dynamically creating the char array is that the
    appropriate size still cannot be determined before the call. I mean, 20
    characters is enough to hold the format I am using, but to me it still
    looks "ugly" and I was trying to find out if there is a way to do it
    "more C++" and "less C".

    I don't think I can cast strftime()'s first argument to string because
    strftime() expects a pointer to a character array as its first argument.

    --
    Marcus Kwok
     
    Marcus Kwok, Sep 26, 2005
    #3
  4. Marcus Kwok

    Jeff Flinn Guest

    "Marcus Kwok" <> wrote in message
    news:dh9gv5$155$...
    >I have a simple LogFile class that I use to log messages, and each
    > message has the current date and time prepended to the message.
    > Currently, I am using strftime() to format the date, and placing the
    > result into a character array. Of course, there is now the issue of
    > whether the length of the string is longer than the character array.
    >
    > Is there an equivalent way to do this so that it is more C++-like? For
    > example, something that will place the result into a std::string or
    > something, so I don't have to worry about if timeStringLength will be
    > large enough.


    See the boost date_time library at
    http://www.boost.org/doc/html/date_time.html

    Jeff
     
    Jeff Flinn, Sep 26, 2005
    #4
  5. Marcus Kwok

    Marcus Kwok Guest

    Jeff Flinn <> wrote:
    > See the boost date_time library at
    > http://www.boost.org/doc/html/date_time.html


    Looks pretty nice. If/when it gets adopted into the new C++ standard I
    will use it, but I don't think I will be able to convince my colleague
    (whom I am working with on this project) to install a third-party
    library for this, considering that it's a minor implementation detail,
    and the other problems he's been having with his code. I was hoping for
    something that's already in the standard C++ implementation, but if not
    then strftime() will have to do in the meantime.

    The Boost libraries look pretty awesome, and seem to fill a lot of holes
    in the standard libraries that have emerged now that C++ has been used
    for so long and people realize what they're missing.

    Thanks.

    --
    Marcus Kwok
     
    Marcus Kwok, Sep 26, 2005
    #5
  6. Marcus Kwok wrote:
    [snip]
    > I was hoping for
    > something that's already in the standard C++ implementation, but if not
    > then strftime() will have to do in the meantime.
    >

    [snip]

    The ANSI C lib IS part of standard C++. So, strftime() is
    part of standard C++ - because it's part of the ANSI C lib.

    Larry
     
    Larry I Smith, Sep 27, 2005
    #6
  7. Marcus Kwok

    Marcus Kwok Guest

    Larry I Smith <> wrote:
    > The ANSI C lib IS part of standard C++. So, strftime() is
    > part of standard C++ - because it's part of the ANSI C lib.


    Right, but I was talking about a standard library function that would
    duplicate strftime()'s functionality but that would utilize a
    std::string instead of a character buffer (array). Sorry for the
    misunderstanding.

    --
    Marcus Kwok
     
    Marcus Kwok, Sep 27, 2005
    #7
    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. Bryan O'Malley

    strftime not working as expected

    Bryan O'Malley, Feb 3, 2004, in forum: C++
    Replies:
    8
    Views:
    2,652
    Jerry Coffin
    Feb 17, 2004
  2. Bimal
    Replies:
    1
    Views:
    486
    Tim Prince
    Aug 20, 2003
  3. Richard Shea

    strftime - %a is always Monday ?

    Richard Shea, Dec 29, 2003, in forum: Python
    Replies:
    5
    Views:
    1,082
    Richard Shea
    Dec 30, 2003
  4. Allen Unueco

    time.strftime Timezone issue

    Allen Unueco, Jun 13, 2004, in forum: Python
    Replies:
    1
    Views:
    366
    Allen Unueco
    Jun 13, 2004
  5. Tim Peters

    RE: time.strftime Timezone issue

    Tim Peters, Jun 13, 2004, in forum: Python
    Replies:
    1
    Views:
    777
    Allen Unueco
    Jun 13, 2004
Loading...

Share This Page