SOL9 2.0 Class: Date

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * Date.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
#pragma once
#include <sol\Object.h>
#include <time.h>

#include <sol\String.h>

namespace SOL {

class Date :public Object {
private:
    int            timeZone;
    String        date;
    SYSTEMTIME    systemTime;

public:

    static const int LOCALTIME = 0;
    static const int GMT       = 1;

private:
    void toString(SYSTEMTIME& sysTime)
    {
        TCHAR* month[] = {
        _T("Jan"),    // 0
        _T("Feb"),    // 1
        _T("Mar"),    // 2
        _T("Apr"),    // 3
        _T("May"),    // 4
        _T("Jun"),    // 5
        _T("Jul"),    // 6
        _T("Aug"),    // 7
        _T("Sep"),    // 8
        _T("Oct"),    // 9
        _T("Nov"),    //10
        _T("Dec"),    //11
        };
    
        TCHAR* week[] = {
        _T("Sun"),    // 0
        _T("Mon"),    // 1
        _T("Tue"),    // 2
        _T("Wed"),    // 3
        _T("Thu"),    // 4
        _T("Fri"),    // 5
        _T("Sat"),    // 6
        };

        this->systemTime = sysTime;

        TCHAR buffer[80];
        if (timeZone == LOCALTIME) {
            _stprintf_s(buffer, SizeOf(buffer), _T("%s, %02d %s %4d %02d:%02d:%02d"),
                week[sysTime.wDayOfWeek],
                sysTime.wDay,
                month[sysTime.wMonth-1],
                sysTime.wYear,
                sysTime.wHour,
                sysTime.wMinute,
                sysTime.wSecond);
        } else {
            _stprintf_s(buffer, SizeOf(buffer), _T("%s, %02d %s %4d %02d:%02d:%02d GMT"),
                week[sysTime.wDayOfWeek],
                sysTime.wDay,
                month[sysTime.wMonth-1],
                sysTime.wYear,
                sysTime.wHour,
                sysTime.wMinute,
                sysTime.wSecond);
        }
        date = buffer;
    }

public:
    Date(int zone=LOCALTIME)
    {
        timeZone = zone;
        SYSTEMTIME sysTime;

        if (timeZone == LOCALTIME) {
            GetLocalTime(&sysTime);
        } else {
            GetSystemTime(&sysTime);
        }
        toString(sysTime);
    }


    Date(SYSTEMTIME& sysTime, int zone=LOCALTIME)
    {
        timeZone = zone;
        toString(sysTime);
    }

public:
    Date(FILETIME& ft, int zone=LOCALTIME)
    {
        timeZone = zone;
        SYSTEMTIME sysTime;
        FILETIME lt;
        if (timeZone == LOCALTIME) {
            FileTimeToLocalFileTime(&ft, &lt);
            FileTimeToSystemTime(&lt, &sysTime);
        } else {
            FileTimeToSystemTime(&ft, &sysTime);
        }
        toString(sysTime);
    }

public:
    const TCHAR* getDate() const { 
        return (const TCHAR*)date; 
    }

public:
    bool getDate(__out StringT<char>& string) const {
        bool rc = false;
        if (date.getLength()>0) {
#ifdef UNICODE
    StringConverter converter;
    converter.convert((const wchar_t*)date, string);
#else
    string = (const char*)date;
#endif
            rc = true;
        }
        return rc;
    }

public:
    SYSTEMTIME&    getSystemTime() { 
        return systemTime; 
    }

public:
    operator const TCHAR*() { 
        return (const TCHAR*)date; 
    }

};

}


Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.