/* 
   NSCalendarDateScannerHandler.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <stdio.h>

#include <Foundation/NSValue.h>
#include <Foundation/NSString.h>
#include <Foundation/NSCalendarDateScannerHandler.h>

@implementation NSCalendarDateScannerHandler

- init
{
    [super init];

    specHandler['a']
	    = [self methodForSelector:@selector(shortDayOfWeek:scanner:)];
    specHandler['A']
	    = [self methodForSelector:@selector(fullDayOfWeek:scanner:)];
    specHandler['b']
	    = [self methodForSelector:@selector(shortMonthOfYear:scanner:)];
    specHandler['B']
	    = [self methodForSelector:@selector(fullMonthOfYear:scanner:)];
    specHandler['c']
	    = [self methodForSelector:
			@selector(localeFormatForDateAndTime:scanner:)];
    specHandler['d']
	    = [self methodForSelector:@selector(decimalDayOfMonth:scanner:)];
    specHandler['H']
	    = [self methodForSelector:@selector(decimal24HourOfDay:scanner:)];
    specHandler['I']
	    = [self methodForSelector:@selector(decimal12HourOfDay:scanner:)];
    specHandler['j']
	    = [self methodForSelector:@selector(decimalDayOfYear:scanner:)];
    specHandler['m']
	    = [self methodForSelector:@selector(decimalMonthOfYear:scanner:)];
    specHandler['M']
	    = [self methodForSelector:@selector(decimalMinuteOfHour:scanner:)];
    specHandler['p']
	    = [self methodForSelector:@selector(AM_PM:scanner:)];
    specHandler['S']
	    = [self methodForSelector:
			@selector(decimalSecondOfMinute:scanner:)];
    specHandler['w']
	    = [self methodForSelector:@selector(decimalDayOfWeek:scanner:)];
    specHandler['x']
	    = [self methodForSelector:@selector(localeDate:scanner:)];
    specHandler['X']
	    = [self methodForSelector:@selector(localeTime:scanner:)];
    specHandler['y']
	    = [self methodForSelector:@selector(yearWithoutCentury:scanner:)];
    specHandler['Y']
	    = [self methodForSelector:@selector(yearWithCentury:scanner:)];
    specHandler['Z']
	    = [self methodForSelector:@selector(timeZoneName:scanner:)];
    specHandler['z']
	    = [self methodForSelector:
			@selector(timeZoneOffsetFromGMT:scanner:)];
    return self;
}

- initForCalendarDate:(NSCalendarDate*)_date
  timeZoneDetail:(NSTimeZoneDetail*)_detail
{
    [self init];
    date = _date;
    detail = _detail;

    year = [date yearOfCommonEra];
    month = [date monthOfYear];
    day = [date dayOfMonth];
    hour = [date hourOfDay];
    minute = [date minuteOfHour];
    second = [date secondOfMinute];
    weekDay = [date dayOfWeek];
    timeZoneOffset = [detail timeZoneSecondsFromGMT];

    return self;
}

- (NSString*)shortDayOfWeek:(va_list*)arg scanner:(FormatScanner*)scanner
{
    return [NSCalendarDate shortDayOfWeek:weekDay];
}

- (NSString*)fullDayOfWeek:(va_list*)arg scanner:(FormatScanner*)scanner
{
    return [NSCalendarDate fullDayOfWeek:weekDay];
}

- (NSString*)shortMonthOfYear:(va_list*)arg scanner:(FormatScanner*)scanner
{
    return [NSCalendarDate shortMonthOfYear:month];
}

- (NSString*)fullMonthOfYear:(va_list*)arg scanner:(FormatScanner*)scanner
{
    return [NSCalendarDate fullMonthOfYear:month];
}

- (NSString*)localeFormatForDateAndTime:(va_list*)arg
  scanner:(FormatScanner*)scanner
{
    return [NSCalendarDate descriptionForCalendarDate:date
			    withFormat:@"%X %x"
			    timeZoneDetail:detail locale:nil];
}

- (NSString*)decimalDayOfMonth:(va_list*)arg scanner:(FormatScanner*)scanner
{
    char buffer[5];

    sprintf (buffer, "%2d", day);
    return [NSString stringWithCString:buffer];
}

- (NSString*)decimal24HourOfDay:(va_list*)arg scanner:(FormatScanner*)scanner
{
    char buffer[5];

    sprintf (buffer, "%2d", hour);
    return [NSString stringWithCString:buffer];
}

- (NSString*)decimal12HourOfDay:(va_list*)arg scanner:(FormatScanner*)scanner
{
    char buffer[5];

    sprintf (buffer, "%2d", hour > 12 ? hour - 12 : hour);
    return [NSString stringWithCString:buffer];
}

- (NSString*)decimalDayOfYear:(va_list*)arg scanner:(FormatScanner*)scanner
{
    char buffer[5];

    sprintf (buffer, "%03d",
	     [NSCalendarDate decimalDayOfYear:year month:month day:day]);
    return [NSString stringWithCString:buffer];
}

- (NSString*)decimalMonthOfYear:(va_list*)arg scanner:(FormatScanner*)scanner
{
    char buffer[5];

    sprintf (buffer, "%02d", month);
    return [NSString stringWithCString:buffer];
}

- (NSString*)decimalMinuteOfHour:(va_list*)arg scanner:(FormatScanner*)scanner
{
    char buffer[5];

    sprintf (buffer, "%02d", minute);
    return [NSString stringWithCString:buffer];
}

- (NSString*)AM_PM:(va_list*)arg scanner:(FormatScanner*)scanner
{
    return hour > 12 ? @"PM" : @"AM";
}

- (NSString*)decimalSecondOfMinute:(va_list*)arg
  scanner:(FormatScanner*)scanner
{
    char buffer[5];

    sprintf (buffer, "%02d", second);
    return [NSString stringWithCString:buffer];
}

- (NSString*)decimalDayOfWeek:(va_list*)arg scanner:(FormatScanner*)scanner
{
    char buffer[5];

    sprintf (buffer, "%d", weekDay);
    return [NSString stringWithCString:buffer];
}

- (NSString*)localeDate:(va_list*)arg scanner:(FormatScanner*)scanner
{
    return [NSCalendarDate descriptionForCalendarDate:date
			    withFormat:@"%b %d %Y"
			    timeZoneDetail:detail locale:nil];
}

- (NSString*)localeTime:(va_list*)arg scanner:(FormatScanner*)scanner
{
    return [NSCalendarDate descriptionForCalendarDate:date
			    withFormat:@"%H:%M:%S %z"
			    timeZoneDetail:detail locale:nil];
}

- (NSString*)yearWithoutCentury:(va_list*)arg scanner:(FormatScanner*)scanner
{
    char buffer[5];

    sprintf (buffer, "%02d", year % 100);
    return [NSString stringWithCString:buffer];
}

- (NSString*)yearWithCentury:(va_list*)arg scanner:(FormatScanner*)scanner
{
    char buffer[5];

    sprintf (buffer, "%d", year);
    return [NSString stringWithCString:buffer];
}

- (NSString*)timeZoneName:(va_list*)arg scanner:(FormatScanner*)scanner
{
    return [detail timeZoneAbbreviation];
}

- (NSString*)timeZoneOffsetFromGMT:(va_list*)arg
  scanner:(FormatScanner*)scanner
{
    int i = timeZoneOffset > 0;
    int j = (timeZoneOffset > 0 ? timeZoneOffset : -timeZoneOffset) / 60;
    return [NSString stringWithFormat:@"GMT%s%02d%02d",
					i ? "+":"-", j / 60, j % 60];
}

@end /* NSCalendarDateScannerHandler */
