Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Trusted Contributor
JohnWS
Posts: 117
Registered: ‎02-08-2013
My Device: 9900
Accepted Solution

need a little help with time calculation

[ Edited ]

Hi,

 

I have been trying to implement oauth2 and youtube api. I can login and can retrieve the access_token and other required oauth json values like expires_in.

 

What I'm trying to do is workout if my access token is valid or not before making further calls to their api - youtube gives a value - expires_in which contains normally 3600 which I'm assuming is seconds. I am trying to add this to now time using clock_gettime()  but I'm not getting correct values for my calculation - nowTime + expiresInt.

 

Can anyone point out the problem with my code please? It might be a bad way of doing it but I thought I'd spend a couple of hours on it before asking for help.

 

Thanks.

 

token = c.value("access_token").toString().toStdString();
	refreshtoken = c.value("refresh_token").toString().toStdString();
	saveValueFor("access_token", QString::fromStdString(token));
	string expires = c.value("expires_in").toString().toStdString();
	saveValueFor("refresh_token", QString::fromStdString(refreshtoken));
	struct timespec* start;
	clock_gettime(CLOCK_REALTIME, start);
	int nowTime = start->tv_sec;
	std::stringstream str(expires);
	int expiresInt;
	str >> expiresInt;
	nowTime += expiresInt;
	stringstream nowTimeString;//create a stringstream
	nowTimeString << nowTime;//add number to the stream

 

Developer
oliver_kranz
Posts: 216
Registered: ‎09-18-2009
My Device: Z10

Re: need a little help with time calculation

You could use QDateTime to get the current time.

 

QDateTime dt = QDateTime::currentDateTime();

 

Trusted Contributor
JohnWS
Posts: 117
Registered: ‎02-08-2013
My Device: 9900

Re: need a little help with time calculation

Thanks,

 

I have now tried with QDateTime and getting simiar weird results. I am just trying to add 3600 to an int about 10 digits long  but I get a result which can be 9 digits long (when i was using clock_gettime()).

 

Can anyone help me get the correct results from my calculations please?

 

qint64 nowTimeInt = QDateTime::currentMSecsSinceEpoch();

	std::stringstream str(expires);
	qint64 expiresInt;
	str >> expiresInt;
	nowTimeInt += expiresInt;
	stringstream expiresTimeString;//create a stringstream
	expiresTimeString << nowTimeInt;//add number to the stream

	/* Lets not forget to error checking */
	if (!str)
	{
		// The conversion failed.
		// Need to do something here.
		// Maybe throw an exception
	}
	else
	{
		saveValueFor("expires", expiresTimeString.str().data());
	}

 

Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook

Re: need a little help with time calculation

Now you are adding milliseconds and seconds.

 

Trusted Contributor
JohnWS
Posts: 117
Registered: ‎02-08-2013
My Device: 9900

Re: need a little help with time calculation

Just went back to it and fixed thanks to your little point in the right direction - thank you.