Revision: 28370
          http://sourceforge.net/p/bibdesk/svn/28370
Author:   hofman
Date:     2023-09-23 13:32:11 +0000 (Sat, 23 Sep 2023)
Log Message:
-----------
cache year and month rather than a date object

Modified Paths:
--------------
    trunk/bibdesk/BDSKFormatParser.m
    trunk/bibdesk/BibItem.h
    trunk/bibdesk/BibItem.m
    trunk/bibdesk/NSDate_BDSKExtensions.h
    trunk/bibdesk/NSDate_BDSKExtensions.m

Modified: trunk/bibdesk/BDSKFormatParser.m
===================================================================
--- trunk/bibdesk/BDSKFormatParser.m    2023-09-23 09:25:22 UTC (rev 28369)
+++ trunk/bibdesk/BDSKFormatParser.m    2023-09-23 13:32:11 UTC (rev 28370)
@@ -350,7 +350,8 @@
                        // year without century
                     NSString *yearString = [pub 
stringValueOfField:BDSKYearString];
                     if ([NSString isEmptyString:yearString] == NO) {
-                        yearString = [NSString stringWithFormat:@"%.2ld", 
(long)([yearString integerValue] % 100)];
+                        NSInteger y = [NSDate yearFromString:yearString];
+                        yearString = [NSString stringWithFormat:@"%.2ld", 
(long)(y % 100)];
                                                [parsedStr 
appendString:yearString];
                                        }
                                        break;
@@ -360,9 +361,7 @@
                        // year with century
                     NSString *yearString = [pub 
stringValueOfField:BDSKYearString];
                     if ([NSString isEmptyString:yearString] == NO) {
-                        NSInteger y = [yearString integerValue];
-                        if ([yearString length] <= 2)
-                            y += y < 50 ? 2000 : 1900;
+                        NSInteger y = [NSDate yearFromString:yearString];
                         yearString = [NSString stringWithFormat:@"%ld", 
(long)y];
                                                [parsedStr 
appendString:yearString];
                                        }

Modified: trunk/bibdesk/BibItem.h
===================================================================
--- trunk/bibdesk/BibItem.h     2023-09-23 09:25:22 UTC (rev 28369)
+++ trunk/bibdesk/BibItem.h     2023-09-23 13:32:11 UTC (rev 28370)
@@ -102,7 +102,8 @@
        NSString *pubType;
     NSMutableDictionary *pubFields;
     NSMutableDictionary *people;
-    NSDate *pubDate;
+    NSInteger month;
+    NSInteger year;
        NSDate *dateAdded;
        NSDate *dateModified;
        NSMutableDictionary *groups;

Modified: trunk/bibdesk/BibItem.m
===================================================================
--- trunk/bibdesk/BibItem.m     2023-09-23 09:25:22 UTC (rev 28369)
+++ trunk/bibdesk/BibItem.m     2023-09-23 13:32:11 UTC (rev 28370)
@@ -96,7 +96,6 @@
 
 @interface BibItem ()
 
-@property (nonatomic, retain) NSDate *date;
 @property (nonatomic, retain) NSDate *dateAdded;
 @property (nonatomic, retain) NSDate *dateModified;
 
@@ -198,8 +197,8 @@
 
 @implementation BibItem
 
