对于表中某些时间戳,它的值很小,不超过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];
}