Datumformat ändern ?



  • Hallo nochmals!

    Ich komme leider schon wieder nicht mehr weiter 😞

    error C2664: 'strftime': Konvertierung des Parameters 4 von 'std::string' in 'const tm *' nicht möglich

    #include <iostream>
    #include <string>
    #include <time.h>
    #include <algorithm>
    
    using namespace std;
    
    class SomeText {
    public:
        virtual string getText() = 0;
        virtual ~SomeText();
    };
    
    SomeText::~SomeText()
    {
    }
    
    //--------------------------------------------
    // Fixer Text
    
    class FixedText : public SomeText {
        string txt;
    public:
        FixedText(string s);
        virtual string getText();
    };
    
    FixedText::FixedText(string s)
        : txt(s)
    {
    }
    
    string FixedText::getText()
    {
        return txt;
    }
    //--------------------------------------------
    // Dekorator - auf Länge l auffüllen/kürzen
    
    class FilledText : public SomeText {
        SomeText& st;
        int len;
    public:
        FilledText(SomeText& t, int l);
        virtual string getText();
    };
    
    FilledText::FilledText(SomeText& t, int l)
        : st(t), len(l)
    {
    }
    
    string FilledText::getText()
    {
        string s = st.getText();
        int l = s.length();
        if (l >= len)
    	return s.substr(0, len);
        else
    	return s + string(len-l, ' ');
    }
    //--------------------------------------------
    // Dekorator -  um N Stellen verschoben
    
    class ShiftedText : public SomeText {
        SomeText& st;
        unsigned int offset;
    public:
        ShiftedText(SomeText& t, unsigned int o);
        virtual string getText();
    
        void setOffset(unsigned int o);
    };
    
    ShiftedText::ShiftedText(SomeText& t, 
    			 unsigned int o)
        : st(t), offset(o)
    {
    }
    
    string ShiftedText::getText()
    {
        string s = st.getText();
        unsigned int o = offset % s.length();
        return s.substr(offset) +  s.substr(0, o);
    }
    
    void ShiftedText::setOffset(unsigned int o)
    {
        offset = o;
    }
    //--------------------------------------------
    // Text mit aktuellem Datum und Uhrzeit
    
    class DateText : public SomeText {
    public:
        DateText();
        virtual string getText();
    };
    
    DateText::DateText()
    {
    }
    
    string DateText::getText()
    {
        time_t now = time(0);
        string t = ctime(&now);
        // \n am ende entfernen
    
        return t.substr(0, t.length()-1); 
    }
    //--------------------------------------------
    // DekorText-Beginn
    class DekorText:public SomeText
    {
      string links;
      SomeText& mitte;
      string rechts;
    public: 
      DekorText(const char*, SomeText&, const char*);
      virtual string getText();
    };
    
    DekorText::DekorText(const char* l, SomeText& m, const char* r)
    :links(l), mitte(m), rechts(r)
    {
    }
    
    string DekorText::getText()
    {
      return links + mitte.getText() + rechts;
    }
    //--------------------------------------------
    class ReverseText:public SomeText
    {
      SomeText& text;
    public: 
      ReverseText(SomeText&);
      virtual string getText();
    };
    
    ReverseText::ReverseText(SomeText& s)
    :text(s)
    {
    }
    
    string ReverseText::getText()
    {
    	string t = text.getText();
    	reverse(t.begin(), t.end());
    	return t;
    }
    //--------------------------------------------
    // Text mit aktuellem Datum und Uhrzeit
    
    class DateFormat : public SomeText {
    public:
        DateFormat();
        virtual string getText();
    };
    
    DateFormat::DateFormat()
    {
    }
    
    string DateFormat::getText()
    {
    	char buffer[128];
        time_t now = time(0);
        string t = ctime(&now);
    	strftime(buffer,128,"Now is %I:%M%p.",*t);
    	return t.substr(0, t.length()-1); 
    }
    //--------------------------------------------
    
    void show(const char* what, SomeText& st, 
    	  char delimiter = '\n')
    {
    	DekorText dt("[[" , st , "]]");
    	cout << what  << ":" << dt.getText() << delimiter;
    }
    
    void show60(ShiftedText& st)
    {
        for(int i=0; i < 60; i++) {
    	st.setOffset(i);
    	_sleep(1000); // Windows: _sleep(1000);
    	show(" ", st, '\r');
    	cout.flush();
        }
        cout << "\n";
    }
    
    //--------------------------------------------
    int main()
    {
    
        FixedText fix("Hello World");
        show("fix", fix);
    
        FilledText filled(fix, 60);
        show("filled", filled);
    
        ShiftedText shifted(filled, 10);
        show("shifted 10", shifted);
    	ReverseText rt(shifted);
    	show("reverse", rt);
        DateText dt;
    	DekorText ddt("#", dt, "#");
    	DateFormat df;
    	//time_t rawtime;
    	//struct tm * timeinfo;
    	//char buffer [80];
    
    	//time ( &rawtime );
    	//timeinfo = localtime ( &rawtime );
    
    	//strftime (buffer,80,"Now is %I:%M%p.",timeinfo);
    	//puts (buffer);
    
    	FilledText filleddate(ddt, 60);
        ShiftedText shifteddate(filleddate, 0);
        show60(shifteddate);
    	system("pause");
    }
    

    lg


Anmelden zum Antworten