2012-03-08 62 views
1

編輯:基本上,我要做到這一點,在UTC時間,最好通過ISO-8601:如何將Python datetime.datetime.utctime()轉換爲Obj-C NSDate並返回?

  1. 的Python:datetime.datetime ---> ISO-8601字符串
  2. 的Python:ISO -8601串---> datetime.datetime
  3. 的OBJ-C:的NSDate ---> ISO-8601的NSString
  4. 的OBJ-C:ISO-8601的NSString --->的NSDate

這看起來應該很簡單,但我似乎無法弄清楚。

Python代碼,轉換爲字符串:

>>> import datetime 
>>> datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S %z") 
'2012-03-08 00:07:31 ' 

注意,時區信息%z打印爲空字符串,因爲utcnow()返回一個幼稚 DateTime對象。我怎麼把它變成知道之一,並讓它打印如下?

'2012-03-08 00:07:31 +0000' 

對事物的對象 - 側:

// This fails and prints (null) since the timezone is missing. 
NSString *pythonDate1 = @"2012-03-07 23:51:58 "; 
NSDate *objCDate1 = [NSDate dateWithString:pythonDate1]; 
NSLog(@"%@", objCDate1); 

// This works, manually adding in the "+0000". 
NSString *pythonDate2 = @"2012-03-07 23:51:58 +0000"; 
NSDate *objCDate2 = [NSDate dateWithString:pythonDate2]; 
NSLog(@"%@", objCDate2); 

打印輸出:

2012-03-07 19:14:47.848 Untitled 3[3912:707] (null) 
2012-03-07 19:14:47.849 Untitled 3[3912:707] 2012-03-07 23:51:58 +0000 

我不太清楚如何從一個NSDate回去一個datetime.datetime對象。任何幫助是極大的讚賞! :)

回答

1

,如果你不希望處理時區的對象,和你的時間是UTC,爲什麼不追加「Z」定義祖魯/零到年底在ISO-8601

>>> datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ") 
'2012-03-08T00:07:31Z' 
定義10

除非Obj-C不支持ISO-8601格式化...

或者,如果你仍然只打算使用utcnow(),你可以欺騙,只是加+00:00:

>>> datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S +00:00") 
'2012-03-08 00:07:31 +00:00' 
+0

它看起來並不嚴格地支持它,但只有部分。它不喜歡'Z',但它會接受'+ 0000'和'+00:00'。 – 2012-03-09 02:53:45

+1

同樣,如果你只能通過Obj-C utc時間,你可以作弊並提供+00:00。否則,我想你將不得不使用pytz,就像Phil提到的一樣。 – monkut 2012-03-09 04:12:48

2

如果你真的想在本地時間:

import datetime 
from pytz import timezone 

ET = timezone('US/Eastern') 

now = datetime.datetime.now(ET) 
now.strftime("%Y-%m-%d %H:%M:%S %z") 

如果您有使用UTC那麼DB或東西:

now = datetime.datetime.utcnow() 
ET.localize(now).strftime("%Y-%m-%d %H:%M:%S %z") 
2

Python的日期時間爲ISO-8601字符串:

>>> from datetime import datetime 
>>> now = datetime.utcnow() 
>>> string = now.strftime("%Y-%m-%dT%H:%M:%S +0000")    # Manually specifying ' +0000' since we know we have UTC time. 
>>> 
>>> string 
'2012-03-08T21:19:26 +0000' 

ISO-8601字符串到Python日期時間:

>>> from datetime import datetime 
>>> string = "2012-03-08T21:19:26 +0000" 
>>> time = datetime.strptime(string, "%Y-%m-%dT%H:%M:%S +0000") # Only works for UTC time. 
>>> 
>>> time 
datetime.datetime(2012, 3, 8, 21, 18, 31) 

的NSDate到ISO-8601的NSString:

NSDate   *now   = [NSDate date]; 
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
NSLocale  *locale   = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; 
NSTimeZone  *timeZone  = [NSTimeZone timeZoneWithName:@"UTC"]; 
[dateFormatter setLocale:locale];         // Using "en_US" for locale ?eliminates/reduces? issues with systems running different locales. 
[dateFormatter setTimeZone:timeZone]; 
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; // Explicitly re-stating default behavior for 10.4+. 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss Z"]; 
NSString  *iso8601String = [dateFormatter stringFromDate:now]; 

NSLog(@"%@", iso8601String); 
2012-03-09T02:44:37 +0000 

ISO-8601的NSString的NSDate:

NSString  *iso8601String = @"2012-03-09T20:37:49 +0000"; 
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
NSLocale  *locale   = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; 
[dateFormatter setLocale:locale];         // Using "en_US" for locale ?eliminates/reduces? issues with systems running different locales. 
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; // Explicitly re-stating default behavior for 10.4+. 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss Z"]; 
NSDate *date = [dateFormatter dateFromString:iso8601String]; 

NSLog(@"%@", date); 
2012-03-09 20:37:49 +0000