/*
  Copyright (C) 2000-2003 SKYRIX Software AG

  This file is part of OGo

  OGo is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with OGo; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/
// $Id$

#include "NSCalendarDate+ICal.h"
#include "common.h"

static NSTimeZone *gmt = nil;
static inline void _setupGMT(void) {
  if (gmt == nil)
    gmt = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
}

@implementation NSCalendarDate(ICalValue)

/* represention */

static NSString *gmtcalfmt = @"%Y%m%dT%H%M00Z";

- (NSString *)icalStringInGMT {
  NSTimeZone *oldtz;
  NSString   *s;
  _setupGMT();
  
  /* set GMT as timezone */
  oldtz = [[self timeZone] retain];
  if (oldtz == gmt) {
    [oldtz release];
    oldtz = nil;
  }
  else {
    [self setTimeZone:gmt];
  }
  
  /* calc string */
  s = [self descriptionWithCalendarFormat:gmtcalfmt];
  
  /* restore old timezone */
  if (oldtz) {
    [self setTimeZone:oldtz];
    [oldtz release];
  }
  
  return s;
}

- (NSString *)icalStringWithTimeZone:(NSTimeZone *)_tz {
  _setupGMT();
  
  if (_tz == gmt || _tz == nil)
    return [self icalStringInGMT];
  else if ([_tz isEqual:gmt])
    return [self icalStringInGMT];
  else {
    /* not in GMT */
    NSLog(@"WARNING(%s): arbitary timezones not supported yet: %@",
          __PRETTY_FUNCTION__, _tz);
    return [self icalStringInGMT];
  }
}

- (NSString *)icalString {
  _setupGMT();
  return [self icalStringWithTimeZone:gmt];
}

@end /* NSDate(ICalValue) */
