iOS 时区获取问题

class TaskHelper {

    public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task) {
        execute(task, (P[]) null);
    }

    @SuppressLint("NewApi")
    public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task, P... params) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
        } else {
            task.execute(params);
        }
    }
}

  

 

时区缩写 UTC, CST, GMT, CEST 以及转换

UTC是协调世界时(Universal Time
Coordinated)英文缩写,是由于国际无线电咨询委员会规定和推荐,并出于国际时间局(BIH)负责维持的因为秒为底蕴之岁月标度。UTC相当给本初子午线(即经度0度)上的平均太阳经常,过去既为此格林威治平均时(GMT)来表示.北京时间比UTC时间早8时,以1999年1月1日0000UTC也条例,UTC时间是零点,北京时间为1999年1月1日早上8点整。

 

GMT(Greenwich Mean Time)是格林尼治平时: 
出于地球轨道并非圆形,其运转速度又就地球与阳光之偏离改变而产出变化,因此视太阳经常不足均匀性。视太阳日的长短同时也受到地球自转轴相对轨道面之斜度所影响。为在如纠正上述的未咸匀性,天文学家计算地球非圆形轨迹和极轴倾斜对视太阳经常之法力。平太阳经常即是依靠通过修订后底看看太阳经常。在格林尼治子午线上的平太阳时叫世界经常(UT0),又吃格林尼治平时(GMT)。
为了确保协调世界时与世界时(UT1)相差不见面越0.9秒,有需要经常就会于协调世界时内长刚还是负闰秒。因此协调世界时与国际原子时(TAI)之间会出现多整数秒的差距。位于巴黎之国际地球自转事务中央局(IERS)负责控制何时在闰秒。

也就是说,UTC 基本上等于GMT

 

CET

(英語:Central European
Time,CET)欧洲正中时间大凡于世界标准时间(UTC)早一个时的时区名称之一。它叫大部分欧洲国同有北非国家采用。冬令日也UTC+1,夏季欧洲夏令时为UTC+2

 

CEST

欧洲中间夏令时间(Central European Summer
Time,简称CEST)比世界标准时间(UTC)早两单小时的时区名称之一。它叫多数欧洲江山以及有北非国家在夏使(UTC+2),冬季这些国家利用欧洲之中时间(UTC+1)

时区在线转换工具网站  http://www.timebie.com/timezone/centraleuropeansummerbeijing.php 

 

 

CST

CST却以可以象征如下 4 个不同之时区: 

Central Standard Time (USA) UTC-6:00 

Central Standard Time (Australia) UTC+9:30 
China Standard Time UTC+8:00 
Cuba Standard Time UTC-4:00  可见,CST可以同时意味着美国,澳大利亚,中国,古巴四个国家之规范日。 也就是说,这个需要依据使用的光景来确定下的是何许人也时区。

////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////

1. 专业的获时区的科学方法

  [NSTimeZone resetSystemTimeZone]; // 重置手机系统的时区

    NSInteger offset = [NSTimeZone localTimeZone].secondsFromGMT;

    offset = offset/3600;

    NSString *tzStr = [NSString stringWithFormat:@”%ld”,
(long)offset];

 抱之时区:(Asia/Shanghai (GMT+8)
offset 28800)

2.  截取 GMT 方法

    //获取当地的时区

    NSString *tzStr;

    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];

    NSArray *zoneArray = [[NSString stringWithFormat:@”%@”,
localTimeZone] componentsSeparatedByString:@”GMT”];

    NSString *tempTZ = [NSString stringWithFormat:@”%@”,
zoneArray.lastObject];

    // eg: +8 去丢”+”, -8 传入全部, +0 / -0

    // – 1 ~ 12, + 1 ~ +12, 0

    if([tempTZ hasPrefix:@”+”]) {

        tzStr = [NSString stringWithFormat:@”%c”, [tempTZ
characterAtIndex:1]];

    }else  if([tempTZ hasPrefix:@”-“]){

        tzStr = [NSString stringWithFormat:@”%@”, [tempTZ
substringToIndex:2]];

    }else if([tempTZ hasPrefix:@”0″]) {

        tzStr = [NSString stringWithFormat:@”%@”, [tempTZ
substringToIndex:1]];

    }

获得的时区:
Europe/Berlin (CEST) offset 7200 (Daylight)

鉴于有些时区非携带 GMT 格式吗故 “方法二 ”
不可行, 采用 “方法一”.

据悉距离 0
时区偏差的秒数算有时区.**
 **

每当3.0 以后引入了新的法门。可以不以一个线程池里运行。

android
的AysncTask直接调用Execute会在以一个线程池里比如调用的先后顺序依次执行。

只要运用的具有网络取都负之来举行,当有一个网络要柱塞,就导致其他要也柱塞了。

 

asyncTask.execute

Note: this function schedules the task on a queue for a single
background thread or pool of threads depending on the platform
version. When first introduced, AsyncTasks were executed serially on a
single background thread. Starting with
android.os.Build.VERSION_CODES.DONUT, this was changed to a pool of
threads allowing multiple tasks to operate in parallel. After
android.os.Build.VERSION_CODES.HONEYCOMB, it is planned to change
this back to a single thread to avoid common application errors caused
by parallel execution. If you truly want parallel execution, you can
use the executeOnExecutor version of this method with
THREAD_POOL_EXECUTOR; however, see commentary there for warnings on
its use. 

This method must be invoked on the UI thread.必须UI线程中调用

在意:这个函数让任务是坐单线程队列方式或丝程池队列方式运行,依赖让阳台版本要有所不同。asyncTask首不好引入时,这个函数会吃任务后台单线程串行方式履行。从android.os.Build.VERSION_CODES.DONUT(android
1.6)开始,它深受许任务在线程池中多任务并行执行。但在 android.os.Build.VERSION_CODES.HONEYCOMB(android
3.0)之后,它以该归了,变成了单线程执行的模式,原因是多线程并行执行容易引发问题。如果你确实想并行执行任务,你可采取另外一个版:使用THREAD_POOL_EXECUTOR参数的executeOnExecutor方法,但如若留意采取警告提示

anyncTask.executeOnExecutor

This method is typically used with THREAD_POOL_EXECUTOR to allow
multiple tasks to run in parallel on a pool of threads managed by
AsyncTask, however you can also use your own Executor for custom
behavior. 

Warning: Allowing multiple tasks to run in parallel from a thread pool
is generally not what one wants, because the order of their operation
is not defined. For example, if these tasks are used to modify any
state in common (such as writing a file due to a button click), there
are no guarantees on the order of the modifications. Without careful
work it is possible in rare cases for the newer version of the data to
be over-written by an older one, leading to obscure data loss and
stability issues. Such changes are best executed in serial; to
guarantee such work is serialized regardless of platform version you
can use this function with SERIAL_EXECUTOR. 
This method must be invoked on the UI thread.
Parameters:
exec The executor to use. THREAD_POOL_EXECUTOR is available as a
convenient process-wide thread pool for tasks that are loosely
coupled.

此办法一般与THREAD_POOL_EXECUTOR一起以,允许多只任务在由AsyncTask管理的线程池中并行执行,但是你若呢得以用于定义行为的Executor。

 

警告:因为实施操作顺序并未定义,通常情况下,允许多独任务在线程池中并行执行,其结果毫无是你想如果的。例如:这些任务都要失去修改某状态值(诸如点击按钮写文件),因为无确定的改顺序,旧的改或会见盖新修改的本内容,导致不安宁数据丢失而改为一个安乐之题材。因此这种任务最是串行执行;确保这些任务串行执行要未因让阳台版本的方式是,使用SERIAL_EXECUTOR