Thread: Date in mm-dd or similar

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    45

    Date in mm-dd or similar

    I'm trying to have a string that is the date, preferably in mm-dd format, though something like dd-mm-yyyy is equally suitable. I found out how to do it in C, and I found examples in C++ that were a MyFunction.print function recall, and I couldn't make them into a string.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I'm trying to have a string that is the date, preferably in mm-dd format, though something like dd-mm-yyyy is equally suitable. I found out how to do it in C, and I found examples in C++ that were a MyFunction.print function recall, and I couldn't make them into a string.

    Why don't you just use a C function like ctime and assign it to an std::string?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    Good idea.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
    time_t now;
    struct tm *d;
    char li[13];
    
    time(&now);
    d = localtime(&now);
    
    strftime(li, 15, "%d-%m-%Y", d);
    
    printf("%s\n", li);
    
    return 0;
    }
    There's a program in C that does what I want. However, I don't know what to do, other than changing the #includes and printf and soforth.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That's about all that needs to be done. Using std::string is not that different.

    string li = "mm-dd-yyyy"; // filler string

    strftime( li.data(), li.size(), "%d-%m-%Y", d);

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    Code:
    #include <iostream>
    #include <ctime>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    time_t now;
    struct tm *d;
    string li = "mm-dd-yyyy"; // filler string
    
    time(&now);
    d = localtime(&now);
    
    strftime( li.data(), li.size(), "%d-%m-%Y", d);
    
    cout << s, li;
    
    }
    ./ctime_conv.cpp: In function ‘int main()’:
    ./ctime_conv.cpp:16: error: invalid conversion from ‘const char*’ to ‘char*’
    ./ctime_conv.cpp:16: error: initializing argument 1 of ‘size_t strftime(char*, size_t, const char*, const tm*)’
    ./ctime_conv.cpp:18: error: ‘s’ was not declared in this scope
    ./ctime_conv.cpp:18: error: expected `;' before ‘li’

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> ./ctime_conv.cpp:16: error: invalid conversion from ‘const char*’ to ‘char*’

    Cast the return value of the data( ) function to char*.

    >> cout << s, li;

    Typo?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    Get rid of li or s?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Get rid of li or s?

    Where is 's' even declared? And what operators does ostream define? Left shift, yes. But what others?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    Ok, to make things simpler, here's the C source.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
    time_t now;
    struct tm *d;
    char li[13];
    
    time(&now);
    d = localtime(&now);
    
    strftime(li, 15, "%d-%m-%Y", d);
    
    printf("%s\n", li);
    
    return 0;
    }
    It outputs 17-07-2009.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Ok, to make things simpler, here's the C source.

    Yes, we've already seen that bit of code. What has that got to do with the code you posted with errors?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    The program I built is in C++, and this is in C. I don't know how to change this to C++.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Muscovy View Post
    The program I built is in C++, and this is in C. I don't know how to change this to C++.
    Well you got pretty far on your own, just read your errors.

    Quote Originally Posted by Muscovy View Post
    ./ctime_conv.cpp: In function ‘int main()’:
    ./ctime_conv.cpp:16: error: invalid conversion from ‘const char*’ to ‘char*’
    ./ctime_conv.cpp:16: error: initializing argument 1 of ‘size_t strftime(char*, size_t, const char*, const tm*)’
    ./ctime_conv.cpp:18: error: ‘s’ was not declared in this scope
    ./ctime_conv.cpp:18: error: expected `;' before ‘li’
    The error on line 16 is from the data( ) function when I showed you how to call strftime. You need to cast the return result to a char * type so the function can write the new date string in li. Sorry I didn't do that earlier.

    The error on line 18 has to do with the cout statement you wrote. "s was not declared" should be clear to a C or C++ person, but I don't see an s variable in your code. Printing li should work if you delete ", s" in your code.

    Then you recompile, work through your errors like this again, and so forth until you succeed.
    Last edited by whiteflags; 07-18-2009 at 04:26 PM.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    YES! It works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a file into a block
    By mickey0 in forum C++ Programming
    Replies: 19
    Last Post: 05-03-2008, 05:53 AM
  2. Convert dms to dd
    By JacquesLeJock in forum C Programming
    Replies: 13
    Last Post: 06-06-2007, 01:44 PM
  3. Back To The Basics: Freeing An Array on the Heap?
    By Deo in forum C++ Programming
    Replies: 12
    Last Post: 04-07-2007, 04:42 AM
  4. don't know where to start
    By spikerotc04 in forum C Programming
    Replies: 5
    Last Post: 04-26-2005, 04:21 PM