Source

headstak / StakDB.m

Diff from to
 
 - (void)dumpStakToFileHandle:(NSFileHandle *)file {
     FMResultSet *fmr = [self executeQuery:@"SELECT * FROM stakitems\
-                        ORDER BY pushdate",nil];
+                                            ORDER BY pushdate",nil];
     while ([fmr next]) {
         NSDictionary *dict = [fmr resultDictionary];
-        NSDateFormatter *fmt = [[NSDateFormatter alloc] initWithDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
-                                                      allowNaturalLanguage:NO];
-        [fmt setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
-        NSString *pushTime = [[fmt stringFromDate:[dict objectForKey:@"pushdate"]] csvField];
-        NSString *popTime = [[fmt stringFromDate:[dict objectForKey:@"popdate"]] csvField];
-        NSString *ftype = [[self typeNameForId:[[dict objectForKey:@"id"] integerValue] ] csvField];
+        NSDateFormatter *fmt = [[NSDateFormatter alloc] init ];
+        [fmt setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
+        [fmt setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
+
+        // Convert the dates to CSVable strings
+        NSDate *popDate = [StakDB timestampFieldToDate:[dict objectForKey:@"popdate"]];
+        NSDate *pushDate = [StakDB timestampFieldToDate:[dict objectForKey:@"pushdate"]];
+        NSString *pushTime = (pushDate ? [[fmt stringFromDate:pushDate] csvField] : @"\"\"");
+        NSString *popTime = (popDate ? [[fmt stringFromDate:popDate] csvField] : @"\"\"");
+        
+        NSString *ftype = [[self typeNameForId:[[dict objectForKey:@"itemtype"] integerValue] ] csvField];
+        
+        // Write the item out to the file handle
         [file writeData:[[NSString stringWithFormat:@"%@,%@,%@,%@\n",
                 pushTime,
                 popTime,
-                [dict objectForKey:@"description"],
-                ftype] dataUsingEncoding:NSUTF8StringEncoding]];
+                ftype,
+                [dict objectForKey:@"description"]
+                ] dataUsingEncoding:NSUTF8StringEncoding]];
     }
 }
 
 - (void)hardDeleteStak {
     [self executeUpdate:
      @"DELETE FROM STAKITEMS",nil];
-    
+}
+
++ (NSDate *)timestampFieldToDate:(NSNumber *)timestamp {
+    if (![timestamp isEqualTo:[NSNull null]]) {
+        return [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
+    }
+    return nil;
 }
 @end