In calling NSISO8601DateFormatter to format a timestamp from NSDate, I'd like to set options for the formatting. Like NSISO8601DateFormatWithSpaceBetweenDateAndTime as described in the documentation for NSISO8601DateFormatOptions
I can't seem to how to specify the necessary import in Python, and how to apply the options. I can't find the NSISO8601DateFormatter in the library repo like with e.g. NSTimeZone but yet the NSISO8601DateFormatter import works.
I've tried the import with the commented lines, but that gives ImportError: cannot import name...
I'm testing on a Mac running macOS 11.2.3 and default python 2.7.16
Example output:
2021-04-01T12:55:41+02:00 [py_objc_timestamp] timestamp from PyObjC
Example of desired output:
2021-04-01 12:55:41+02:00 [py_objc_timestamp] timestamp from PyObjC
This is my testing code, it's working aside from the lines commented out.
#!/usr/bin/python
# coding=utf-8
import CoreFoundation
from Foundation import (NSDate,
NSISO8601DateFormatter,
NSTimeZone)
#from Foundation import kCFISO8601DateFormatWithSpaceBetweenDateAndTime
#from Foundation import NSISO8601DateFormatOptions
def timestamp(dt=None):
if not dt:
dt = NSDate.date() # use now
the_format = NSISO8601DateFormatter.alloc().init()
the_format.setTimeZone_(NSTimeZone.localTimeZone())
#the_format.formatOptions = { kCFISO8601DateFormatWithSpaceBetweenDateAndTime }
timestamp = the_format.stringFromDate_(dt)
return timestamp
if __name__ == '__main__':
print (timestamp() + " [py_objc_timestamp] timestamp from PyObjC")