2013-08-25 21 views
19

從文檔:理解UILocalNotification的timeZone

在fireDate指定的日期爲根據該屬性的值 解釋。如果您指定nil(默認值),則解釋爲絕對GMT時間的解除日期爲 ,這適用於 作爲倒計時定時器的情況。如果您爲此 媒體資源分配了有效的NSTimeZone對象,則在有時間區域發生更改時,會將發射日期解釋爲 自動調整的掛鐘時間;適用於這種情況的示例是鬧鐘。

假設我爲格林威治標準時間明天中午(絕對時間)安排本地通知。我安排在西雅圖下午1點(太平洋時間,格林威治標準時間8),然後立即前往芝加哥到達下午11點(中部時間,格林威治標準時間-6)。我的設備從太平洋時間調整到中央時間。請幫我理解我的通知何時會發生在以下三種情況下:

  1. UILocalNotification timeZone被設置爲零。
  2. UILocalNotification timeZone被設置爲GMT。
  3. UILocalNotification timeZone被設置爲太平洋時間。

回答

41

我剛剛在iOS 6.1.3上運行了一些測試。這是我得到的:

我在西雅圖,在下午1:00(太平洋夏令時,格林威治標準時間-7)。我創建了一個NSDate

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
// 2013-08-31 @ 12:00:00 (noon) 
dateComponents.year = 2013; 
dateComponents.month = 8; 
dateComponents.day = 31; 
dateComponents.hour = 12; 
dateComponents.minute = 0; 
dateComponents.second = 0; 

NSDate *fireDate = [gregorianCalendar dateFromComponents:dateComponents]; 

現在我有

fireDate = 2013-08-31 19:00:00 +0000 (2013-08-31 12:00:00 -0700) 

然後我創建並安排通知:西雅圖(太平洋夏令時剛剛創建的通知

notification1 = [[UILocalNotification alloc] init]; 
notification1.fireDate = fireDate; 
// notification1.timeZone is nil by default 

NSLog(@"%@", notification1); 

notification2 = [[UILocalNotification alloc] init]; 
notification2.fireDate = fireDate; 
notification2.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 

NSLog(@"%@", notification2); 

notification3 = [[UILocalNotification alloc] init]; 
notification3.fireDate = fireDate; 
notification3.timeZone = [NSTimeZone defaultTimeZone]; 

NSLog(@"%@", notification3); 

日誌, ,GMT-7):

notification1: 
fire date = Saturday, August 31, 2013, 12:00:00 PM Pacific Daylight Time, 
time zone = (null), 
next fire date = Saturday, August 31, 2013, 12:00:00 PM Pacific Daylight Time 

notification2: 
fire date = Saturday, August 31, 2013, 7:00:00 PM GMT, 
time zone = GMT (GMT) offset 0, 
next fire date = Saturday, August 31, 2013, 7:00:00 PM Pacific Daylight Time 

notification3: 
fire date = Saturday, August 31, 2013, 12:00:00 PM Pacific Daylight Time, 
time zone = US/Pacific (PDT) offset -25200 (Daylight), 
next fire date = Saturday, August 31, 2013, 12:00:00 PM Pacific Daylight Time 

我將電話的時區更改爲芝加哥,現在是下午3點(中央夏令時間,格林威治標準時間-5)。

日誌的通知,在芝加哥(中部夏令時間,GMT-5)

notification1: 
fire date = Saturday, August 31, 2013, 2:00:00 PM Central Daylight Time, 
time zone = (null), 
next fire date = Saturday, August 31, 2013, 2:00:00 PM Central Daylight Time 

notification2: 
fire date = Saturday, August 31, 2013, 7:00:00 PM GMT, 
time zone = GMT (GMT) offset 0, 
next fire date = Saturday, August 31, 2013, 7:00:00 PM Central Daylight Time 

notification3: 
fire date = Saturday, August 31, 2013, 12:00:00 PM Pacific Daylight Time, 
time zone = US/Pacific (PDT) offset -25200 (Daylight), 
next fire date = Saturday, August 31, 2013, 12:00:00 PM Central Daylight Time 

結論:

  1. 當UILocalNotification timeZone是零,火日期是固定的時間。這意味着通知將在GMT格林威治標準時間下午12:00,格林威治標準時間下午2點或格林威治標準時間下午7點發射。
  2. 當UILocalNotification timeZone設置爲GMT時,計算GMT時間的啓動日期,並且如果用戶轉到其他時區,則會自動更新。在這個例子中,格林尼治標準時間-7點的時間被轉換爲格林尼治標準時間19點,並且通知被設置爲當地時間19點,無論我們在哪個時區(格林威治標準時間19點,格林威治標準時間5點19分或格林威治標準時間19:00)。
  3. 當UILocalNotification timeZone設置爲當地時區(太平洋夏令時,格林威治標準時間-7)時,計算當地時間的火災日期,如果用戶轉到其他時區,則會自動更新火災日期。在這個例子中,時間是格林威治標準時間-7點,因此無論我們在哪個時區(格林威治標準時間12:00,格林威治標準時間-5點或格林威治標準時間12:00),當地時間12:00通知您。 7)。
+0

太棒了。從技術上講,你預定這個時間是格林尼治時間7PM而不是中午,但它也回答了這個問題。 –

+1

有沒有人對iOS 10中的UNNotification觸發日期進行過類似的調查? – Jonny

+0

@Jonny我也想知道。看來設置時區沒有任何區別。 – David

0

在Swift 3中,使用TimeZone.autoupdatingCurrent的行爲與objective-C [NSTimeZone defaultTimeZone]的行爲相同。

+0

這不回答OP。你有任何進一步的信息。 – David