datetime.cpp

来自「在手机操作系统symbina上使用的一个脚本扩展语言的代码实现,可以参考用于自己」· C++ 代码 · 共 654 行 · 第 1/2 页

CPP
654
字号
// DATETIME.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <OPLAPI.H>
#include <F32FILE.H>
#include <OPLDB.H>
#include "datetime.h"

CDateTimeOpx::CDateTimeOpx() : iDTArray(4)
    {
    }


CDateTimeOpx::~CDateTimeOpx() 
    {
    iDTArray.ResetAndDestroy();
    }


void CDateTimeOpx::DTNewDateTime(OplAPI& aOplAPI)
    {
    TInt micro=aOplAPI.PopInt32();
	TInt second=aOplAPI.PopInt32();
	TInt minute=aOplAPI.PopInt32();
	TInt hour=aOplAPI.PopInt32();
   	TInt day=aOplAPI.PopInt32();
	TInt month=aOplAPI.PopInt32();
	TInt year=aOplAPI.PopInt32();

    RangeCheckL(micro,0,999999);
    RangeCheckL(second,0,59);
    RangeCheckL(minute,0,59);
    RangeCheckL(hour,0,23);
    RangeCheckL(month,1,12);
    RangeCheckL(day,1,Time::DaysInMonth(year,TMonth(month-1)));
	
    TDateTime* dtptr = new(ELeave) TDateTime(year,TMonth(month-1), day-1, hour, minute, second, micro);
    TRAPD(err,iDTArray.AppendL(dtptr));
    if (err)
        {
        delete dtptr;
        User::Leave(err);
        }
    aOplAPI.Push(TInt32(dtptr));
    }


void CDateTimeOpx::DTDeleteDateTime(OplAPI& aOplAPI)
    {
    TDateTime* dtptr=(TDateTime *)aOplAPI.PopInt32();
    TInt pos=CheckPointerL(dtptr);
    delete dtptr;
    iDTArray.Delete(pos);
    aOplAPI.Push(TReal64(0.0));

    }


TInt CDateTimeOpx::CheckPointerL(TDateTime* adtptr)
    {
    TInt count=iDTArray.Count();
    TInt ii;
    for(ii=0;ii<count;ii++)
        {
        if (iDTArray[ii]==adtptr)
            return ii;
        }
    User::Leave(KOplErrInvalidArgs);
    return ii;    
    }


void CDateTimeOpx::RangeCheckL(TInt aVal, TInt aMin, TInt aMax)
    {
    if(aVal<aMin || aVal>aMax)
        User::Leave(KOplErrOutOfRange);
    }


void CDateTimeOpx::DTYear(OplAPI& aOplAPI)      // Check pointer
    {	
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    aOplAPI.Push(TInt32(pDate->Year()));
    }


void CDateTimeOpx::DTMonth(OplAPI& aOplAPI)
    {
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    aOplAPI.Push(TInt32(pDate->Month()+1));
    }


void CDateTimeOpx::DTDay(OplAPI& aOplAPI)
    {
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    aOplAPI.Push(TInt32(pDate->Day()+1));
    }


void CDateTimeOpx::DTHour(OplAPI& aOplAPI)
    {
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    aOplAPI.Push(TInt32(pDate->Hour()));
    }


void CDateTimeOpx::DTMinute(OplAPI& aOplAPI)
    {
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    aOplAPI.Push(TInt32(pDate->Minute()));
    }


void CDateTimeOpx::DTSecond(OplAPI& aOplAPI)
    {
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    aOplAPI.Push(TInt32(pDate->Second()));
    }


void CDateTimeOpx::DTMicro(OplAPI& aOplAPI)
    {
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    aOplAPI.Push(TInt32(pDate->MicroSecond()));
    }


void CDateTimeOpx::DTSetYearL(OplAPI& aOplAPI)      // returns error if not possible
    {
    TInt year=aOplAPI.PopInt32();    
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    pDate->SetYear(year);
    aOplAPI.Push(TReal64(0.0));
    }


void CDateTimeOpx::DTSetMonthL(OplAPI& aOplAPI)       // returns error if not possible
    {
    TInt month=aOplAPI.PopInt32();
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    RangeCheckL(month,1,12);
    pDate->SetMonth(TMonth(month-1));
    aOplAPI.Push(TReal64(0.0));
    }


void CDateTimeOpx::DTSetDayL(OplAPI& aOplAPI)       // returns error if not possible
    {
    TInt day=aOplAPI.PopInt32();
	TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    TInt year=pDate->Year();
    TMonth month=pDate->Month();
    RangeCheckL(day,1,Time::DaysInMonth(year,month));
    pDate->SetDay(day-1);
    aOplAPI.Push(TReal64(0.0));
    }


void CDateTimeOpx::DTSetHourL(OplAPI& aOplAPI)       // returns error if not possible
    {
    TInt hour=aOplAPI.PopInt32();    
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    RangeCheckL(hour,0,23);
    pDate->SetHour(hour);
    aOplAPI.Push(TReal64(0.0));
    }


void CDateTimeOpx::DTSetMinuteL(OplAPI& aOplAPI)       // returns error if not possible
    {
    TInt minute=aOplAPI.PopInt32();
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    RangeCheckL(minute,0,59);
    pDate->SetMinute(minute);
    aOplAPI.Push(TReal64(0.0));
    }


