vpavkin / dtc
Showing 1 of 2 files from the diff.

@@ -5,8 +5,10 @@
Loading
5 5
6 6
import dtc._
7 7
8 +
import scala.language.implicitConversions
8 9
import scala.scalajs.js.Date
9 10
import scala.util.Try
11 +
import JSDateCompatibility._
10 12
11 13
/**
12 14
  * Mutability safe wrapper around plain JS Date.
@@ -15,35 +17,36 @@
Loading
15 17
  * to fit [[dtc.Local]] typeclass requirements.
16 18
  *
17 19
  * Supports only [[dtc.Local]] typeclass due to weak time-zone capabilities.
20 +
  *
21 +
  * Be aware of [[JSDateCompatibility]] conversions.
18 22
  */
19 23
class JSDate private(private val underlying: Date) {
20 24
21 25
  override def toString: String = underlying.toUTCString()
22 26
23 -
  def dayOfMonth: Int = underlying.getUTCDate().toInt
24 -
  def month: Int = (underlying.getUTCMonth() + 1).toInt
25 -
  def year: Int = underlying.getUTCFullYear().toInt
26 -
  def hour: Int = underlying.getUTCHours().toInt
27 -
  def minute: Int = underlying.getUTCMinutes().toInt
28 -
  def second: Int = underlying.getUTCSeconds().toInt
29 -
  def millisecond: Int = underlying.getUTCMilliseconds().toInt
27 +
  def dayOfMonth: Int = underlying.getUTCDate()
28 +
  def month: Int = underlying.getUTCMonth() + 1
29 +
  def year: Int = underlying.getUTCFullYear()
30 +
  def hour: Int = underlying.getUTCHours()
31 +
  def minute: Int = underlying.getUTCMinutes()
32 +
  def second: Int = underlying.getUTCSeconds()
33 +
  def millisecond: Int = underlying.getUTCMilliseconds()
30 34
31 -
  def dayOfWeek: DayOfWeek = DayOfWeek.of(dayOfWeekJSToJVM(underlying.getUTCDay().toInt))
35 +
  def dayOfWeek: DayOfWeek = DayOfWeek.of(dayOfWeekJSToJVM(underlying.getUTCDay()))
32 36
  def toLocalDate: LocalDate = LocalDate.of(year, month, dayOfMonth)
33 37
  def toLocalTime: LocalTime = LocalTime.of(hour, minute, second, millisToNanos(millisecond))
34 38
35 39
  def jsGetTime: Double = underlying.getTime()
36 40
37 -
  def withYear(year: Int): JSDate = updated(_.setUTCFullYear(yearD, monthD - 1,
38 -
    limitToLastDayOfMonth(dayOfMonth, year).toDouble))
41 +
  def withYear(year: Int): JSDate = updated(_.setUTCFullYear(year, month - 1, limitToLastDayOfMonth(dayOfMonth, year)))
39 42
40 43
  def withMonth(month: Int): JSDate =
41 -
    updated(_.setUTCMonth(monthD - 1, limitToLastDayOfMonth(dayOfMonth, forMonth = month).toDouble))
42 -
  def withDayOfMonth(dayOfMonth: Int): JSDate = updated(_.setUTCDate(dayOfMonthD))
43 -
  def withHour(hour: Int): JSDate = updated(_.setUTCHours(hourD, minuteD, secondD, millisecondD))
44 -
  def withMinute(minute: Int): JSDate = updated(_.setUTCMinutes(minuteD, secondD, millisecondD))
45 -
  def withSecond(second: Int): JSDate = updated(_.setUTCSeconds(secondD, millisecondD))
46 -
  def withMillisecond(millisecond: Int): JSDate = updated(_.setUTCMilliseconds(millisecondD))
44 +
    updated(_.setUTCMonth(month - 1, limitToLastDayOfMonth(dayOfMonth, forMonth = month)))
45 +
  def withDayOfMonth(dayOfMonth: Int): JSDate = updated(_.setUTCDate(dayOfMonth))
46 +
  def withHour(hour: Int): JSDate = updated(_.setUTCHours(hour, minute, second, millisecond))
47 +
  def withMinute(minute: Int): JSDate = updated(_.setUTCMinutes(minute, second, millisecond))
48 +
  def withSecond(second: Int): JSDate = updated(_.setUTCSeconds(second, millisecond))
49 +
  def withMillisecond(millisecond: Int): JSDate = updated(_.setUTCMilliseconds(millisecond))
47 50
48 51
  def millisecondsUntil(other: JSDate): Long = (other.jsGetTime - jsGetTime).toLong
49 52
  def secondsUntil(other: JSDate): Long = millisecondsUntil(other) / MillisInSecond
@@ -61,9 +64,9 @@
Loading
61 64
  def plusYears(n: Int): JSDate = {
62 65
    val newYear = year + n
63 66
    updated(_.setUTCFullYear(
64 -
      newYear.toDouble,
67 +
      newYear,
65 68
      underlying.getUTCMonth(),
66 -
      limitToLastDayOfMonth(dayOfMonth, forYear = newYear).toDouble
69 +
      limitToLastDayOfMonth(dayOfMonth, forYear = newYear)
67 70
    ))
68 71
  }
69 72
  def plusMillis(n: Long): JSDate = updatedRaw(_ + n)
@@ -91,16 +94,7 @@
Loading
91 94
    else thisDate.until(otherDate, units)
92 95
  }
