//
//  NSString+KLTDateString.m
//  KaiLiTe
//
//  Created by boosal on 2016/9/7.
//  Copyright © 2016年 boosal. All rights reserved.
//

#import "NSString+KLTDateString.h"

@implementation NSString (KLTDateString)

//NSDate     2016-09-07 02:32:37 +0000

+ (NSString *)getWholeStringWithDate:(NSDate *)date {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [formatter stringFromDate:date];
}

+ (NSString *)getDayStringWithDate:(NSDate *)date {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:@"MM-dd"];
    return [formatter stringFromDate:date];
}

+ (NSString *)getHourStringWithDate:(NSDate *)date {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:@"HH"];
    return [NSString stringWithFormat:@"%@:00",[formatter stringFromDate:date]];
}

+ (NSString *)getMinuteStringWithDate:(NSDate *)date {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:@"HH:mm"];
    return [formatter stringFromDate:date];
}


+ (NSDate *)getMinuteDateWithString:(NSString *)string {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:@"HH:mm"];
    return [formatter dateFromString:string];
}

+ (NSDate *)getHourDateWithString:(NSString *)string {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:@"HH:00"];
    return [formatter dateFromString:string];
}

+ (NSDate *)getDayDateWithString:(NSString *)string {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:@"MM-dd"];
    return [formatter dateFromString:string];
}

// 发射器发过来的日期格式：2016/10/20 15:18
+ (NSDate *)getDateTimeWithString:(NSString *)string {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    return [formatter dateFromString:string];
}


+ (NSDate *)getDateFromWholeString:(NSString *)whole {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [formatter dateFromString:whole];
}

+ (NSString *)getTimeStringWithCount:(int )count {
    NSString *h = [NSString stringWithFormat:@"%02d",count/3600 > 0 ? count/3600 : 0];
    NSString *m = [NSString stringWithFormat:@"%02d",(count - 3600 * [h intValue]) / 60 > 0 ? (count - 3600 * [h intValue]) / 60 : 0];
    NSString *s = [NSString stringWithFormat:@"%02d",(count - 3600 * [h intValue]) % 60 > 0 ? (count - 3600 * [h intValue]) % 60 : 0];
    return [NSString stringWithFormat:@"%@:%@:%@",h,m,s];
}

- (BOOL)klt_containsString:(NSString *)other {
    NSRange range = [self rangeOfString:other];
    return range.length != 0;
}

- (BOOL)klt_hasValidTransmit {
    NSInteger length = self.length;
    if (length >= 2) {
        NSString *lastHexString = [self substringFromIndex:length - 2];
        return [@"00" isEqualToString:lastHexString];
    }
    return NO;
}

- (NSString *)reverseToSting:(NSUInteger)numbers {
    NSInteger startIndex = self.length;
    if (numbers <= 0 || startIndex <= numbers) {
        return self;
    }
    
    NSString *reverseString = @"";
    while (startIndex >= numbers) {
        reverseString = [NSString stringWithFormat:@"%@%@", reverseString, [self substringWithRange:NSMakeRange(startIndex-numbers, numbers)]];
        startIndex -=numbers;
    }
    if (1 == startIndex) {
        reverseString = [NSString stringWithFormat:@"%@%@", reverseString, [self substringToIndex:startIndex]];
    }
    return reverseString;
}

+ (NSString *)getUTCFormatDate:(NSString *)localDate {
    NSDate *date = [self getDateFromWholeString:localDate];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    return dateString;
}

+ (NSString *)getLocalFromUTC:(NSString *)utc {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *ldate = [dateFormatter dateFromString:utc];
    NSString *local = [self getWholeStringWithDate:ldate];
    return local;
}

@end
