//
//  NSObject+GMDate.m
//  Gengmei
//
//  Created by lizhen on 2019/2/15.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "NSObject+GMDate.h"

#include <assert.h>
#include <CoreServices/CoreServices.h>
#include <mach/mach.h>
#include <unistd.h>

@implementation NSObject (GMDate)

#pragma mark - NSDate转化为时间戳
- (CGFloat)getDateTime:(NSDate *)Date {
    //获取选择的时间戳
    NSDate *date = [Date dateByAddingTimeInterval:0];
    NSTimeInterval time = [date timeIntervalSince1970];// *1000 是精确到毫秒，不乘就是精确到秒
    return time;
}

#pragma mark - NSDate->时间字符串
- (NSString *)getDateToString:(NSDate *)Date {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设定时间格式,这里可以设置成自己需要的格式
    [formatter setDateFormat:@"yyyy/MM/dd"];
    NSString *time = [formatter stringFromDate:Date];
    return time;
}

#pragma mark - 时间戳转字符串
- (NSString *)getDateStringWithTimeStr:(float)time {
    // 时间戳转时间,时间戳为13位是精确到毫秒的，10位精确到秒
    NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *currentDateStr = [self getDateToString:detailDate];
    return currentDateStr;
}

#pragma mark - 字符串时间—>时间戳
- (CGFloat)timeStringToTimeFloat:(NSString *)time {
    // 转换为时间戳
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd"];
    NSDate *date = [formatter dateFromString:time];
    return [self getDateTime:date];
}

#pragma mark - 计算两个 NSDate 相差天数
- (NSString *)getTimeFrom:(NSDate *)fromDate to:(NSDate *)toDate {
    //计算两个中间差值(秒)
    NSTimeInterval time = [toDate timeIntervalSinceDate:fromDate];
    //开始时间和结束时间的中间相差的时间
    int days;
    days = ((int)time)/(3600*24);  //一天是24小时*3600秒
    NSString *dateValue = [NSString stringWithFormat:@"%i",days];
    return dateValue;
}

#pragma mark - 字符串转日期
- (NSDate *)getDateWithString:(NSString *)str {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy/MM/dd";
    [dateFormatter setMonthSymbols:[NSArray arrayWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12", nil]];
    NSDate * ValueDate = [dateFormatter dateFromString:str];
    return ValueDate;
}

#pragma mark - 时间戳->NSDate
- (NSDate *)getDateWithFloat:(float)time {
    if (time <= 0) {
        return nil;
    }
    NSString *timeStr = [self getDateStringWithTimeStr:time];
    NSDate *date = [self getDateWithString:timeStr];
    return date;
}

uint64_t GetPIDTimeInNanoseconds(uint64_t start,uint64_t end)
{
    uint64_t        elapsed;
    uint64_t        elapsedNano;
    static mach_timebase_info_data_t    sTimebaseInfo;

    // Start the clock.

//    start = mach_absolute_time();

    // Call getpid. This will produce inaccurate results because
    // we're only making a single system call. For more accurate
    // results you should call getpid multiple times and average
    // the results.

    (void) getpid();

    // Stop the clock.

//    end = mach_absolute_time();

    // Calculate the duration.

    elapsed = end - start;

    // Convert to nanoseconds.

    // If this is the first time we've run, get the timebase.
    // We can use denom == 0 to indicate that sTimebaseInfo is
    // uninitialised because it makes no sense to have a zero
    // denominator is a fraction.

    if ( sTimebaseInfo.denom == 0 ) {
        (void) mach_timebase_info(&sTimebaseInfo);
    }

    // Do the maths. We hope that the multiplication doesn't
    // overflow; the price you pay for working in fixed point.

    elapsedNano = elapsed * sTimebaseInfo.numer / sTimebaseInfo.denom;

    return elapsedNano/1000000;
}

@end
