2016-04-01 20 views
0

Uygulamamın, kullanıcıların doğrudan müşteri hizmetleri temsilcilerine konuştuğu ve ofis saatlerinde yardıma başvurup yardım istemediklerini bildiren bir sohbet bileşeni vardır.Yerel NSDate saati ile PST çalışma saatlerini karşılaştırın

Ofis saatleri sabah saat 9'dan akşam 7'ye kadardır.

Şu anda ofisimin kapalı olması durumunda kullanıcıya bildiren bir bildirim görüntülemek için geçerli kodum var, ancak düzgün çalışmıyor. önemsediğiniz tüm bu sabah 9'dan ve 19:00 PST arasında şu anda var mı yoksa

- (void)checkOfficeHours { 

//set opening hours date 
NSDateComponents *openingTime = [[NSDateComponents alloc] init]; 
openingTime.hour = 9; 
openingTime.minute = 0; 

//set closing time hours 
NSDateComponents *closingTime = [[NSDateComponents alloc] init]; 
closingTime.hour = 19; 
closingTime.minute = 0; 

//get the pst time from local time 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[email protected]"hh:mm"; 
NSDate *currentDate = [NSDate date]; 
NSTimeZone *pstTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"PST"]; 
dateFormatter.timeZone = pstTimeZone; 
NSString *pstTimeString = [dateFormatter stringFromDate:currentDate]; 

//convert pst date string back to date 
NSDate *now = [dateFormatter dateFromString:pstTimeString]; 

//create the current date component 
NSDateComponents *currentTime = [[NSCalendar currentCalendar] components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:now]; 

//sort the array by times 
NSMutableArray *times = [@[openingTime, closingTime, currentTime] mutableCopy]; 
[times sortUsingComparator:^NSComparisonResult(NSDateComponents *t1, NSDateComponents *t2) { 
    if (t1.hour > t2.hour) { 
     return NSOrderedDescending; 
    } 

    if (t1.hour < t2.hour) { 
     return NSOrderedAscending; 
    } 
    // hour is the same 
    if (t1.minute > t2.minute) { 
     return NSOrderedDescending; 
    } 

    if (t1.minute < t2.minute) { 
     return NSOrderedAscending; 
    } 
    // hour and minute are the same 
    if (t1.second > t2.second) { 
     return NSOrderedDescending; 
    } 

    if (t1.second < t2.second) { 
     return NSOrderedAscending; 
    } 
    return NSOrderedSame; 

}]; 

//if the current time is in between (index == 1) then its during office hours 
if ([times indexOfObject:currentTime] == 1) { 
    NSLog(@"We are Open!"); 
    self.officeHoursView.hidden = YES; 
} else { 
    NSLog(@"Sorry, we are closed!"); 
    self.officeHoursView.hidden = NO; 
} 

}

cevap

1

, bir sürü daha kolay bundan daha yapabilirsiniz. Geçerli saat için PST'de bir NSDateComponents alın ve sonucun hour özelliğine bakın. Ayrıca hafta ya da diğer detaylar gün önem veriyorsanız

NSTimeZone *pst = [NSTimeZone timeZoneWithName:@"PST"]; 
NSDateComponents *pstComponentsForNow = [[NSCalendar currentCalendar] componentsInTimeZone:pst fromDate:[NSDate date]]; 

if ((pstComponentsForNow.hour >= 9) && (pstComponentsForNow.hour <= 19)) { 
    NSLog(@"Open"); 
} else { 
    NSLog(@"Closed"); 
} 

, NSDateComponents diğer özellikleri bak.

+0

Teşekkürler! Bu iş yapıyor gibi görünüyor! –

İlgili konular