objective c - How to convert a Python datetime.datetime.utctime() to an Obj-C NSDate, and back again? -


edit: basically, i'm looking this, in utc-time, ideally via iso-8601:

  1. python: datetime.datetime ---> iso-8601 string
  2. python: iso-8601 string ---> datetime.datetime
  3. obj-c: nsdate ---> iso-8601 nsstring
  4. obj-c: iso-8601 nsstring ---> nsdate

this seems should simple, can't seem figure out.

python code, converting string:

>>> import datetime >>> datetime.datetime.utcnow().strftime("%y-%m-%d %h:%m:%s %z") '2012-03-08 00:07:31 ' 

note time-zone info %z printed empty string, since utcnow() returns naive datetime object. how turn aware 1 , print following?

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

on obj-c side of things:

// fails , prints (null) since timezone missing. nsstring *pythondate1 = @"2012-03-07 23:51:58 "; nsdate   *objcdate1   = [nsdate datewithstring:pythondate1]; nslog(@"%@", objcdate1);  // works, manually adding in "+0000". nsstring *pythondate2 = @"2012-03-07 23:51:58 +0000"; nsdate   *objcdate2   = [nsdate datewithstring:pythondate2]; nslog(@"%@", objcdate2); 

printout:

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 

i'm not quite sure how go nsdate datetime.datetime object either. appreciated! :)

if don't want deal timezone objects, , time in utc, why not append "z" defining zulu/zero end defined in iso-8601

>>> datetime.datetime.utcnow().strftime("%y-%m-%dt%h:%m:%sz") '2012-03-08t00:07:31z' 

unless of course obj-c doesn't support iso-8601 formatting...

or, if still going use utcnow(), can cheat , add +00:00:

>>> datetime.datetime.utcnow().strftime("%y-%m-%d %h:%m:%s +00:00") '2012-03-08 00:07:31 +00:00' 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -