private long truncateCompoundPeriod(long t) {
      long current;
      if (t >= origin) {
        long next = origin;
        do {
          current = next;
          next = chronology.add(period, current, 1);
        } while (t >= next);
      } else {
        current = origin;
        do {
          current = chronology.add(period, current, -1);
        } while (t < current);
      }
      return current;
    }
UDFDateFloor.

Abstract class that converts a timestamp to a timestamp with a given granularity.
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDateFloor.java