Code example for Timestamp

Methods: getNanos, getTime, setNanos, setTime

0
   * 
   * @return seconds corresponding to this TimestampWritable 
   */ 
  public int getSeconds() { 
    if (bytesEmpty) {
      return (int) (timestamp.getTime() / 1000);
    } 
    return TimestampWritable.getSeconds(currentBytes, offset);
  } 
 
  /** 
   * 
   * @return nanoseconds in this TimestampWritable 
   */ 
  public int getNanos() { 
    if (!timestampEmpty) {
      return timestamp.getNanos();
    } 
 
    return TimestampWritable.getNanos(currentBytes, offset+4);
  } 
 
  /** 
   * 
   * @return length of serialized TimestampWritable data 
   */ 
  private int getTotalLength() { 
    return 4 + getDecimalLength(); 
  } 
 
  /** 
   * 
   * @return number of bytes the variable length decimal takes up 
   */ 
  private int getDecimalLength() { 
    checkBytes(); 
    return WritableUtils.decodeVIntSize(currentBytes[offset+4]);
  } 
 
  public Timestamp getTimestamp() {
    if (timestampEmpty) {
      populateTimestamp(); 
    } 
    return timestamp;
  } 
 
  /** 
   * Used to create copies of objects 
   * @return a copy of the internal TimestampWritable byte[] 
   */ 
  public byte[] getBytes() { 
    checkBytes(); 
 
    int len = getTotalLength();
    byte[] b = new byte[len];
 
    System.arraycopy(currentBytes, offset, b, 0, len);
    return b;
  } 
 
  /** 
   * @return byte[] representation of TimestampWritable that is binary 
   * sortable (4 byte seconds, 4 bytes for nanoseconds) 
   */ 
  public byte[] getBinarySortable() { 
    byte[] b = new byte[8];
    int nanos = getNanos();
    int seconds = HAS_DECIMAL_MASK | getSeconds();
    intToBytes(seconds, b, 0);
    intToBytes(nanos, b, 4);
    return b;
  } 
 
  /** 
   * Given a byte[] that has binary sortable data, initialize the internal 
   * structures to hold that data 
   * @param bytes 
   * @param offset 
   */ 
  public void setBinarySortable(byte[] bytes, int offset) {
    int seconds = bytesToInt(bytes, offset);
    int nanos = bytesToInt(bytes, offset+4);
    if (nanos == 0) {
      seconds &= NO_DECIMAL_MASK;
    } else { 
      seconds |= HAS_DECIMAL_MASK;
    } 
    intToBytes(seconds, internalBytes, 0);
    setNanosBytes(nanos, internalBytes, 4);
    currentBytes = internalBytes;
    this.offset = 0;
  } 
 
  /** 
   * The data of TimestampWritable can be stored either in a byte[] 
   * or in a Timestamp object. Calling this method ensures that the byte[] 
   * is populated from the Timestamp object if previously empty. 
   */ 
  private void checkBytes() { 
    if (bytesEmpty) {
      // Populate byte[] from Timestamp 
      convertTimestampToBytes(timestamp, internalBytes, 0);
      offset = 0;
      currentBytes = internalBytes;
      bytesEmpty = false;
    } 
  } 
 
  /** 
   * 
   * @return double representation of the timestamp, accurate to nanoseconds 
   */ 
  public double getDouble() { 
    double seconds, nanos;
    if (bytesEmpty) {
      seconds = timestamp.getTime() / 1000;
      nanos = timestamp.getNanos();
    } else { 
      seconds = getSeconds();
      nanos = getNanos();
    } 
    return seconds + ((double) nanos) / 1000000000;
  } 
 
 
 
  public void readFields(DataInput in) throws IOException {
    in.readFully(internalBytes, 0, 4);
    if (TimestampWritable.hasDecimal(internalBytes[0])) {
      in.readFully(internalBytes, 4, 1);
      int len = (byte) WritableUtils.decodeVIntSize(internalBytes[4]);
      in.readFully(internalBytes, 5, len-1);
    } 
    currentBytes = internalBytes;
    this.offset = 0;
  } 
 
  public void write(OutputStream out) throws IOException {
    checkBytes(); 
    out.write(currentBytes, offset, getTotalLength());
  } 
 
  public void write(DataOutput out) throws IOException {
    write((OutputStream) out);
  } 
 
  public int compareTo(TimestampWritable t) {
    checkBytes(); 
    int s1 = this.getSeconds();
    int s2 = t.getSeconds();
    if (s1 == s2) {
      int n1 = this.getNanos();
      int n2 = t.getNanos();
      if (n1 == n2) {
        return 0; 
      } 
      return n1 - n2;
    } else { 
      return s1 - s2;
    } 
  } 
 
  @Override 
  public boolean equals(Object o) {
    return compareTo((TimestampWritable) o) == 0;
  } 
 
  @Override 
  public String toString() {
    if (timestampEmpty) {
      populateTimestamp(); 
    } 
 
    String timestampString = timestamp.toString();
    if (timestampString.length() > 19) {
      if (timestampString.length() == 21) {
        if (timestampString.substring(19).compareTo(".0") == 0) {
          return dateFormat.format(timestamp);
        } 
      } 
      return dateFormat.format(timestamp) + timestampString.substring(19);
    } 
 
    return dateFormat.format(timestamp);
  } 
 
  @Override 
  public int hashCode() { 
    long seconds = getSeconds();
    seconds <<= 32;
    seconds |= getNanos();
    return (int) ((seconds >>> 32) ^ seconds);
  } 
 
  private void populateTimestamp() { 
    long seconds = getSeconds();
    int nanos = getNanos();
    timestamp.setTime(seconds * 1000);
    timestamp.setNanos(nanos);
  } 
 
  /** Static methods **/ 
 
  /**