-@synthesize owner, macroResolver, downloads, fileOrder, identifierURL, 
date=pubDate, dateAdded, dateModified, pubType, citeKey, hasBeenEdited, 
pubFields, searchScore, imported, itemIndex;
-@dynamic undoManager, localFiles, existingLocalFiles, remoteURLs, usedMacros, 
usedLocalMacros, allPeople, people, numberOfAuthors, pubAuthors, firstAuthor, 
secondAuthor, thirdAuthor, lastAuthor, bibTeXAuthorString, 
numberOfAuthorsOrEditors, pubAuthorsOrEditors, firstAuthorOrEditor, 
secondAuthorOrEditor, thirdAuthorOrEditor, lastAuthorOrEditor, crossrefParent, 
title, displayTitle, container, sortingTitle, sortingContainer, 
sortingBooktitle, rating, color, suggestedCiteKey, hasEmptyOrDefaultCiteKey, 
needsToAutogenerateCiteKey, canAutogenerateCiteKey, allFieldNames, 
searchIndexInfo, completionObject, bibTeXString, RISStringValue, MODSXMLValue, 
endNoteXMLValue, wordXMLValue, RSSStringValue, requiredFields, optionalFields, 
defaultFields, allFields, fields, urls, persons, fieldComponents, authors, 
editors, authorsOrEditors, keywords, currentDate, textSkimNotes, 
richTextSkimNotes, linkedText, remoteURL, localURL, localUrlPath, URLFields, 
skimNotesForLocalURL, bdskURL;
+@synthesize owner, macroResolver, downloads, fileOrder, identifierURL, 
dateAdded, dateModified, pubType, citeKey, hasBeenEdited, pubFields, 
searchScore, imported, itemIndex;
+@dynamic undoManager, localFiles, existingLocalFiles, remoteURLs, usedMacros, 
usedLocalMacros, allPeople, people, numberOfAuthors, pubAuthors, firstAuthor, 
secondAuthor, thirdAuthor, lastAuthor, bibTeXAuthorString, 
numberOfAuthorsOrEditors, pubAuthorsOrEditors, firstAuthorOrEditor, 
secondAuthorOrEditor, thirdAuthorOrEditor, lastAuthorOrEditor, crossrefParent, 
title, displayTitle, container, sortingTitle, sortingContainer, 
sortingBooktitle, rating, color, suggestedCiteKey, hasEmptyOrDefaultCiteKey, 
needsToAutogenerateCiteKey, canAutogenerateCiteKey, allFieldNames, 
searchIndexInfo, completionObject, bibTeXString, RISStringValue, MODSXMLValue, 
endNoteXMLValue, wordXMLValue, RSSStringValue, requiredFields, optionalFields, 
defaultFields, allFields, fields, urls, persons, fieldComponents, authors, 
editors, authorsOrEditors, keywords, currentDate, textSkimNotes, 
richTextSkimNotes, linkedText, remoteURL, localURL, localUrlPath, URLFields, 
skimNotesForLocalURL, bdskURL, date;
 
 + (void)initialize
 {
@@ -285,7 +284,9 @@
         [self setPubTypeString:type];
         [self setCiteKeyString: key ?: defaultCiteKey];
         
-        pubDate = nil;
+        year = 0;
+        month = 0;
+        
         dateAdded = nil;
         dateModified = nil;
                
@@ -387,7 +388,8 @@
             identifierURL = createUniqueURL();
             
             // these are set by updateMetadataForKey:
-            pubDate = nil;
+            year = 0;
+            month = 0;
             dateAdded = nil;
             dateModified = nil;
             
@@ -411,7 +413,7 @@
         [coder encodeObject:files forKey:@"files"];
         // Legacy, these are necessary for sharing with older versions of 
BibDesk
         [coder encodeObject:BDSKBibtexString forKey:@"fileType"];
-        [coder encodeObject:ensureCalendarDate(pubDate) forKey:@"pubDate"];
+        [coder encodeObject:ensureCalendarDate([self dateInheriting:NO]) 
forKey:@"pubDate"];
         [coder encodeObject:ensureCalendarDate(dateAdded) forKey:@"dateAdded"];
         [coder encodeObject:ensureCalendarDate(dateModified) 
forKey:@"dateModified"];
         [coder encodeBool:hasBeenEdited forKey:@"hasBeenEdited"];
@@ -434,7 +436,6 @@
        BDSKDESTROY(groups);
     BDSKDESTROY(pubType);
     BDSKDESTROY(citeKey);
-    BDSKDESTROY(pubDate);
     BDSKDESTROY(dateAdded);
     BDSKDESTROY(dateModified);
     BDSKDESTROY(fileOrder);
@@ -994,10 +995,13 @@
 - (NSDate *)dateInheriting:(BOOL)inherit{
     BibItem *parent;
        
-       if(inherit && pubDate == nil && (parent = [self crossrefParent])) {
+    if (year != 0)
+        return [NSDate dateWithMonth:month year:year];
+    
+    if (inherit && (parent = [self crossrefParent]))
                return [parent dateInheriting:NO];
-       }
-       return pubDate;
+       
+       return nil;
 }
 
 - (void)setPubType:(NSString *)newType{
@@ -1424,17 +1428,7 @@
     } else if([field isEqualToString: BDSKDateModifiedString]) {
         return [[self dateModified]shortDateDescription];
     } else if([field isEqualToString: BDSKPubDateString]) {
-        NSDate *date = [self date];
-        if(nil == date) 
-            return nil;
-        static NSDateFormatter *formatter = nil;
-        if (formatter == nil) {
-            formatter = [[NSDateFormatter alloc] init];
-            [formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
-            [formatter setLocale:[NSLocale currentLocale]]; // is this 
necessary?
-        }
-        [formatter setDateFormat:[NSString isEmptyString:[self 
valueOfField:BDSKMonthString]] ? @"yyyy" : @"MMM yyyy"];
-        return [formatter stringFromDate:date];
+        return year == 0 ? nil : [NSDate formattedDateForMonth:month 
year:year];
     } else if([field isEqualToString: BDSKFirstAuthorString]) {
         return [[self authorAtIndex:0] displayName];
     } else if([field isEqualToString: BDSKSecondAuthorString]){
@@ -4181,8 +4175,8 @@
     // pubDate is a derived field based on Month and Year fields; we take the 
15th day of the month to avoid edge cases
     if (key == nil || allFieldsChanged || [BDSKYearString isEqualToString:key] 
|| [BDSKMonthString isEqualToString:key]) {
         // allows month as number, name or abbreviated name
-        theDate = [NSDate dateWithMonthString:[pubFields 
objectForKey:BDSKMonthString] yearString:[pubFields 
objectForKey:BDSKYearString]];
-        [self setDate:theDate];
+        year = [NSDate yearFromString:[pubFields objectForKey:BDSKYearString]];
+        month = [NSDate monthFromString:[pubFields 
objectForKey:BDSKMonthString]];
        }
        
     // initially or when all fields are changed set the added and modified 
date based on the field values

Modified: trunk/bibdesk/NSDate_BDSKExtensions.h
===================================================================
--- trunk/bibdesk/NSDate_BDSKExtensions.h       2023-09-23 09:25:22 UTC (rev 
28369)
+++ trunk/bibdesk/NSDate_BDSKExtensions.h       2023-09-23 13:32:11 UTC (rev 
28370)
@@ -48,8 +48,10 @@
 
 @interface NSDate (BDSKExtensions)
 
++ (NSInteger)yearFromString:(NSString *)yearString;
 + (NSInteger)monthFromString:(NSString *)monthString;
-+ (NSDate *)dateWithMonthString:(NSString *)monthString yearString:(NSString 
*)yearString;
++ (NSDate *)dateWithMonth:(NSInteger)month year:(NSInteger)year;
++ (NSString *)formattedDateForMonth:(NSInteger)month year:(NSInteger)year;
 
 - (NSString *)dateDescription;
 - (NSString *)longDateDescription;

Modified: trunk/bibdesk/NSDate_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/NSDate_BDSKExtensions.m       2023-09-23 09:25:22 UTC (rev 
28369)
+++ trunk/bibdesk/NSDate_BDSKExtensions.m       2023-09-23 13:32:11 UTC (rev 
28370)
@@ -54,6 +54,24 @@
     return gregorianCalendar;
 }
 
+static NSCalendar *localGregorianCalendar() {
+    static NSCalendar *gregorianCalendar = nil;
+    if (gregorianCalendar == nil) {
+        gregorianCalendar = [[NSCalendar alloc] 
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
+        [gregorianCalendar setLocale:[NSLocale currentLocale]];
+    }
+    return gregorianCalendar;
+}
+
++ (NSInteger)yearFromString:(NSString *)yearString {
+    if([yearString isComplex])
+        yearString = [(BDSKStringNode *)[[yearString nodes] objectAtIndex:0] 
value];
+    NSInteger year = [yearString integerValue];
+    if ([yearString length] <= 2)
+        year += year < 50 ? 2000 : 1900;
+    return year;
+}
+
 + (NSInteger)monthFromString:(NSString *)monthString {
     if([monthString isComplex]) {
         BDSKStringNode *node = nil;
@@ -95,11 +113,9 @@
         if (i == NSNotFound) {
             i = [[[gregorianCalendar() monthSymbols] 
valueForKey:@"lowercaseString"] indexOfObject:monthString];
             if (i == NSNotFound && [[[NSLocale currentLocale] 
localeIdentifier] hasPrefix:@"en"] == NO) {
-                [gregorianCalendar() setLocale:[NSLocale currentLocale]];
-                i = [[[gregorianCalendar() shortMonthSymbols] 
valueForKey:@"lowercaseString"] indexOfObject:monthString];
+                i = [[[localGregorianCalendar() shortMonthSymbols] 
valueForKey:@"lowercaseString"] indexOfObject:monthString];
                 if (i == NSNotFound)
-                    i = [[[gregorianCalendar() monthSymbols] 
valueForKey:@"lowercaseString"] indexOfObject:monthString];
-                [gregorianCalendar() setLocale:[NSLocale 
localeWithLocaleIdentifier:@"en_US_POSIX"]];
+                    i = [[[localGregorianCalendar() monthSymbols] 
valueForKey:@"lowercaseString"] indexOfObject:monthString];
             }
         }
         if (i != NSNotFound)
@@ -108,25 +124,23 @@
     return month;
 }
 
-+ (NSDate *)dateWithMonthString:(NSString *)monthString yearString:(NSString 
*)yearString {
-    if([yearString isComplex])
-        yearString = [(BDSKStringNode *)[[yearString nodes] objectAtIndex:0] 
value];
-    if ([NSString isEmptyString:yearString])
-        return nil;
-    NSInteger year = [yearString integerValue];
-    if ([yearString length] <= 2)
-        year += year < 50 ? 2000 : 1900;
-    NSInteger month = [self monthFromString:monthString] ?: 1;
++ (NSDate *)dateWithMonth:(NSInteger)month year:(NSInteger)year {
     NSDateComponents *components = [[[NSDateComponents alloc] init] 
autorelease];
     [components setYear:year];
-    [components setMonth:month];
+    [components setMonth:month ?: 1];
     [components setDay:15];
     [components setHour:12];
-    [components setMonth:0];
+    [components setMinute:0];
     [components setSecond:0];
     return [gregorianCalendar() dateFromComponents:components];
 }
 
++ (NSString *)formattedDateForMonth:(NSInteger)month year:(NSInteger)year {
+    if (month > 0 && month <= 12)
+        return [NSString stringWithFormat:@"%ld %@", (long)year, 
[[localGregorianCalendar() shortMonthSymbols] objectAtIndex:month]];
+    return [NSString stringWithFormat:@"%ld", (long)year];
+}
+
 - (NSString *)dateDescription{
     // Saturday, March 24, 2001 (full date format)
     static NSDateFormatter *formatter = nil;

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to