93 96
94 -
  // Since Scala.js 1, JSDate works with Doubles instead io Ints.
95 -
  // See https://github.com/scala-js/scala-js/issues/2751
96 -
  // This is just to reduce amount of back and forth conversions between Int and Double
97 -
  private def dayOfMonthD: Double = dayOfMonth.toDouble
98 -
  private def monthD: Double = month.toDouble
99 -
  private def yearD: Double = year.toDouble
100 -
  private def hourD: Double = hour.toDouble
101 -
  private def minuteD: Double = minute.toDouble
102 -
  private def secondD: Double = second.toDouble
103 -
  private def millisecondD: Double = millisecond.toDouble
97 +
  private def unusedImportHack: Double = 1.toInt
104 98
105 99
}
106 100
@@ -130,7 +124,18 @@
Loading
130 124
    // see: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date#Two_digit_years_map_to_1900_-_1999
131 125
    // scalastyle:on
132 126
    if (date.getYear >= 0 && date.getYear <= 99)
133 -
      jsDate.setUTCFullYear(date.getYear.toDouble, date.getMonthValue.toDouble - 1, date.getDayOfMonth.toDouble)
127 +
      jsDate.setUTCFullYear(date.getYear, date.getMonthValue - 1, date.getDayOfMonth)
134 128
    new JSDate(jsDate)
135 129
  }
136 130
}
131 +
132 +
private[js] object JSDateCompatibility {
133 +
134 +
  /**
135 +
    * Since Scala.js 1, JSDate works with Doubles instead io Ints.
136 +
    * See https://github.com/scala-js/scala-js/issues/2751.
137 +
    * This is just to reduce amount of back and forth conversions between Int and Double.
138 +
    */
139 +
  implicit def doubleToInt(x: Double): Int = x.toInt
140 +
141 +
}
Files Coverage
cats/shared/src/main/scala/dtc/cats 0.00%
core 83.69%
examples/shared/src/main/scala/dtc/examples 0.00%
laws/shared/src/main/scala/dtc/laws 98.36%
moment/src/main/scala/dtc 81.50%
Project Totals (39 files) 77.73%
221.2
TRAVIS_OS_NAME=linux
openjdk8=
221.1
TRAVIS_OS_NAME=linux
openjdk8=
221.3
openjdk11=
TRAVIS_OS_NAME=linux
221.4
openjdk11=
TRAVIS_OS_NAME=linux

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading