閱讀640 返回首頁    go 阿裏雲 go 技術社區[雲棲]


obj-c編程10:Foundation庫中類的使用(5)[時間對象]

    隔了好久才有了這新的一篇,還是無奈的時間啊!so這次我們就著重談談它嘍。

    F庫中有很多時間相關的類,比如NSDate,NSTimeInterval,NSTimeZone,NSDateComponents,NSCalendar,最後還有一個NSDateFormatter。新手很快就被弄的雲裏霧裏了,我也在霧裏呆了好長段時間了。簡單地說NSDate隻是一個單純的時間類;NSTimeInterval用來表示2個時間之間的間隔;NSTimeZone和NSCalendar顧名思義自然是表示時區和日曆啦;NSDateComponents可以從多個單個元素構建任意一個時間,而NSDateFormatter可以將時間轉換為一個人類可讀的字符串或者反向轉換。下麵我們用代碼來說明問題嘍:

#import <Foundation/Foundation.h>

//static int g_idx = 0;

#define msg(...) printf("[[%d]]:\n",__LINE__);NSLog(__VA_ARGS__)

int main(int argc,char *argv[])
{
	@autoreleasepool{
		//創建現在時間的NSDate對象
		NSDate *now = [NSDate date];
		NSDate *now_too = [[NSDate alloc] init];
		msg(@"now is %@ and %@",now,now_too);

		//使用時間間隔創建NSDate對象
		NSDate *now_sub_hour = [now dateByAddingTimeInterval:-3600];
		NSDate *now_add_hour = [now dateByAddingTimeInterval:3600];
		msg(@"now +- hour is %@ and %@",now_add_hour,now_sub_hour);

		//計算2個時間的間隔(以秒計)
		NSTimeInterval interval1 = [now timeIntervalSinceDate:now_sub_hour];
		NSTimeInterval interval2 = [now timeIntervalSinceDate:now_add_hour];
		msg(@"intervals is %f and %f",interval1,interval2);

		//比較2個時間的“大小”
		assert([now laterDate:now_add_hour] == now_add_hour);	//true
		assert([now earlierDate:now_sub_hour] == now_sub_hour);	//true
		assert([now compare:now_sub_hour] == NSOrderedDescending);	//true

		NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];

		//使用時間組件對象和日曆對象構造時間
		NSDateComponents *cps = [[NSDateComponents alloc] init];
		[cps setTimeZone:zone];

		[cps setMonth:11];
		[cps setDay:23];
		[cps setYear:2018];
		[cps setHour:23];
		[cps setMinute:59];
		[cps setSecond:31];
		NSCalendar *calar = [NSCalendar currentCalendar];
		NSDate *date = [calar dateFromComponents:cps];
		msg(@"date is %@",date);
		[cps setDay:([cps day] - 1)];
		[cps setMinute:([cps minute] + 1)];
		date = [calar dateFromComponents:cps];
		msg(@"date is %@",date);

		//設置日曆類型為中國日曆類型
		NSCalendar *china_calar = [[NSCalendar alloc] initWithCalendarIdentifier:\
			NSRepublicOfChinaCalendar];
		date = [china_calar dateFromComponents:cps];
		msg(@"china_calar style is %@",date);

		//列出所有已知時區
		//msg(@"all zone :\n%@",[NSTimeZone knownTimeZoneNames]);
		//在時間對象中考慮時區因素
		msg(@"時區縮寫:%@",[zone abbreviation]);
		NSData *data = [zone data];
		msg(@"time data is %@",data);
		NSCalendar *calar_zone = [NSCalendar currentCalendar];
		[calar_zone setTimeZone:zone];
		cps = [calar_zone components:(NSYearCalendarUnit|NSMonthCalendarUnit|\
			NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|\
			NSSecondCalendarUnit) fromDate:now];
		[cps setTimeZone:zone];
		date = [calar_zone dateFromComponents:cps];
		msg(@"time with zone : %@",date);

		NSDateFormatter *f = [[NSDateFormatter alloc]init];
		[f setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

		msg(@"init time_zone is %@",[f timeZone]);
		date = [f dateFromString:@"2019-10-11 03:14:59"];
		msg(@"date is %@",date);
		[f setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
		date = [f dateFromString:@"2019-10-11 03:14:59"];
		msg(@"date is %@",date);
		[f setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+8"]];
		NSString *fix_date_str = [f stringFromDate:date];
		msg(@"fix_str is %@",fix_date_str);
		[f setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
		date = [f dateFromString:fix_date_str];
		msg(@"date is %@",date);
	}	
	return 0;
}

編譯執行結果如下:

apple@kissAir: objc_src$./8

[[13]]:

2014-07-20 17:42:03.593 8[2468:507] now is 2014-07-20 09:42:03 +0000 and 2014-07-20 09:42:03 +0000

[[18]]:

2014-07-20 17:42:03.594 8[2468:507] now +- hour is 2014-07-20 10:42:03 +0000 and 2014-07-20 08:42:03 +0000

[[23]]:

2014-07-20 17:42:03.594 8[2468:507] intervals is 3600.000000 and -3600.000000

[[44]]:

2014-07-20 17:42:03.595 8[2468:507] date is 2018-11-23 15:59:31 +0000

[[48]]:

2014-07-20 17:42:03.596 8[2468:507] date is 2018-11-22 16:00:31 +0000

[[54]]:

2014-07-20 17:42:03.597 8[2468:507] china_calar style is 3929-11-22 16:00:31 +0000

[[59]]:

2014-07-20 17:42:03.599 8[2468:507] 時區縮寫:GMT+8

[[61]]:

2014-07-20 17:42:03.601 8[2468:507] time data is <545a6966 00000000 00000000 00000000 00000000 00000003 00000003 00000000 00000011 00000003 0000000c b0fe9a9b c85c0180 c8fa2770 c9d50e80 cadb5af0 1eba3600 1f697f70 207e6880 21496170 225e4a80 23294370 24476700 25125ff0 26274900 26f241f0 28072b00 28d223f0 02010201 02010201 02010201 02010201 02000071 e5000000 007e9001 04000070 8000084c 4d540043 44540043 53540000 00000000 00>

[[69]]:

2014-07-20 17:42:03.602 8[2468:507] time with zone : 2014-07-20 09:42:03 +0000

[[74]]:

2014-07-20 17:42:03.603 8[2468:507] init time_zone is Asia/Harbin (GMT+8) offset 28800

[[76]]:

2014-07-20 17:42:03.604 8[2468:507] date is 2019-10-10 19:14:59 +0000

[[79]]:

2014-07-20 17:42:03.604 8[2468:507] date is 2019-10-11 03:14:59 +0000

[[82]]:

2014-07-20 17:42:03.605 8[2468:507] fix_str is 2019-10-11 11:14:59

[[85]]:

2014-07-20 17:42:03.606 8[2468:507] date is 2019-10-11 11:14:59 +0000


要注意的是,我開始在用NSDateFormatter轉換時間時,會發現設置時區後,轉換的時間時區總是不對,後來發現NSDateFormatter總會把任意時區時間轉換為GMT時間,如果你是GMT+8時區,即中國標準時間,則會將時間減去8小時。就像代碼中最後一個例子,如果你不設置zone的時區,默認會使用本機時區,我這裏就是GMT+8時區,則date對象實際上時間為2019-10-11 03:14:59減去8小時哦。當我們把f對象的時區變為GMT後,則時間上就不會有任何差別。所以你想顯示當前時間是不可以的:

NSDate *now2 = [NSDate date];
msg(@"now2 is %@",now2);

當前時間是 2014-07-20 17:49:10,可是你顯示出來會減去8小時的哦,你必須如下做轉換:

NSDate *now2 = [NSDate date];
		msg(@"now2 is %@",now2);
		NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
		[f2 setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
		[f2 setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+8"]];
		NSString *now2_str = [f2 stringFromDate:now2];
		msg(@"now2 is %@",now2_str);

了解了吧?運行結果如下:

2014-07-20 17:52:59.513 8[2502:507] now2 is 2014-07-20 09:52:59 +0000

[[93]]:

2014-07-20 17:52:59.513 8[2502:507] now2 is 2014-07-20 17:52:59



最後更新:2017-04-03 05:39:29

  上一篇:go 2014年7月10日,我人生的最重要Upgrade
  下一篇:go 【北大夏令營筆記-數學題】百練1700-Crossing River