libs-base/Tests/base/NSDateFormatter/general.m
rfm 3fba03ba55 Important change to the START_SET and END_SET macros to stop their use
being confusing.  They now both take a simple C-string argument which
names the set, and the macros check that each end matches a start of
the same name.  Since tis means that a START_SET no longer takes an
argument sayng whether or notthe set is to be skipped, we now have a
SKIP macro to be used inside a set to skip to the end of it.  This
is actually more versatile as we can have multiple SKIP macros in the
same set, each providing a different reason for the set being skipped.
Also removed a few obsolete/unused functions and macros.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@32355 72102866-910b-0410-8b05-ffd578937521
2011-02-24 16:26:01 +00:00

82 lines
2.8 KiB
Objective-C

#import <Foundation/NSCalendar.h>
#import <Foundation/NSDate.h>
#import <Foundation/NSDateFormatter.h>
#import <Foundation/NSLocale.h>
#import <Foundation/NSTimeZone.h>
#import "Testing.h"
#if defined(GS_USE_ICU)
#define NSLOCALE_SUPPORTED GS_USE_ICU
#else
#define NSLOCALE_SUPPORTED 1 /* Assume Apple support */
#endif
int main(void)
{
NSDateFormatter *inFmt;
NSDateFormatter *outFmt;
NSDate *date;
NSString *str;
NSLocale *locale;
NSCalendar *cal;
unsigned int components;
NSInteger year;
START_SET("NSDateFormatter")
if (!NSLOCALE_SUPPORTED)
SKIP("NSLocale not supported\nThe ICU library was not available when GNUstep-base was built")
[NSTimeZone setDefaultTimeZone: [NSTimeZone timeZoneWithName: @"GMT"]];
inFmt = [[NSDateFormatter alloc] init];
[inFmt setDateFormat: @"yyyy-MM-dd 'at' HH:mm"];
date = [inFmt dateFromString: @"2011-01-27 at 17:36"];
outFmt = [[NSDateFormatter alloc] init];
[outFmt setLocale: [[NSLocale alloc] initWithLocaleIdentifier: @"pt_BR"]];
[outFmt setDateFormat: @"HH:mm 'on' EEEE MMMM d"];
str = [outFmt stringFromDate: date];
PASS_EQUAL(str, @"17:36 on quinta-feira janeiro 27",
"Output has the same format as Cocoa.")
RELEASE(outFmt);
RELEASE(inFmt);
locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
inFmt = [NSDateFormatter new];
[inFmt setDateStyle: NSDateFormatterShortStyle];
[inFmt setTimeStyle: NSDateFormatterNoStyle];
[inFmt setLocale: locale];
[inFmt setTimeZone: [NSTimeZone timeZoneWithName: @"GMT"]];
date = [inFmt dateFromString: @"15/06/1982"];
PASS_EQUAL([date description], @"1982-06-15 00:00:00 +0000",
"GMT time zone is correctly accounted for.");
[inFmt setTimeZone: [NSTimeZone timeZoneWithName: @"EST"]];
date = [inFmt dateFromString: @"15/06/1982"];
PASS_EQUAL([date description], @"1982-06-15 05:00:00 +0000",
"EST time zone is correctly accounted for.");
RELEASE(inFmt);
cal = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
[cal setTimeZone: [NSTimeZone timeZoneWithName: @"CST"]];
[cal setLocale: locale];
components = NSYearCalendarUnit;
year = [[cal components: components fromDate: date] year];
inFmt = [NSDateFormatter new];
[inFmt setLocale: locale];
[inFmt setDateStyle: NSDateFormatterLongStyle];
[inFmt setTimeStyle: NSDateFormatterNoStyle];
str = [inFmt stringFromDate: date];
PASS (year == 1982, "Year is 1982");
PASS_EQUAL(str, @"15 June 1982", "Date is formatted correctly.");
RELEASE(cal);
RELEASE(inFmt);
str = [NSDateFormatter dateFormatFromTemplate: @"MMMdd"
options: 0 locale: locale];
PASS_EQUAL(str, @"dd MMM", "Convert date format as Cocoa.");
RELEASE(locale);
END_SET("NSDateFormatter")
return 0;
}