Skip to content

Commit

Permalink
Merge pull request #628 from adzerk/time-utc-local-date-time
Browse files Browse the repository at this point in the history
Add `utcLocalDateTime` to `ApsoTimeDateTime`
  • Loading branch information
jcazevedo authored Feb 26, 2024
2 parents 8a1d18c + 561a6e9 commit 6448cdf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions time/src/main/scala/com/velocidi/apso/time/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ object Implicits {
*/
final implicit class ApsoTimeDateTime(val d1: DateTime) extends AnyVal {

/** Returns a `LocalDateTime` corresponding to this `DateTime` at UTC.
* @return
* a `LocalDateTime` corresponding to this `DateTime` at UTC.
*/
def utcLocalDateTime: LocalDateTime = d1.withZone(DateTimeZone.UTC).toLocalDateTime

/** Returns a `LocalDate` corresponding to this `DateTime` at UTC.
* @return
* a `LocalDate` corresponding to this `DateTime` at UTC.
Expand Down
10 changes: 10 additions & 0 deletions time/src/test/scala/com/velocidi/apso/time/ImplicitsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ class ImplicitsSpec extends Specification {

"An ApsoTimeDateTime" should {

"support conversion to UTC LocalDateTime" in {
val localDateTime = LocalDateTime.parse("2014-01-01T01:10:58")
val dateTime = localDateTime.toDateTime(DateTimeZone.forID("NZ"))
val estLocalDateTime = dateTime.toLocalDateTime
val utcLocalDateTime = dateTime.utcLocalDateTime

estLocalDateTime === new LocalDateTime(2014, 1, 1, 1, 10, 58)
utcLocalDateTime === new LocalDateTime(2013, 12, 31, 12, 10, 58)
}

"support conversion to UTC LocalDate" in {
val localDate = "2014-01-01".toLocalDate
val dateTime = localDate.toDateTimeAtStartOfDay.toDateTime(DateTimeZone.forID("EST"))
Expand Down

0 comments on commit 6448cdf

Please sign in to comment.