void CDateTimeOpx::DTSetSecondL(OplAPI& aOplAPI)       // returns error if not possible
    {
    TInt second=aOplAPI.PopInt32();
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    RangeCheckL(second,0,59);
    pDate->SetSecond(second);
    aOplAPI.Push(TReal64(0.0));
    }


void CDateTimeOpx::DTSetMicroL(OplAPI& aOplAPI)       // returns error if not possible
    {
    TInt micro=aOplAPI.PopInt32();
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    RangeCheckL(micro,0,999999);
    pDate->SetMicroSecond(micro);
    aOplAPI.Push(TReal64(0.0));
    }


void CDateTimeOpx::DTSetHomeTime(OplAPI& aOplAPI)       // returns error if not possible
    {
    TDateTime* pDate=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(pDate);
    TTime set(*pDate);
    User::SetHomeTime(set);
    aOplAPI.Push(TReal64(0.0));
    }


void CDateTimeOpx::DTNow(OplAPI& aOplAPI)
    {
    TDateTime* dtptr = new(ELeave) TDateTime();

    TTime now ;
    now.HomeTime();
    *dtptr=now.DateTime();
    TRAPD(err,iDTArray.AppendL(dtptr));
    if (err)
        {
        delete dtptr;
        User::Leave(err);
        }    
    aOplAPI.Push(TInt32(dtptr));
    }


void CDateTimeOpx::DTDateTimeDiff(OplAPI& aOplAPI)
    {
    TInt32* micro=aOplAPI.PopPtrInt32();
	TInt32* second=aOplAPI.PopPtrInt32();
	TInt32* minute=aOplAPI.PopPtrInt32();
	TInt32* hour=aOplAPI.PopPtrInt32();
   	TInt32* day=aOplAPI.PopPtrInt32();
	TInt32* month=aOplAPI.PopPtrInt32();
	TInt32* year=aOplAPI.PopPtrInt32();
    TDateTime* aEnd=(TDateTime*)aOplAPI.PopInt32();
    TDateTime* TheStart=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(aEnd);
    CheckPointerL(TheStart);

    TDateTime toReturn;     
    TDateTime aStart(*TheStart);
    TTime start(aStart);
    TTime end(*aEnd);
    TTimeIntervalYears years(start.YearsFrom(end));
    aStart.SetYear(aStart.Year()+ years.Int());
    start=aStart;
    TTimeIntervalMonths months(start.MonthsFrom(end));
    aStart.SetMonth(TMonth(aStart.Month()+months.Int()));
    start=aStart;
    TTimeIntervalDays days(start.DaysFrom(end));
    aStart.SetDay(aStart.Day()+days.Int());
    start=aStart;
    TTime diff=(end.Int64()-start.Int64());
    toReturn=diff.DateTime();
    aOplAPI.PutLong(year,years.Int());
    aOplAPI.PutLong(month,months.Int());
    aOplAPI.PutLong(day,days.Int());
    aOplAPI.PutLong(hour,toReturn.Hour());
    aOplAPI.PutLong(minute,toReturn.Minute());
    aOplAPI.PutLong(second,toReturn.Second());
    aOplAPI.PutLong(micro,toReturn.MicroSecond());
    aOplAPI.Push(TReal64(0.0));
    }


void CDateTimeOpx::DTYearsDiff(OplAPI& aOplAPI)
    {                                                           // Check ptr s
    TDateTime* endPtr=(TDateTime*)aOplAPI.PopInt32();
    TDateTime* startPtr=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(endPtr);
    CheckPointerL(startPtr);
    TTime theStart(*startPtr);
    TTime end(*endPtr);
    TTimeIntervalYears years(end.YearsFrom(theStart));
    aOplAPI.Push(TInt32(years.Int()));
    }


void CDateTimeOpx::DTMonthsDiff(OplAPI& aOplAPI)
    {                                                           // Check ptr s
    TDateTime* endPtr=(TDateTime*)aOplAPI.PopInt32();
    TDateTime* startPtr=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(endPtr);
    CheckPointerL(startPtr);
    TTime theStart(*startPtr);
    TTime end(*endPtr);
    TTimeIntervalMonths months(end.MonthsFrom(theStart));
    aOplAPI.Push(TInt32(months.Int()));
    }


void CDateTimeOpx::DTDaysDiff(OplAPI& aOplAPI)
    {                                                           // Check ptr s
    TDateTime* endPtr=(TDateTime*)aOplAPI.PopInt32();
    TDateTime* startPtr=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(endPtr);
    CheckPointerL(startPtr);
    TTime theStart(*startPtr);
    TTime end(*endPtr);
    TTimeIntervalDays days(end.DaysFrom(theStart));
    aOplAPI.Push(TInt32(days.Int()));
    }


void CDateTimeOpx::DTHoursDiff(OplAPI& aOplAPI)
    {                                                           // Check ptr s
    TDateTime* endPtr=(TDateTime*)aOplAPI.PopInt32();
    TDateTime* startPtr=(TDateTime*)aOplAPI.PopInt32();
    CheckPointerL(endPtr);
    CheckPointerL(startPtr);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?