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
Developer
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook
My Carrier: wifi
Accepted Solution

Best Way to Get Timezone

I'm looking to get a user's current timezone. I see there is a function which seems to be able to do this as part of Calendar Pim. I'm not currently using any Calendar features and having to ask for permission to do so seems like overkill when I just want to get the current timezone.

 

http://developer.blackberry.com/native/reference/cascades/bb__pim__calendar__calendarsettings.html#f...

 

Is there a better way to do this?

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Best Way to Get Timezone

This thread and one that it references will help you out...

 

http://supportforums.blackberry.com/t5/Native-Development/CalendarEvent-and-TimeZones/td-p/2284333


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 1,168
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Best Way to Get Timezone

[ Edited ]

This can be pretty tricky since there isn't any way I'm aware of to ask BB10 directly for the offset from UTC (GMT). The basic procedure is this:

 

  • Get the current time with QDateTime::currentDateTime()
  • Make a copy of it with QDateTime::toUTC()
  • Force the original date to UTC.
  • The difference between the two datetimes now is the offset from UTC.

Something like this:

 

QDateTime dt1 = QDateTime::currentDateTime();
QDateTime dt2 = dt1.toUTC();
dt1.setTimeSpec(Qt::UTC);

int offset = dt2.secsTo(dt1);
 

After this runs offset will contain the offset from UTC in seconds, so divide by 3600 if you want hours.

 

UPDATE: For anyone reading this is the future who is having trouble understanding what the heck is going on in this code...

 

  • Line one just gets the current date and time in a QDateTime. This will be the time in the local timezone. It doesn't have to be current for this to work, any date and time will do, but current is convenient.
  • Line two makes a copy of the first date and time, but as a UTC time. So for instance, if you are in Toronto at UTC-5:00 and the current local time is 3:46pm, then dt2 will be set to 8:46pm, the current time in Greenwich, England, but the two dates are actually still identical since one is local and the other is UTC. For this reason at this point subtracting one from the other will result in zero since they are actually exactly the same date and time.
  • Line three says to dt1 "I know you think you are 3:46pm local time, but you are actually 3:46pm UTC." dt1 believes this without question. Now the two dates are actually different, and separated by the local offset from UTC.

Hope this makes things clearer because the reason this code works may not be obvious.



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.
Developer
Posts: 1,168
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Best Way to Get Timezone

How many times have you posted while I'm busy typing?

 


BBSJdev wrote:

This thread and one that it references will help you out...

 

http://supportforums.blackberry.com/t5/Native-Development/CalendarEvent-and-TimeZones/td-p/2284333






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Best Way to Get Timezone

I can't help it if you are a slow typer. Smiley Happy

 

... it's obviously quicker to reference a thread you remember reading before and search for it than try and rewrite the code, unless it's in my cookbook of course and I can just copy and paste.

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 1,168
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Best Way to Get Timezone

Yep, if you remember where you saw it. In my case I usually spend longer looking for the thread I vaguely remember than just typing it out from memory or my own code. The fact that you never sleep makes it very likely you are going to be posting while I'm still typing.

 


BBSJdev wrote:

I can't help it if you are a slow typer. Smiley Happy

 

... it's obviously quicker to reference a thread you remember reading before and search for it than try and rewrite the code, unless it's in my cookbook of course and I can just copy and paste.

 






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Best Way to Get Timezone

I think that's the advantage I have in having been on here for a while now, I know all the tricks for getting quickly to the relevant posting and can usually turn a vague remembrance in to a concrete link.

 

I wonder if anyone would be interested in that as a KB article, probably not many. Smiley Happy

 

It wouldn't be a big article anyway,

 

 - learn to use the advanced search

 - know that you can tag and search on tags

 - use of chrome extensions and scripts


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Highlighted
Developer
Posts: 1,168
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Best Way to Get Timezone

I'd be interested, but I think you knew I would be. Can't speak for the others here though, apathy runs deep here sometimes, as you well know.


BBSJdev wrote:

I think that's the advantage I have in having been on here for a while now, I know all the tricks for getting quickly to the relevant posting and can usually turn a vague remembrance in to a concrete link.

 

I wonder if anyone would be interested in that as a KB article, probably not many. Smiley Happy

 

It wouldn't be a big article anyway,

 

 - learn to use the advanced search

 - know that you can tag and search on tags

 - use of chrome extensions and scripts






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.