summaryrefslogtreecommitdiff
path: root/examples/scala-js/javalib/src/main/scala/java/util/Date.scala
blob: 8f4e85f564e5f16e3eee04ef1ef3705e66cdf6b5 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 * 2014 Matt Seddon
 * This code is donated in full to the scala-js project.
 */

package java.util

import scalajs.js

class Date private (private val date: js.Date) extends Object
    with Serializable with Cloneable with Comparable[Date] {

  import Date._

  def this() = this(new js.Date())

  @Deprecated
  def this(year: Int, month: Int, date: Int, hrs: Int, min: Int, sec: Int) = {
    this(new js.Date())
    this.date.setFullYear(1900 + year, month, date)
    this.date.setHours(hrs, min, sec, 0)
  }

  @Deprecated
  def this(year: Int, month: Int, date: Int, hrs: Int, min: Int) =
    this(year, month, date, hrs, min, 0)

  @Deprecated
  def this(year: Int, month: Int, date: Int) =
    this(year, month, date, 0, 0, 0)

  def this(date: Long) = this(new js.Date(date))

  @Deprecated
  def this(date: String) = this(new js.Date(date))

  def after(when: Date): Boolean = date.getTime() > when.date.getTime()

  def before(when: Date): Boolean = date.getTime() < when.date.getTime()

  override def clone(): Object = new Date(new js.Date(date.getTime()))

  override def compareTo(anotherDate: Date): Int =
    date.getTime().compareTo(anotherDate.date.getTime())

  override def equals(obj: Any): Boolean = obj match {
    case d: Date => d.date.getTime() == date.getTime()
    case _       => false
  }

  override def hashCode(): Int = date.getTime().hashCode()

  @Deprecated
  def getDate(): Int = date.getDate()

  @Deprecated
  def getDay(): Int = date.getDay()

  @Deprecated
  def getHours(): Int = date.getHours()

  @Deprecated
  def getMinutes(): Int = date.getMinutes()

  @Deprecated
  def getMonth(): Int = date.getMonth()

  @Deprecated
  def getSeconds(): Int = date.getSeconds()

  def getTime(): Long = date.getTime().toLong

  @Deprecated
  def getTimeZoneOffset(): Int = date.getTimezoneOffset()

  @Deprecated
  def getYear(): Int = date.getFullYear() - 1900

  @Deprecated
  def setDate(date: Int): Unit = this.date.setDate(date)

  @Deprecated
  def setHours(hours: Int): Unit = date.setHours(hours)

  @Deprecated
  def setMinutes(minutes: Int): Unit = date.setMinutes(minutes)

  @Deprecated
  def setMonth(month: Int): Unit = date.setMonth(month)

  @Deprecated
  def setSeconds(seconds: Int): Unit = date.setSeconds(seconds)

  def setTime(time: Long): Unit = date.setTime(time)

  @Deprecated
  def setYear(year: Int): Unit = date.setFullYear(1900 + year)

  @Deprecated
  def toGMTString(): String = {
    date.getUTCDate() + " " + Months(date.getUTCMonth()) + " " +
      date.getUTCFullYear() + " " + pad0(date.getUTCHours()) + ":" +
      pad0(date.getUTCMinutes()) + ":" +
      pad0(date.getUTCSeconds()) +" GMT"
  }

  @Deprecated
  def toLocaleString(): String = {
    date.getDate() + "-" + Months(date.getMonth()) + "-" +
      date.getFullYear() + "-" + pad0(date.getHours()) + ":" +
      pad0(date.getMinutes()) + ":" + pad0(date.getSeconds())
  }

  override def toString(): String = {
    val offset = -date.getTimezoneOffset()
    val sign = if(offset < 0) "-" else "+"
    val hours = pad0(Math.abs(offset) / 60)
    val mins = pad0(Math.abs(offset) % 60)
    Days(date.getDay()) + " "+ Months(date.getMonth()) + " " +
      pad0(date.getHours()) + ":" + pad0(date.getMinutes()) + ":" +
      pad0(date.getSeconds()) + " GMT" + sign + hours + mins + " " +
      date.getFullYear()
  }
}

object Date {
  private val Days = Array(
      "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")

  private val Months = Array(
      "Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

  private def pad0(i: Int): String = {
    val str = "" + i
    if (str.length < 2) "0" + str else str
  }

  @Deprecated
  def UTC(year: Int, month: Int, date: Int,
      hrs: Int, min: Int, sec: Int): Long =
    js.Date.UTC(year + 1900, month, date, hrs, min, sec).toLong

  @Deprecated
  def parse(string: String): Long =
    new Date(new js.Date(string)).getTime.toLong
}