Coverage Report - com.aragost.javahg.DateTime
 
Classes in this File Line Coverage Branch Coverage Complexity
DateTime
47%
17/36
50%
6/12
2.333
 
 1  
 package com.aragost.javahg;
 2  
 
 3  
 import java.util.Date;
 4  
 import java.util.TimeZone;
 5  
 
 6  
 /**
 7  
  * Represents a timestamp and a timezone offset
 8  
  */
 9  
 public class DateTime {
 10  
 
 11  
     /**
 12  
      * Timestamp in milliseconds since 1970
 13  
      */
 14  
     private long timestamp;
 15  
 
 16  
     /**
 17  
      * Timezone offset in milliseconds
 18  
      */
 19  
     private int tzOffset;
 20  
 
 21  
     /**
 22  
      * Construct a new DateTime with the specified timestamp and timezone
 23  
      * 
 24  
      * @param timestamp
 25  
      * @param tzOffset
 26  
      */
 27  115
     public DateTime(long timestamp, int tzOffset) {
 28  115
         this.timestamp = timestamp;
 29  115
         this.tzOffset = tzOffset;
 30  115
     }
 31  
 
 32  
     /**
 33  
      * Construct a new DateTime with the specified date and timezone
 34  
      * 
 35  
      * @param date
 36  
      * @param tz
 37  
      */
 38  0
     public DateTime(Date date, TimeZone tz) {
 39  0
         this.timestamp = date.getTime();
 40  0
         this.tzOffset = tz.getOffset(this.timestamp);
 41  0
     }
 42  
 
 43  
     /**
 44  
      * @deprecated Use constructor with tz argument
 45  
      * @param date
 46  
      */
 47  
     @Deprecated
 48  
     public DateTime(Date date) {
 49  0
         this(date, TimeZone.getDefault());
 50  0
     }
 51  
 
 52  
     /**
 53  
      * Create a new instance by parsing the given String.
 54  
      * <p>
 55  
      * The string should be in standard Mercurial format, i.e.
 56  
      * '<seconds since 1970> <timezone offset in seconds>'
 57  
      * 
 58  
      * @param string
 59  
      * @return
 60  
      */
 61  
     public static DateTime parse(String string) {
 62  4
         String[] parts = string.split(" ");
 63  4
         if (parts.length != 2) {
 64  0
             throw new IllegalArgumentException("Wrong date format: " + string);
 65  
         }
 66  4
         long millis = 1000L * Long.valueOf(parts[0]);
 67  4
         int timezoneOffset = 1000 * Integer.valueOf(parts[1]);
 68  4
         return new DateTime(millis, timezoneOffset);
 69  
     }
 70  
 
 71  
     /**
 72  
      * 
 73  
      * @return the timestamp as a java.util.Date object
 74  
      */
 75  
     public Date getDate() {
 76  0
         return new Date(this.timestamp);
 77  
     }
 78  
 
 79  
     /**
 80  
      * 
 81  
      * @return the time/zone formatted for Mercurial
 82  
      */
 83  
     public String getHgString() {
 84  6
         return "" + (this.timestamp / 1000) + " " + this.tzOffset / 1000;
 85  
     }
 86  
 
 87  
     @Override
 88  
     public String toString() {
 89  0
         return getHgString();
 90  
     }
 91  
 
 92  
     @Override
 93  
     public int hashCode() {
 94  0
         final int prime = 31;
 95  0
         int result = 1;
 96  0
         result = prime * result + (int) (timestamp ^ (timestamp >>> 32));
 97  0
         result = prime * result + tzOffset;
 98  0
         return result;
 99  
     }
 100  
 
 101  
     @Override
 102  
     public boolean equals(Object obj) {
 103  1
         if (this == obj)
 104  0
             return true;
 105  1
         if (obj == null)
 106  0
             return false;
 107  1
         if (getClass() != obj.getClass())
 108  0
             return false;
 109  1
         DateTime other = (DateTime) obj;
 110  1
         if (timestamp != other.timestamp)
 111  0
             return false;
 112  1
         if (tzOffset != other.tzOffset)
 113  0
             return false;
 114  1
         return true;
 115  
     }
 116  
 
 117  
 }