Example #1
0
//------------------------------------------------------
// Class:       TChineseCalendar
// Function:    ChineseToDateTime
// Arguments:   TDateTime &
//
// Comments:    This function converts the date held within
//				the TChineseCalendar class to a TDateTime format and 
//				places it in the TDateTime class provided.
//
// Return:      void
//------------------------------------------------------
EXPORT_C void TChineseCalendar::ChineseToDateTime(TDateTime &aDT)
	{
	TArithmeticalDate gregDate;
	TGregorianCalendar greg(iJulianDay);

	greg.GregFromJulianDay(gregDate,iJulianDay);

	aDT.SetMicroSecond(0);
	aDT.SetSecond(0);
	aDT.SetMinute(0);
	aDT.SetHour(0);
	aDT.SetMonth((TMonth)(gregDate.iMonth - KCalConvMonthOffsetByOne));
	aDT.SetDay(gregDate.iDay - KCalConvMonthOffsetByOne);
	aDT.SetYear(gregDate.iYear);
	}
// ----------------------------------------------------------------------------
// MPXDbUtil::MonthStart
// ----------------------------------------------------------------------------
//
TTime MPXDbUtil::MonthStart()
    {
    MPX_FUNC("MPXDbUtil::MonthStart");

    TTime now;  // in microseconds
    now.HomeTime();
    TDateTime lastMonthStartDate = now.DateTime();

    lastMonthStartDate.SetDay(0);  // Day range from 0 to 30
    lastMonthStartDate.SetHour(0);
    lastMonthStartDate.SetMinute(0);
    lastMonthStartDate.SetSecond(0);
    lastMonthStartDate.SetMicroSecond(0);

    return TTime(lastMonthStartDate);
    }
Example #3
0
datetime_t TimePackToRel(const datepack_t *tp, bool_t Local)
{
    TDateTime Date;
    TTime ot;
	if (!tp) return INVALID_DATETIME_T;
    Date.SetYear(tp->Year);
    Date.SetMonth((TMonth)(tp->Month-1));
    Date.SetDay(tp->Day-1);
    Date.SetHour(tp->Hour);
    Date.SetMinute(tp->Minute);
    Date.SetSecond(tp->Second);

    ot = TTime(Date);

    if (Local) 
    {
#ifndef SYMBIAN90
        TLocale locale;
        TTimeIntervalSeconds universalTimeOffset(locale.UniversalTimeOffset());
        ot -= universalTimeOffset;
        if (locale.QueryHomeHasDaylightSavingOn())
        {
            TTimeIntervalHours daylightSaving(1);
            ot -= daylightSaving;
        }
#else
        RTz TzServer;
        if (TzServer.Connect()==KErrNone)
        {
            CTzConverter* Converter = CTzConverter::NewL(TzServer); 
            Converter->ConvertToUniversalTime(ot);
            delete Converter;
            TzServer.Close();
        }
#endif
    }

    return SymbianToDateTime(ot);
}
void TAzenqosEngineUtils::ParseTimeStrMilliL(const TDesC& str,TTime &time)
	{
		TInt instrlen = str.Length();
		if(str.Length() != KTimeStampMillisecStrLen )
			User::Leave(KErrBadName);

		TDateTime date;

		TPtrC ptr(0,0);
		TInt pos=0;
		TLex lex;
		TInt val;

		//_LIT(KTimeStampMillisecFormat,"%02d%02d%02d_%02d:%02d:%02d.%03d");

		///year
		ptr.Set(str.Mid(pos,2));
		pos+=2;
		lex.Assign(ptr);
		val=0;
		User::LeaveIfError(lex.Val(val));
		val+=2000;
		User::LeaveIfError(date.SetYear(val));

		//month
		ptr.Set(str.Mid(pos,2));
		pos+=2;
		lex.Assign(ptr);
		val=0;
		User::LeaveIfError(lex.Val(val));
		if(val<1 || val > 12)//month should be at least 1 so we can set as symbian format: month-1
			User::Leave(KErrGeneral);
		val--;//Symbian format EJanuary is 0
		User::LeaveIfError(date.SetMonth((TMonth)val));

		//day
		ptr.Set(str.Mid(pos,2));
		pos+=2;
		lex.Assign(ptr);
		val=0;
		User::LeaveIfError(lex.Val(val));
		if(val<1 || val > 31)//day limit
			User::Leave(KErrGeneral);
		val--;//Symbian format EJanuary is 0
		User::LeaveIfError(date.SetDay(val));


		//skip '_'
		pos+=1;

		//HH
		ptr.Set(str.Mid(pos,2));
		pos+=2;
		lex.Assign(ptr);
		val=0;
		User::LeaveIfError(lex.Val(val));
		if(val<0 || val > 23)//0 to 23 Hrs
			User::Leave(KErrGeneral);
		User::LeaveIfError(date.SetHour(val));

		//skip ':'
		pos+=1;

		//MM
		ptr.Set(str.Mid(pos,2));
		pos+=2;
		lex.Assign(ptr);
		val=0;
		User::LeaveIfError(lex.Val(val));
		if(val<0 || val > 59)//0 to 59 mins
			User::Leave(KErrGeneral);
		User::LeaveIfError(date.SetMinute(val));

		//skip ':'
		pos+=1;

		//SS
		ptr.Set(str.Mid(pos,2));
		pos+=2;
		lex.Assign(ptr);
		val=0;
		User::LeaveIfError(lex.Val(val));
		if(val<0 || val > 59)//0 to 59 Seconds
			User::Leave(KErrGeneral);
		User::LeaveIfError(date.SetSecond(val));

		//skip '.'
		pos+=1;

		//SSS
		ptr.Set(str.Mid(pos,3));
		pos+=3;
		lex.Assign(ptr);
		val=0;
		User::LeaveIfError(lex.Val(val));
		if(val<0 || val > 999999)//0 to 999999 MicroSeconds
			User::Leave(KErrGeneral);
		User::LeaveIfError(date.SetMicroSecond(val));

		time = date;

	}
