[futurebasic] re: Fuzzy Date Finder (Cocoa embedded in FB)

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2009 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Mon, 13 Jul 2009 08:23:04 -0400
Herbie noted:

> The very Cocoa method doesn't parse expression #13 (here). The
> remaining results appear correct but they are _not_ returned in the
> (German) date format specified in my OS X International System
> Preferences but in a common US date format: month/day/shortyear.



Herbie,

Ich entschuldige mich.

Expression 13 was the "bogus" one I mentioned that was added show the  
limitations of the method.

I have rewritten the Cocoa code to allow for more robust  
localization. Unfortunately, the method only accepts English language  
input. You may like to experiment with different date formats, or  
customizing your own,  in this section of the code:

//[ dateFormat setDateFormat: @"EEEE MMMM d, YYYY" ];
[ dateFormat setDateStyle: NSDateFormatterLongStyle ];
//[ dateFormat setDateStyle: NSDateFormatterMediumStyle ];
//[ dateFormat setDateStyle: NSDateFormatterShortStyle ];

At a minimum we get to play with a little Cocoa in our FB.

Ken


output file "Fuzzy Date Finder Localized"

include "Util_Containers.incl"

begin globals
dim as Container gC
end globals

#if def _PASSTHROUGHFUNCTION
#import <Cocoa/Cocoa.h>

CFStringRef FuzzyDateFinder( Str255 fuzzyDate )
{
CFStringRef queryRef, outDate;

// Convert Pascal string to a CFString
queryRef = CFStringCreateWithPascalString( kCFAllocatorDefault,
fuzzyDate, kCFStringEncodingMacRoman );

// Cast CFString as NSString and create date object
NSDate *date = [ NSDate dateWithNaturalLanguageString:
(NSString*)queryRef ];

// Done with this, empty the trash...
CFRelease( queryRef );

// Set system behavior
[NSDateFormatter setDefaultFormatterBehavior:
NSDateFormatterBehavior10_4 ];

// Create date formatter
NSDateFormatter *dateFormat = [[ NSDateFormatter alloc ] init] ;

// Set custom style of formatted date, or select defaults
//[ dateFormat setDateFormat: @"EEEE MMMM d, YYYY" ];
[ dateFormat setDateStyle: NSDateFormatterLongStyle ];
//[ dateFormat setDateStyle: NSDateFormatterMediumStyle ];
//[ dateFormat setDateStyle: NSDateFormatterShortStyle ];

/*
Set the formatter locale.

This example uses German: "de-DE"

Others include:
French: "fr-FR"
Japanese: "ja_JP",
English-US: "en-US
English-GB: "en-GB"
Italian: "it-IT", etc.
*/
[ dateFormat setLocale: [[[ NSLocale alloc ]
initWithLocaleIdentifier: @"de-DE" ] autorelease ]];

// Create date string from formatter
NSString *dateString = [ dateFormat stringFromDate: date ] ;

// Done with this, empty the trash...
[dateFormat release];

outDate = (CFStringRef) dateString;

return( outDate );
}
#endif


toolbox fn FuzzyDateFinder( Str255 fuzzyDate ) = CFStringRef

window 1, "Fuzzy Date Finder", ( 0, 0 )-( 420, 600 )
text _courier, 12

_arrayElements = 22

dim as Str255    sampleStrings( _arrayElements )
dim as CFStringRef tempCFRef
dim as Long       i

sampleStrings(0) = "Yesterday"
sampleStrings(1) = "today"
sampleStrings(2) = "Tomorrow"
sampleStrings(3) = "last month"
sampleStrings(4) = "Next month"
sampleStrings(5) = "Third Friday of this month"
sampleStrings(6) = "last Tuesday at dinner"
sampleStrings(7) = "3pm December 31, 2001"
sampleStrings(8) = "12/31/09"
sampleStrings(9) = "Today next year"
sampleStrings(10) = "last monday 00:00"
sampleStrings(11) = "Noon next Friday"
sampleStrings(12) = "1-5-92"
sampleStrings(13) = "29/11/10"
sampleStrings(14) = "28 Feb 07"
sampleStrings(15) = "Wednesday at 12am"
sampleStrings(16) = "Saturday morning"
sampleStrings(17) = "Lunch next week"
sampleStrings(18) = "2008-11-01 17:00:00 -0500"
sampleStrings(19) = "2011"
sampleStrings(20) = "yesterday 5pm"
sampleStrings(21) = "February 22, 2013"

print
for i = 0 to _arrayElements
gC = " "
long if ( len( sampleStrings(i) ))
tempCFRef = fn FuzzyDateFinder( sampleStrings(i) )
long if ( tempCFRef )
fn ContainerCreateWithCFString( tempCFRef, gC )
CFRelease( tempCFRef )
print i; ". "; sampleStrings(i); " is "; gC
print
xelse
print i; ". [Sorry, could not parse this phrase]"
print
end if
xelse
exit for
end if
next i

local fn DoDialog
if dialog(0) = _wndClose then end
end fn

on dialog fn DoDialog

do
HandleEvents
until ( gFBQuit )