]> err.no Git - sope/blobdiff - sope-core/NGExtensions/FdExt.subproj/NSCalendarDate+misc.m
fixed a gstep-base issue
[sope] / sope-core / NGExtensions / FdExt.subproj / NSCalendarDate+misc.m
index 5327438b3e94c8417e3b0cb9351f832d19f7a49a..7e5f93ced72b8523f4204142ade16968ffb8a869 100644 (file)
@@ -1,26 +1,27 @@
 /*
-  Copyright (C) 2000-2004 SKYRIX Software AG
+  Copyright (C) 2000-2005 SKYRIX Software AG
 
-  This file is part of OpenGroupware.org.
+  This file is part of SOPE.
 
-  OGo is free software; you can redistribute it and/or modify it under
+  SOPE 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
+  SOPE 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
+  License along with SOPE; see the file COPYING.  If not, write to the
   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.
 */
 
 #include "NSCalendarDate+misc.h"
 #include "common.h"
+#include <math.h>
 
 #define NUMBER_OF_SECONDS_IN_DAY (24 * 60 * 60)
 
 }
 #endif
 
+/* Oct. 15, 1582 */
+#define IGREG (15+31L*(10+12L*1582))
+
+- (long)julianNumber {
+  long jul;
+  int  ja, jy, jm, year, month, day;
+
+  year  = [self yearOfCommonEra];
+  month = [self monthOfYear];
+  day   = [self dayOfMonth];
+  jy    = year;
+
+  if (jy == 0)
+    return 0;
+  if (jy < 0)
+    jy++;
+
+  if (month > 2)
+    jm = month + 1;
+  else {
+    jy--;
+    jm = month + 13;
+  }
+  
+  jul = (long) (floor(365.25 * jy) + floor(30.6001 * jm) + day + 1720995);
+  
+  if (day + 31L * (month + 12L * year) >= IGREG) {
+    ja   = (int)(0.01 * jy);
+    jul += 2 - ja + (int) (0.25 * ja);
+  }
+  return jul;
+}
+
++ (NSCalendarDate *)dateForJulianNumber:(long)_jn {
+  long     ja, jalpha, jb, jc, jd, je;
+  unsigned day, month, year;
+
+  if (_jn >= IGREG) {
+    jalpha = (long)(((float) (_jn - 1867216) - 0.25) / 36524.25);
+    ja = _jn + 1 + jalpha - (long)(0.25 * jalpha);
+  } else {
+    ja = _jn;
+  }
+
+  jb    = ja + 1524;
+  jc    = (long)(6680.0 + ((float)(jb - 2439870) - 122.1) / 365.25);
+  jd    = (long)(365 * jc + (0.25 * jc));
+  je    = (long)((jb - jd) / 30.6001);
+  day   = jb - jd - (long)(30.6001 * je);
+  month = je - 1;
+  if (month > 12)
+    month -= 12;
+  year = jc - 4715;
+  if (month > 2)
+    year--;
+  if (year <= 0)
+    year--;
+  return [NSCalendarDate dateWithYear:year month:month day:day
+                         hour:12 minute:0 second:0
+                         timeZone:nil];
+}
+
 @end /* NSCalendarDate(misc) */
 
+
+@implementation NSString(FuzzyDayOfWeek)
+
+- (int)dayOfWeekInEnglishOrGerman {
+  NSString *s;
+  unichar  c1;
+  unsigned len;
+  
+  if ((len = [self length]) == 0)
+    return -1;
+  
+  if (isdigit([self characterAtIndex:0]))
+    return [self intValue];
+
+  if (len < 2) /* need at least two chars */
+    return -1;
+  
+  s  = [self lowercaseString];
+  c1 = [s characterAtIndex:1];
+  switch ([s characterAtIndex:0]) {
+  case 'm': // Monday, Montag, Mittwoch
+    return (c1 == 'i') ? 3 /* Wednesday */ : 1 /* Monday */;
+
+  case 't': // Tuesday, Thursday
+    return (c1 == 'u') ? 2 /* Tuesday */ : 4 /* Thursday */;
+
+  case 'f': // Friday, Freitag
+    return 5 /* Friday */;
+
+  case 's': // Saturday, Sunday, Samstag, Sonntag, Sonnabend
+    if (c1 == 'a')
+      return 6; /* Saturday */
+    
+    if (c1 == 'o' && [s hasPrefix:@"sonna"])
+      return 6; /* Sonnabend */
+    
+    return 0 /* Sunday */;
+    
+  case 'w': // Wed
+    return 3 /* Wednesday */;
+  }
+  
+  return -1;
+}
+
+@end /* NSString(FuzzyDayOfWeek) */
+
+
 /* static linking */
 
 void __link_NSCalendarDate_misc(void) {