//
//  NSDate+currentDate.m
//  RDSmartPlug
//
//  Created by Rayson on 2020/5/28.
//  Copyright © 2020 RDIOT. All rights reserved.
//

#import "NSDate+currentDate.h"

@implementation NSDate (currentDate)

+ (NSDate *)currentDate{
    NSDate *date = [NSDate date];// NSDate默认相对的时区是GMT, 即: 格林威治标准时间
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval interval = [zone secondsFromGMTForDate:date];
    NSDate *current = [date dateByAddingTimeInterval:interval];
    return current;
}

// 时间戳—>字符串时间
+ (NSString *)rds_timeStringFromTimestamp:(NSString *)timestamp{
    //时间戳转时间的方法
    NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:[timestamp integerValue]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    NSString *strTime = [dateFormatter stringFromDate:timeDate];
    return strTime;
}

+ (NSString *)rds_timeStringFromNSDate:(NSDate *)date{
    
    if (date == nil) {
        return @"——";
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    if ([NSDate is12HourFormat]) {
        [dateFormatter setDateFormat:@"hh:mm a"];
    }else{
        [dateFormatter setDateFormat:@"HH:mm"];
    }
    
    NSString *strTime = [dateFormatter stringFromDate:date];
    return strTime;
}

+ (NSString *)rds_getDayString{
    NSArray * arrWeek=[NSArray arrayWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];
    NSDate *date = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday;
    comps = [calendar components:unitFlags fromDate:date];
    int week = (int)[comps weekday];
    int month = (int)[comps month];
    int day = (int)[comps day];
    
    NSString *w = [arrWeek objectAtIndex:week-1];
    NSString *time = [NSString stringWithFormat:@"%d.%d %@", month,day,w];

    return time;
}

+ (NSDate *)rds_dateFromTimeString:(NSString *)timeString{
    
    timeString = [NSString stringWithFormat:@"%@:00",timeString];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    NSDate *date = [dateFormatter dateFromString:timeString];
    return date;
}

+ (NSDate *)rds_dateFromTimestamp:(NSString *)timestamp{
    //时间戳转时间的方法
    NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:[timestamp integerValue]];
    return timeDate;
}

+ (BOOL)isSameDay:(NSDate *)date1 date2:(NSDate *)date2{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    calendar.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear ;
    
    //1.
    NSDateComponents *date1Cmps = [calendar components:unit fromDate:date1];
    
    //2.
    NSDateComponents *date2Cmps = [calendar components:unit fromDate:date2];
    
    BOOL resule = (date1Cmps.year == date2Cmps.year) && (date1Cmps.month == date2Cmps.month) && (date1Cmps.day == date2Cmps.day);
    return resule;
    
}

+ (BOOL)isSameMinute:(NSDate *)date1 date2:(NSDate *)date2{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitHour | NSCalendarUnitMinute;
    calendar.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    if (![NSDate isSameDay:date1 date2:date2]) {
        return NO;
    }
    
    //1.
    NSDateComponents *date1Cmps = [calendar components:unit fromDate:date1];
    
    //2.
    NSDateComponents *date2Cmps = [calendar components:unit fromDate:date2];
    
    return (date1Cmps.hour == date2Cmps.hour) && (date1Cmps.minute == date2Cmps.minute);
}

+ (BOOL)isDateInToday:(NSDate *)date{
    if(!date) return NO;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    calendar.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    BOOL today = [calendar isDateInToday:date];
    return today;
}

+ (NSString *)rds_getTimestamp:(NSDate *)date{
    
    int second = [date timeIntervalSince1970];
    NSString *timestamp = [NSString stringWithFormat:@"%d", second];
    
    return timestamp;
}


+ (NSString *)rds_getZeroTime:(NSDate *)date{
    
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [greCalendar setTimeZone: timeZone];

    NSDateComponents *dateComponents = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:date];

    //  定义一个NSDateComponents对象，设置一个时间点
    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
    [dateComponentsForDate setDay:dateComponents.day];
    [dateComponentsForDate setMonth:dateComponents.month];
    [dateComponentsForDate setYear:dateComponents.year];
    [dateComponentsForDate setHour:0];
    [dateComponentsForDate setMinute:0];
    [dateComponentsForDate setSecond:0];

    NSDate *resultDate = [greCalendar dateFromComponents:dateComponentsForDate];
    
    int second = [resultDate timeIntervalSince1970];
    NSString *timestamp = [NSString stringWithFormat:@"%d", second];
    
    return timestamp;
}

+ (NSString *)rds_getEndTime:(NSDate *)date{
    
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [greCalendar setTimeZone: timeZone];

    NSDateComponents *dateComponents = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:date];

    //  定义一个NSDateComponents对象，设置一个时间点
    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
    [dateComponentsForDate setDay:dateComponents.day];
    [dateComponentsForDate setMonth:dateComponents.month];
    [dateComponentsForDate setYear:dateComponents.year];
    [dateComponentsForDate setHour:23];
    [dateComponentsForDate setMinute:59];
    [dateComponentsForDate setSecond:59];

    NSDate *resultDate = [greCalendar dateFromComponents:dateComponentsForDate];
    
    int second = [resultDate timeIntervalSince1970];
    NSString *timestamp = [NSString stringWithFormat:@"%d", second];
    
    return timestamp;
}


+ (NSDate *)rds_firstDayOfMonth:(NSDate *)date{
    
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *componentsCurrentDate =[calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    
    NSDateComponents *componentsNewDate = [NSDateComponents new];
    
    componentsNewDate.year = componentsCurrentDate.year;
    componentsNewDate.month = componentsCurrentDate.month;
    componentsNewDate.day = 1;
    
    return [calendar dateFromComponents:componentsNewDate];
}


+ (NSDate *)rds_lastDayOfMonth:(NSDate *)date{
    
    NSInteger lastDay = [NSDate countThisMonthDays:date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *componentsCurrentDate =[calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    
    NSDateComponents *componentsNewDate = [NSDateComponents new];
    
    componentsNewDate.year = componentsCurrentDate.year;
    componentsNewDate.month = componentsCurrentDate.month;
    componentsNewDate.day = lastDay;
    
    return [calendar dateFromComponents:componentsNewDate];
}


+ (NSInteger)countThisMonthDays:(NSDate *)date{
    NSCalendar * calendar = [NSCalendar currentCalendar];
    if (isEmptyString(date)) {
        date = [NSDate date];
    }
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return range.length;
}


+ (BOOL)is12HourFormat{
    NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
    NSRange containsA =[formatStringForHours rangeOfString:@"a"];
    BOOL hasAMPM =containsA.location != NSNotFound;
    return hasAMPM;
}



- (NSString *)rds_getMinute{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:self];
    NSInteger hour = [components hour];
    NSInteger minute = [components minute];
    
    NSString *min = [NSString stringWithFormat:@"%ld", hour*60 + minute];
    return min;
}

+ (NSString *)rds_getTimeStringFromMinute:(NSString *)min{
    int h = min.intValue/60;
    int m = min.intValue%60;
    NSString *time = [NSString stringWithFormat:@"%02d:%02d", h, m];
    return time;
}
@end
