[英]How to convert a Python datetime.datetime.utctime() to an Obj-C NSDate, and back again?
編輯:基本上,我希望在UTC時間,最好通過ISO-8601:
這似乎應該非常簡單,但是我似乎無法弄清楚。
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'
在事物的Obj-C方面:
// 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對象。 任何幫助是極大的贊賞! :)
使用datetime.now(tz)
代替datetime.utcnow()
將使您知道日期時間對象。
如果您確實想要當地時間:
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的數據庫,則:
now = datetime.datetime.utcnow()
ET.localize(now).strftime("%Y-%m-%d %H:%M:%S %z")
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
如果您不想處理時區對象,並且您的時間以UTC為單位,那么為什么不按照ISO-8601的定義僅將定義Zulu / Zero的“ Z”附加到末尾
>>> datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
'2012-03-08T00:07:31Z'
除非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'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.