Program: 15696
Project: 15696
Buggy source:
/** 
 * Calculate the number of crossed unit boundaries between two timestamps. This method is supported for MS SQL Server compatibility. <pre> DATEDIFF(YEAR, '2004-12-31', '2005-01-01') = 1 </pre>
 * @param part the part
 * @param d1 the first date
 * @param d2 the second date
 * @return the number of crossed boundaries
 */
private static long datediff(String part,Timestamp d1,Timestamp d2){
  int field=getDatePart(part);
  Calendar calendar=Calendar.getInstance();
  long t1=d1.getTime(), t2=d2.getTime();
  TimeZone zone=calendar.getTimeZone();
  calendar.setTime(d1);
  t1+=zone.getOffset(calendar.get(Calendar.ERA),calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.DAY_OF_WEEK),calendar.get(Calendar.MILLISECOND));
  calendar.setTime(d2);
  t2+=zone.getOffset(calendar.get(Calendar.ERA),calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.DAY_OF_WEEK),calendar.get(Calendar.MILLISECOND));
switch (field) {
case Calendar.MILLISECOND:
    return t2 - t1;
case Calendar.SECOND:
case Calendar.MINUTE:
case Calendar.HOUR_OF_DAY:
case Calendar.DAY_OF_YEAR:
case Calendar.WEEK_OF_YEAR:
{
    long hour=60 * 60 * 1000;
    long add=Math.min(t1 / hour * hour,t2 / hour * hour);
    t1-=add;
    t2-=add;
switch (field) {
case Calendar.SECOND:
      return t2 / 1000 - t1 / 1000;
case Calendar.MINUTE:
    return t2 / (60 * 1000) - t1 / (60 * 1000);
case Calendar.HOUR_OF_DAY:
  return t2 / hour - t1 / hour;
case Calendar.DAY_OF_YEAR:
return t2 / (hour * 24) - t1 / (hour * 24);
case Calendar.WEEK_OF_YEAR:
return t2 / (hour * 24 * 7) - t1 / (hour * 24 * 7);
default :
throw DbException.throwInternalError("String_Node_Str" + field);
}
}
case Calendar.DATE:
return t2 / (24 * 60 * 60* 1000) - t1 / (24 * 60 * 60* 1000);
default :
break;
}
calendar.setTimeInMillis(t1);
int year1=calendar.get(Calendar.YEAR);
int month1=calendar.get(Calendar.MONTH);
calendar.setTimeInMillis(t2);
int year2=calendar.get(Calendar.YEAR);
int month2=calendar.get(Calendar.MONTH);
int result=year2 - year1;
if (field == Calendar.MONTH) {
return 12 * result + (month2 - month1);
}
 else if (field == Calendar.YEAR) {
return result;
}
 else {
throw DbException.getUnsupportedException("String_Node_Str" + part);
}
}

Fixed version (ground truth):
/** 
 * Calculate the number of crossed unit boundaries between two timestamps. This method is supported for MS SQL Server compatibility. <pre> DATEDIFF(YEAR, '2004-12-31', '2005-01-01') = 1 </pre>
 * @param part the part
 * @param d1 the first date
 * @param d2 the second date
 * @return the number of crossed boundaries
 */
private static long datediff(String part,Timestamp d1,Timestamp d2){
  int field=getDatePart(part);
  Calendar calendar=Calendar.getInstance();
  long t1=d1.getTime(), t2=d2.getTime();
  TimeZone zone=calendar.getTimeZone();
  calendar.setTime(d1);
  t1+=zone.getOffset(calendar.get(Calendar.ERA),calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.DAY_OF_WEEK),calendar.get(Calendar.MILLISECOND));
  calendar.setTime(d2);
  t2+=zone.getOffset(calendar.get(Calendar.ERA),calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.DAY_OF_WEEK),calendar.get(Calendar.MILLISECOND));
switch (field) {
case Calendar.MILLISECOND:
    return t2 - t1;
case Calendar.SECOND:
case Calendar.MINUTE:
case Calendar.HOUR_OF_DAY:
case Calendar.DAY_OF_YEAR:
case Calendar.WEEK_OF_YEAR:
{
    long hour=60 * 60 * 1000;
    long add=Math.min(t1 / hour * hour,t2 / hour * hour);
    t1-=add;
    t2-=add;
switch (field) {
case Calendar.SECOND:
      return t2 / 1000 - t1 / 1000;
case Calendar.MINUTE:
    return t2 / (60 * 1000) - t1 / (60 * 1000);
case Calendar.HOUR_OF_DAY:
  return t2 / hour - t1 / hour;
case Calendar.DAY_OF_YEAR:
return t2 / (hour * 24) - t1 / (hour * 24);
case Calendar.WEEK_OF_YEAR:
return t2 / (hour * 24 * 7) - t1 / (hour * 24 * 7);
default :
throw DbException.throwInternalError("String_Node_Str" + field);
}
}
case Calendar.DATE:
return t2 / (24 * 60 * 60* 1000) - t1 / (24 * 60 * 60* 1000);
default :
break;
}
calendar.setTimeInMillis(t1);
int year1=calendar.get(Calendar.YEAR);
int month1=calendar.get(Calendar.MONTH);
calendar.setTimeInMillis(t2);
int year2=calendar.get(Calendar.YEAR);
int month2=calendar.get(Calendar.MONTH);
int yearResult=year2 - year1;
if (field == Calendar.MONTH) {
return 12 * yearResult + (month2 - month1);
}
 else if (field == Calendar.YEAR) {
return (12 * yearResult + (month2 - month1)) / 12;
}
 else {
throw DbException.getUnsupportedException("String_Node_Str" + part);
}
}