Example #5
0
void CAzqBtGPSReader::RunL()
{

	iCallbackTimer->Cancel();

	switch(iState)
	{
		case ENoState:
		break;

		case EInquiringDevices:
			{
				if( iStatus !=KErrNone)
					{
						iHostResolver.Close();

						if(iStatus == KErrHostResNoMoreResults)
							{

	    							{
	    							iGPSData.Reset();
									_LIT(KGPSStateUpdate,"BT GPS Search finished...");
									iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );
										}

						    //choose and connect to BT GPS modules
						    if(iBtGPSArray.Count()>0)
						    	{
						    		User::LeaveIfError(iSendingSocket.Open(iSocketServer,RFCOMM));
		    						TBTSockAddr address;
		    						address.SetBTAddr(iBtGPSArray[0]);
		    						// GPS devices usually use port 1 as data channel
		    						// so we don't have to query it
		    						address.SetPort(1);

		    							{
		    							iGPSData.Reset();
		    							iState = EConnecting;
										_LIT(KGPSStateUpdate,"Trying to connect to first device");
										iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );
											}


		    						iSendingSocket.Connect(address, iStatus);
		    						SetActive();
		    						ResetBtTimer();

						    	}
						    else
						    	{

						    		{
						    		iGPSData.Reset();
						    		iState = ENoState;
									_LIT(KGPSStateUpdate,"No BT GPS. Retry press 9");
									iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );
									}


						    	}

							}
						else
							{
								{
								iGPSData.Reset();
								iState = ENoState;
								_LIT(KGPSStateUpdate,"Search failed. Retry press 9");
								iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );
									}



							}
					}
				else
					{

					TInquirySockAddr &addr = TInquirySockAddr::Cast(iNameEntry().iAddr);

					if(addr.MajorClassOfDevice() == KBtGPSMajorClass)
						{
							//add to array...
							iBtGPSArray.AppendL(addr.BTAddr());

								{
								iGPSData.Reset();
							_LIT(KGPSStateUpdate,"Found %d BT GPS, Searching...");
							TBuf<128> buf;
							buf.Format(KGPSStateUpdate, iBtGPSArray.Count());
							iObserver.OnGPSStateUpdate(buf,iGPSData );
								}
						}

					    ///////////get next device
						iState = EInquiringDevices;
						iHostResolver.Next(iNameEntry, iStatus);
						SetActive();
						ResetBtTimer();
						///////////////////////////////////
					}
			}
		break;

		case EConnecting:
			{
				if( iStatus !=KErrNone)
					{
						//remove previous device from array
						if(iBtGPSArray.Count()>0)
							{
							iBtGPSArray.Remove(0);
							}

						//if more gps devices remaining to try
						if(iBtGPSArray.Count()>0)
							{
								//socket alraedy opened...

								TBTSockAddr address;
								address.SetBTAddr(iBtGPSArray[0]);
								// GPS devices usually use port 1 as data channel
								// so we don't have to query it
								address.SetPort(1);

								iState = EConnecting;

									{
									iGPSData.Reset();
									_LIT(KGPSStateUpdate,"Connecting next device");
									iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );
										}

								iSendingSocket.Connect(address, iStatus);
								SetActive();
								ResetBtTimer();
							}
						else//finished list
							{
								{
								iGPSData.Reset();
								iState = ENoState;

								_LIT(KGPSStateUpdate,"Connect GPS failed. Retry press 9");
								iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );
									}

								iSendingSocket.Close();
							}
					}
				else
					{
						iState = EReading;
							{
							_LIT(KGPSStateUpdate,"Reading GPS data...");
							iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );
								}
						iWaitGPSReadyRetry = 0; //only related if iSyncTimeWhenValid is ETrue
						line.Zero();
						iLineHeadReceivedTime = 0;//just to make sure it's fresh
						iSendingSocket.RecvOneOrMore(data, 0, iStatus, iReadDataLength);
						SetActive();
						ResetBtTimer();
					}
			}
		break;

		case EReading:
			{

				if( iStatus !=KErrNone)
					{

						{
						iGPSData.Reset();
						iState = ENoState;

						_LIT(KGPSStateUpdate,"Read GPS failed. Retry press 9");
						iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );
							}


						iSendingSocket.Close();
						return;
					}

				iState = EReadComplete;

				iGPSData.Reset();//prepare for new data

				int rpos = 0;

				while ((rpos = data.Locate('\r')) != KErrNotFound)
				{
					TPtrC8 leftr(0,0);
					leftr.Set(data.Left(rpos));

					TInt leftrlen = leftr.Length();

					if(leftrlen + line.Length() < line.MaxLength())
						{
							if(line.Length()==0)
								iLineHeadReceivedTime.HomeTime();
							line.Append(leftr);
						}
					else
					{
						line.Zero();

						if(leftrlen + line.Length() < line.MaxLength())
							{
								//if(line.Length()==0) sure it is, see above lines
									iLineHeadReceivedTime.HomeTime();
								line.Append(leftr);
							}
					}

					if(rpos+1<data.Length())
						{
							data.Copy(data.Mid(rpos+1));

							if(data[0] == '\n')
								data.Delete(0,1);
						}
					else
						{
							data.Zero();
						}



					TGPSData gpsdata;
					TTimeIntervalMicroSeconds proctime;

					if(iLineHeadReceivedTime == TTime(0))//just in case...
						{
						iLineHeadReceivedTime.HomeTime();
						CAknWarningNote* informationNote = new (ELeave) CAknWarningNote(ETrue);
						//informationNote->SetTimeout(CAknNoteDialog::EShortTimeout);
						_LIT(KConfirmText,"Warning: No read-offset time correction");
						informationNote->ExecuteLD(KConfirmText);
						}

					if(gpsdata.ParseGPSInput(line,iLineHeadReceivedTime,proctime))
					{


						if((gpsdata.POS_STAT.Length() == 0) || (gpsdata.POS_STAT.Length()>0 && gpsdata.POS_STAT[0]!='A'))//if data NOT valid
							{
							///////////////BEGIN parsed but NOT valid data

							if(iSyncTimeWhenValid)//if the NetMonview is in the "Awaiting Fresh GPS Params State for the first time of session - needs time sync"
								{
									if(iWaitGPSReadyRetry>=KMaxWaitGPSReadyRetries)
									{
										//max retries finished, notify as in EReadcomplete state as set by above
										_LIT(KGPSStateUpdate,"GPS Connected but Not Ready");
										iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );
										iSyncTimeWhenValid = EFalse;
									}
									else
									{
									//don't notify as in EReadcomplete state yet...
									iState = EReading;
									iWaitGPSReadyRetry++;
									TBuf<64> buf;
									_LIT(KGPSStateUpdateFormat,"GPS NotReady: TimeSync retry %d/%d");

									buf.Format(KGPSStateUpdateFormat,iWaitGPSReadyRetry,KMaxWaitGPSReadyRetries);

									iObserver.OnGPSStateUpdate(buf,iGPSData );
									}
								}
							else
								{
									_LIT(KGPSStateUpdate,"GPS Connected but Not Ready");
									iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );
								}




									//latdeg = londeg = latmin = lonmin = latsec = lonsec = 0;
									line.Zero();


									if(data.Length()>0)
										{
											if(line.Length() + data.Length() < line.MaxLength())
												{
												iLineHeadReceivedTime.HomeTime();
												line.Append(data);
												}
											else
												{
													line.Zero();
													if(line.Length() + data.Length() < line.MaxLength())
														{
														//line is zero len from line.Zero() above
														iLineHeadReceivedTime.HomeTime();
														line.Append(data);
														}
												}
										}

									data.Zero();

									iState = EReading;
									iSendingSocket.RecvOneOrMore(data, 0, iStatus, iReadDataLength);
									SetActive();
									ResetBtTimer();
									return;
									///////////////END not valid
								}

						////////// boyond here is the VALID case


						////////////set device's UTC time
						if(iSyncTimeWhenValid)
							{

										TTime devTime;
										devTime.HomeTime();
										TDateTime dt = devTime.DateTime();


										if(gpsdata.POS_UTC.Length() < 6 || gpsdata.DATE.Length() < 6)
										{
											//line not ready/full... do nothing
										}
										else
										{

											//All NMEA chars are ASCII

											dt.SetHour((gpsdata.POS_UTC[0]-48)*10+(gpsdata.POS_UTC[1]-48));
											dt.SetMinute((gpsdata.POS_UTC[2]-48)*10+(gpsdata.POS_UTC[3]-48));
											dt.SetSecond((gpsdata.POS_UTC[4]-48)*10+(gpsdata.POS_UTC[5]-48));


											dt.SetDay((gpsdata.DATE[0]-48)*10+(gpsdata.DATE[1]-48) -1);

											dt.SetMonth(TMonth((gpsdata.DATE[2]-48)*10+(gpsdata.DATE[3]-48) -1));

											dt.SetYear((gpsdata.DATE[4]-48)*10+(gpsdata.DATE[5]-48)+KY2K);



											if(gpsdata.POS_UTC.Length()>9 && gpsdata.POS_UTC.Find(_L8("."))>0)
											{
												dt.SetMicroSecond((gpsdata.POS_UTC[7]-48)*KONEHUNDREDTHOUSAND+(gpsdata.POS_UTC[8]-48)*KTENTHOUSAND+(gpsdata.POS_UTC[9]-48)*1000);
											}

											devTime = dt;
											devTime += proctime;
#ifdef EKA2
											User::SetUTCTime(devTime);
#else
											//TODO: detect timezone and add accordingly
											TTime hometime(devTime);
											hometime += TTimeIntervalHours(7);
											User::SetHomeTime(hometime);
#endif


											iSyncTimeWhenValid = EFalse;

											CAknConfirmationNote* informationNote = new (ELeave) CAknConfirmationNote(EFalse);
											//informationNote->SetTimeout(CAknNoteDialog::EShortTimeout);
											_LIT(KConfirmText,"GPS TimeSync Successful");
											informationNote->ExecuteLD(KConfirmText);
											//TODO: report to scheduler that device time has changed? OR let scheduler detect device time change?


										}

							}
						/////////////////////////

							//SET LAT LON values...
							TBuf<1> ref;


							if(gpsdata.LAT.Length() < iGPSData.iLat.MaxLength()   && gpsdata.LAT_REF.Length()==1)
								{
							iGPSData.iLat.Copy(gpsdata.LAT);
							ref.Zero();
							ref.Copy(gpsdata.LAT_REF);
							iGPSData.iLat+=ref;
								}

							if(gpsdata.LON.Length() < iGPSData.iLon.MaxLength() && gpsdata.LON_REF.Length()==1)
								{
							iGPSData.iLon.Copy(gpsdata.LON);
							ref.Zero();
							ref.Copy(gpsdata.LON_REF);
							iGPSData.iLon+=ref;
								}






						_LIT(KGPSStateUpdate,"Connected to GPS");
						iObserver.OnGPSStateUpdate(KGPSStateUpdate,iGPSData );


					}//END if gps parsed


					line.Zero();
				}


				if(data.Length()>0)
					{
						if(line.Length() + data.Length() < line.MaxLength())
							{
								if(line.Length()==0) //if not appending data to previous line
									iLineHeadReceivedTime.HomeTime();
								line.Append(data);
							}
						else
							{
								line.Zero();
								if(line.Length() + data.Length() < line.MaxLength())
									{
										//if(line.Length()==0) sure it is, see above lines
											iLineHeadReceivedTime.HomeTime();
										line.Append(data);
									}
							}
					}

				data.Zero();

				iState = EReading;
				iSendingSocket.RecvOneOrMore(data, 0, iStatus, iReadDataLength);
				SetActive();
				ResetBtTimer();
			}
			break;
	}


}