The time here (at least in this piece of code) comes from calling
"time(NULL)", which is really no better than calling new Date().getTime().
You are also confusing local time and time set by the user. Local time
is UTC time corrected by the local timezone. Java uses UTC time for
internal representation, is not affected by timezone changes, and
shouldn't give you any trouble.
On the other hand, the user is free to change time to any value
whatsoever, even an incorrect one. The time zone setting can be wrong, too.
If you really need time information that's device- (and user-)
independent, you'll need to get it from some place that's not controlled
by the user and can be counted on for being there.
Like an NTP server.
http://www.pool.ntp.org/en/
-- Kostya
25.08.2010 12:11, optimusgeek пишет:
I need to get pure UTC time in application level.
It should not be changed by user or location or anything else.
When I try with System.currentTimeMillis() or getTime() of Date class,
they always return value of local time. If user change date or time,
the return value is also changed.
I found handleEndOfInit method in android_modem.c (external\qemu
\telephony)
and the method is like below.
Probably, the variable "utc" is what I want.
1. Is the android_modem.c for emulator??
2. If it is, I think there must be similar file for target. Where is
it?
3. How can I get the "utc" value in my application?
static const char*
handleEndOfInit( const char* cmd, AModem modem )
{
time_t now = time(NULL);
struct tm utc, local;
long e_local, e_utc;
long tzdiff;
char tzname[64];
tzset();
utc = *gmtime(&now );
local = *localtime(&now );
e_local = local.tm_min + 60*(local.tm_hour + 24*local.tm_yday);
e_utc = utc.tm_min + 60*(utc.tm_hour + 24*utc.tm_yday);
if ( utc.tm_year< local.tm_year )
e_local += 24*60;
else if ( utc.tm_year> local.tm_year )
e_utc += 24*60;
tzdiff = e_local - e_utc; /* timezone offset in minutes */
/* retrieve a zoneinfo-compatible name for the host timezone
*/
{
char* end = tzname + sizeof(tzname);
char* p = bufprint_zoneinfo_timezone( tzname, end );
if (p>= end)
strcpy(tzname, "Unknown/Unknown");
/* now replace every / in the timezone name by a "!"
* that's because the code that reads the CTZV line is
* dumb and treats a / as a field separator...
*/
p = tzname;
while (1) {
p = strchr(p, '/');
if (p == NULL)
break;
*p = '!';
p += 1;
}
}
/* as a special extension, we append the name of the host's time
zone to the
* string returned with %CTZ. the system should contain special
code to detect
* and deal with this case (since it normally relied on the
operator's country code
* which is hard to simulate on a general-purpose computer
*/
return amodem_printf( modem, "%%CTZV: %02d/%02d/%02d:%02d:%02d:%02d
%c%d:%d:%s",
(utc.tm_year + 1900) % 100, utc.tm_mon + 1, utc.tm_mday,
utc.tm_hour, utc.tm_min, utc.tm_sec,
(tzdiff>= 0) ? '+' : '-', (tzdiff>= 0 ? tzdiff : -
tzdiff) / 15,
(local.tm_isdst> 0),
tzname );
}
--
Kostya Vasilev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en