android - TimeZone based on location -
i have following code:
msavedtime = new time();//????always returns america/new york double latitude = 40.09596; double longitude = -74.22213; double elevation = 0; timezone timezone = timezone.gettimezone(msavedtime.timezone);
my first question is: how elevation information lat\lon combination? second question is: how timezone string lon\lan combination? above code provide timezone string clock on cell phone set (returns america/new york). want string according lat\lon setup me.
try code use google time zone api:
private string get_utc_datetime_from_timestamp(long timestamp){ try{ calendar cal = calendar.getinstance(); timezone tz = cal.gettimezone(); int tzt = tz.getoffset(system.currenttimemillis()); timestamp -= tzt; // dateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss",locale.getdefault()); dateformat sdf = new simpledateformat(); date netdate = (new date(timestamp)); return sdf.format(netdate); } catch(exception ex){ return ""; } } class ntp_utc_time { private static final string tag = "sntpclient"; private static final int receive_time_offset = 32; private static final int transmit_time_offset = 40; private static final int ntp_packet_size = 48; private static final int ntp_port = 123; private static final int ntp_mode_client = 3; private static final int ntp_version = 3; // number of seconds between jan 1, 1900 , jan 1, 1970 // 70 years plus 17 leap days private static final long offset_1900_to_1970 = ((365l * 70l) + 17l) * 24l * 60l * 60l; private long mntptime; public boolean requesttime(string host, int timeout) { try { datagramsocket socket = new datagramsocket(); socket.setsotimeout(timeout); inetaddress address = inetaddress.getbyname(host); byte[] buffer = new byte[ntp_packet_size]; datagrampacket request = new datagrampacket(buffer, buffer.length, address, ntp_port); buffer[0] = ntp_mode_client | (ntp_version << 3); writetimestamp(buffer, transmit_time_offset); socket.send(request); // read response datagrampacket response = new datagrampacket(buffer, buffer.length); socket.receive(response); socket.close(); mntptime = readtimestamp(buffer, receive_time_offset); } catch (exception e) { // if (config.logd) log.d(tag, "request time failed: " + e); return false; } return true; } public long getntptime() { return mntptime; } /** * reads unsigned 32 bit big endian number given offset in buffer. */ private long read32(byte[] buffer, int offset) { byte b0 = buffer[offset]; byte b1 = buffer[offset+1]; byte b2 = buffer[offset+2]; byte b3 = buffer[offset+3]; // convert signed bytes unsigned values int i0 = ((b0 & 0x80) == 0x80 ? (b0 & 0x7f) + 0x80 : b0); int i1 = ((b1 & 0x80) == 0x80 ? (b1 & 0x7f) + 0x80 : b1); int i2 = ((b2 & 0x80) == 0x80 ? (b2 & 0x7f) + 0x80 : b2); int i3 = ((b3 & 0x80) == 0x80 ? (b3 & 0x7f) + 0x80 : b3); return ((long)i0 << 24) + ((long)i1 << 16) + ((long)i2 << 8) + (long)i3; } /** * reads ntp time stamp @ given offset in buffer , returns * system time (milliseconds since january 1, 1970). */ private long readtimestamp(byte[] buffer, int offset) { long seconds = read32(buffer, offset); long fraction = read32(buffer, offset + 4); return ((seconds - offset_1900_to_1970) * 1000) + ((fraction * 1000l) / 0x100000000l); } /** * writes 0 ntp starttime stamp in buffer. --> ntp returns time offset since 1900 */ private void writetimestamp(byte[] buffer, int offset) { int ofs = offset++; (int i=ofs;i<(ofs+8);i++) buffer[i] = (byte)(0); } } string get_time_zone_time(geopoint gp){ string erg = ""; string raw_offset = ""; string dst_offset = ""; double longitude = gp.getlongitudee6()/1e6; double latitude = gp.getlatitudee6()/1e6; // string request = "http://ws.geonames.org/timezone?lat="+latitude+"&lng="+ longitude+ "&style=full"; long tslong = 0; // system.currenttimemillis()/1000; ntp_utc_time client = new ntp_utc_time(); if (client.requesttime("pool.ntp.org", 2000)) { tslong = client.getntptime(); } if (tslong != 0) { tslong = tslong / 1000; // https://maps.googleapis.com/maps/api/timezone/xml?location=39.6034810,-119.6822510×tamp=1331161200&sensor=true string request = "https://maps.googleapis.com/maps/api/timezone/xml?location="+latitude+","+ longitude+ "×tamp="+tslong +"&sensor=true"; string xmltext = get_xml_server_reponse(request); if(xmltext.compareto("")!= 0) { int startpos = xmltext.indexof("<timezoneresponse"); xmltext = xmltext.substring(startpos); xmlpullparser parser; try { parser = xmlpullparserfactory.newinstance().newpullparser(); parser.setinput(new stringreader (xmltext)); int eventtype = parser.geteventtype(); string tagname = ""; while(eventtype != xmlpullparser.end_document) { switch(eventtype) { case xmlpullparser.start_tag: tagname = parser.getname(); break; case xmlpullparser.text : if (tagname.equalsignorecase("raw_offset")) if(raw_offset.compareto("")== 0) raw_offset = parser.gettext(); if (tagname.equalsignorecase("dst_offset")) if(dst_offset.compareto("")== 0) dst_offset = parser.gettext(); break; } try { eventtype = parser.next(); } catch (ioexception e) { e.printstacktrace(); } } } catch (xmlpullparserexception e) { e.printstacktrace(); erg += e.tostring(); } } int ro = 0; if(raw_offset.compareto("")!= 0) { float rof = str_to_float(raw_offset); ro = (int)rof; } int dof = 0; if(dst_offset.compareto("")!= 0) { float doff = str_to_float(dst_offset); dof = (int)doff; } tslong = (tslong + ro + dof) * 1000; erg = get_utc_datetime_from_timestamp(tslong); } return erg; }
and use with:
geopoint gp = new geopoint(39.6034810,-119.6822510); string current_timezone_time = get_time_zone_time(gp);
Comments
Post a Comment