iOS:时间戳 表示时间点&时间段

     对于表中某些时间戳,它的值很小,不超过86400(一天),它的意义表示一些时间点。

    

//---------------<span style="font-family: Arial, Helvetica, sans-serif;">workSchedule中-------------------</span>
    //sch.ClockOutTime 值为 39600.000000
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
	[outputFormatter setDateFormat:@"KK:mm a"];
    //outputFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	NSString *clockOutTimeStr = [outputFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:sch.ClockOutTime]];
    // clockOutTimeStr 显示为 7:30 PM(模拟器时区为Harbin)

备注:

--判断俩个时间点time1 和time2大小(早晚)

NSInteger secondForGMT = [[NSTimeZone systemTimeZone] secondsFromGMT];
    NSInteger secondFromDay = 24*60*60;
    double actualT1,actualT2;
    actualT1 = ((time1 + secondForGMT) >= secondFromDay) ? (time1 + secondForGMT - secondFromDay):(time1 + secondForGMT);
    actualT2 = ((time2 + secondForGMT) >= secondFromDay) ? (time2 + secondForGMT - secondFromDay):(time2 + secondForGMT);    
    if (actualT1 < actualT2) {  //注意三目运算符?,表达式3中secondForGMT别忘记加上
       ……
    }

========================================时间间距========================================

       对于某些时间戳,它的值很小,不超过86400(一天),看起来像是表示一段时间间距

//----------------review timecard中---------
    // sch.ClockOutTime 值为 39600.000000
    // currentTimeClockDateTime  转换NSDate,直接输出为:2014-07-04 07:15:15 +0000
    double currentDate = [[[NSDate dateWithTimeIntervalSince1970:currentTimeClockDateTime]getDateOnly] timeIntervalSince1970];
    double scheduledTime = [[[NSDate dateWithTimeIntervalSince1970:sch.ClockOutTime] getTimeOnly]timeIntervalSince1970]
    double scheduledClockOut = currentDate + scheduledTime;
    //currentDate   值为:1404403200.000000  转换NSDate,直接输出为:2014-07-03 16:00:00 +0000
    //scheduledTime 值为:39600.000000       转换NSDate,直接输出为:1970-01-01 11:00:00 +0000
    //相加后scheduledClockOut 值为:1404442800.000000    转换NSDate,直接输出为:2014-07-04 03:00:00 +0000    
    
    double graceBefore = scheduledClockOut - beforeMin * 60;
    double graceAfter = scheduledClockOut + afterMin * 60;
    //graceAfter  转换NSDate,直接输出为:2014-07-04 03:05:00 +0000
    
    if (currentTimeClockDateTime < graceBefore)
    {
        *scheduledDateTime = scheduledClockOut;
        return ClockOutNotOK_BeforeGracePeriod;
    }
    else if (currentTimeClockDateTime > graceAfter)
    {
        // 该if情况符合条件,
        // currentTimeClockDateTime 为2014-07-04 07:15:15 +0000
        // graceAfter 为 2014-07-04 03:05:00 +0000
        // 跟实际意义不符合
        *scheduledDateTime = scheduledClockOut;
        return ClockOutNotOK_AfterGracePeriod;
    }
    else
    {
        return ClockOutOK_WithinScheduleOrGrace;
    }
总结:

--上面2例中,前者当时间点用,clock out时间为7点30 PM,后者当时间间距用,表示11个小时,所以当当前时间为15:15:15(07:15:15 +8)时,操作clock out结果为AfterGracePeriod。与实际情况不符(还没超过7点30 PM)。

--对于时间戳,必须弄清楚是表示一段间距还是时间点。若是表示时间点,2例中scheduledClockOut不能有时间戳算术相加得到,必须由sch.ClockOutTime转换为本地段,再相加,或者俩个时间点拼成一个(见下例);如果是时间段,则1例中显示必须将NSDateFormatter时区设为GMT。

- (NSDate *)composeNewDateWithDateOnly:(NSDate *)dateOnly timeOnly:(NSDate *)timeOnly
{
    //currentDate 转换为NSDate为:2014-07-03 16:00:00 +0000
    //scheduledTime 转换为NSDate为:1970-01-01 11:00:00 +0000
    NSDateFormatter *formatter = [[NSDateFormatter alloc]  init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *newDateStr = [NSString stringWithFormat:@"%d-%d-%d %d:%d:%d", currentDate.year, currentDate.month, currentDate.day, scheduledTime.hour, scheduledTime.minute, scheduledTime.second];
    NSDate * scheduledClockOutDate = [formatter dateFromString:newDateStr];
    //newDateStr 输出为:2014-7-4 19:00:0
    //scheduledClockOutDate 输出为:2014-07-04 11:00:00 +0000
}
// NSDate的一些year、month、day……方法类似
- (NSInteger)year{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:unitFlags fromDate:self];
    return [comps year];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值