
com.couchbase.client.scala.util.DurationConversions.scala Maven / Gradle / Ivy
/*
* Copyright (c) 2019 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.couchbase.client.scala.util
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
import scala.language.implicitConversions
/** Functions to convert between Java and Scala Duration.
*
* @author Graham Pople
* @since 1.0.0
*/
private[client] object DurationConversions {
implicit def scalaDurationToJava(in: Duration): java.time.Duration = {
java.time.Duration.ofNanos(in.toNanos)
}
implicit def javaDurationToScala(in: java.time.Duration): Duration = {
scala.concurrent.duration.Duration(in.toNanos, TimeUnit.NANOSECONDS)
}
implicit class DurationOps(private val d: Duration) extends AnyVal {
def toN1qlFormat: String =
if (d.toSeconds > 0) d.toSeconds.toString + "s"
else d.toNanos.toString + "ns"
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy