aboutsummaryrefslogtreecommitdiff
path: root/streaming/src/main/scala/spark/streaming/Time.scala
blob: 3c6fd5d9671612e52f95174827d092ffa7ee5ce4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package spark.streaming

/**
 * This is a simple class that represents time. Internally, it represents time as UTC.
 * The recommended way to create instances of Time is to use helper objects
 * [[spark.streaming.Milliseconds]], [[spark.streaming.Seconds]], and [[spark.streaming.Minutes]].
 * @param millis Time in UTC.
 */

case class Time(private val millis: Long) {
  
  def < (that: Time): Boolean = (this.millis < that.millis)

  def <= (that: Time): Boolean = (this.millis <= that.millis)

  def > (that: Time): Boolean = (this.millis > that.millis)
  
  def >= (that: Time): Boolean = (this.millis >= that.millis)

  def + (that: Time): Time = Time(millis + that.millis)
  
  def - (that: Time): Time = Time(millis - that.millis)
  
  def * (times: Int): Time = Time(millis * times)

  def / (that: Time): Long = millis / that.millis

  def floor(that: Time): Time = {
    val t = that.millis
    val m = math.floor(this.millis / t).toLong 
    Time(m * t)
  }

  def isMultipleOf(that: Time): Boolean = 
    (this.millis % that.millis == 0)

  def min(that: Time): Time = if (this < that) this else that

  def max(that: Time): Time = if (this > that) this else that

  def isZero: Boolean = (this.millis == 0)

  override def toString: String = (millis.toString + " ms")

  def toFormattedString: String = millis.toString
  
  def milliseconds: Long = millis
}

private[streaming] object Time {
  val zero = Time(0)

  implicit def toTime(long: Long) = Time(long)
}

/**
 * Helper object that creates instance of [[spark.streaming.Time]] representing
 * a given number of milliseconds.
 */
object Milliseconds {
  def apply(milliseconds: Long) = Time(milliseconds)
}

/**
 * Helper object that creates instance of [[spark.streaming.Time]] representing
 * a given number of seconds.
 */
object Seconds {
  def apply(seconds: Long) = Time(seconds * 1000)
}

/**
 * Helper object that creates instance of [[spark.streaming.Time]] representing
 * a given number of minutes.
 */
object Minutes {
  def apply(minutes: Long) = Time(minutes * 60000)
}