Android Question GMT time

kepler

Active Member
Licensed User
Good evening,

I was wondering how I can get the universal GMT time from any android device.

I can extract the current date, and the time zone offset.

But what is the simpliest way of getting the GMT time in two strings, one with the date and the other with the time (24h format)?

Is the offset suposed to be added to the current time (that is, eastern regions have negative offsets)? But how can I subtract the offset from a date?...

Sorry for disturbing.

Kind regards,

Kepler
 

kepler

Active Member
Licensed User
Hi,

Thanks for the link. I've made the following test code:

B4X:
Sub GetGMT As String()
   Dim s, d As String
   Dim l As Long
   s = DateTime.DateFormat
   DateTime.DateFormat = "dd/MM/yyyy HH:mm:ss"
   l = DateTime.Now
   d = DateTime.Date(l) & " GMT"
   DateTime.DateFormat = "dd/MM/yyyy HH:mm:ss z"
   Dim res As Int
   res = -Round((l - DateTime.DateParse(d))/3600000) 'offset in seconds
   DateTime.DateFormat = s
  
   res = res * 1000 'offset in miliseconds
  
   l = l + res
  
    Dim dt As String
    Dim dt2 As String
  
    DateTime.DateFormat = "dd.MM.yyyy"   
    dt = DateTime.Date(l) 'Date only
    DateTime.DateFormat = "HH.mm.ss"
    dt2 = DateTime.Date(l) 'Time only

    Dim res_data() As String
    res_data = Array As String (dt,dt2)
    Return res_data
  
End Sub

Is this right in your opinion?

Regards,

Kepler
 
Upvote 0

Mahares

Expert
Licensed User
This looks more like what you want: the offset res in in hrs not sec
B4X:
Sub GetGMT As String()
   Dim s, d As String
   Dim l As Long
   s = DateTime.DateFormat
   DateTime.DateFormat = "dd/MM/yyyy HH:mm:ss"
   l = DateTime.Now
   d = DateTime.Date(l) & " GMT"
   DateTime.DateFormat = "dd/MM/yyyy HH:mm:ss z"
   Dim res As Int
   res = -Round((l - DateTime.DateParse(d))/3600000) 'offset in hrs  :exple: -5
 
   l = l  - res * 1000 * 3600  'millisec

    Dim dt, dt2 As String
    DateTime.DateFormat = "dd.MM.yyyy"  
    dt = DateTime.Date(l) 'Date only
    DateTime.DateFormat = "HH.mm.ss"
    dt2 = DateTime.Date(l) 'Time only

    Dim res_data() As String
    res_data = Array As String (dt,dt2)
    Log(res_data(0) &  "   "  & res_data(1))  'returns GMT date and time: exple: 28.2.2016  01:30:56
    DateTime.DateFormat = s
    Return res_data
 End Sub
 
Upvote 0

kepler

Active Member
Licensed User
Hi,

You're right - I've noticed it a few hours ago. But I had no chance to come here and correct the posted code.

Thanks anyway :)

Kind regards,

Kepler
 
Upvote 0

LucaMs

Expert
Licensed User
Note that using SetTimeZone(0) you'll get the device date converted to GMT, so, if the device date (time) is wrong the GMT will be wrong.

I think you should get the real GMT from the WEB.
 
Upvote 0
Top