Question

I have created simple ObjectiveC dynamic library with one function

NSString* DateTimeToString(NSDate* dt)
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY'-'MM'-'dd'T'HH':'mm':'ss"];
    NSString *stringDate = [formatter stringFromDate:dt];
    [formatter release];

    return stringDate;    
}

As you can see this function will return date in format 2011-04-14T09:29:53.

And I've integrated this library into my Delphi XE3 test project

function _DateTimeToString(date: Pointer): Pointer; cdecl;  external
         'libTimeLib.dylib' name '_DateTimeToString';

function DateTimeToString(dt: TDateTime): string;
var
    date: NSDate;
    res: NSString;
begin
    date := TNSDate.Wrap(TNSDate.OCClass.date);

    res := TNSString.Wrap(_DateTimeToString((date as ILocalObject).GetObjectID));
    Result := string(res.UTF8String);
end;

And when I call it from my Delphi project it returns 05/15/13 whilst in XCode project it returns correct 2013-05-15T16:42:42 date.

Can someone explain why NSDateFormatter does not work in library which is used from Delphi application?

UPDATE

I've created this function in Delphi

function DateTimeToStringDelphi(): string;
var
    formatter: NSDateFormatter;
    stringDate: NSString;
begin
    formatter := TNSDateFormatter.Create();
    formatter.setDateFormat(NSSTR('YYYY-MM-dd HH:mm:ss'));

    stringDate := formatter.stringFromDate( TNSDate.Wrap(TNSDate.OCClass.date) );
    formatter.release;

    result := string(stringDate.UTF8String);
end;

And it also returns date in format 05/15/13, so it is not library problem.

Was it helpful?

Solution

Problem was with default NSDateFormatterBehaviour and has been fixed using this call

TNSDateFormatter.OCClass.setDefaultFormatterBehavior(NSDateFormatterBehavior10_4);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top