Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Switch from joda-time's DateTime to java.time.ZonedDateTime #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ val s3obj: Option[S3Object] = bucket.getObject("sample.txt")

s3obj.foreach { obj =>
obj.publicUrl // http://unique-name-xxx.s3.amazonaws.com/sample.txt
obj.generatePresignedUrl(DateTime.now.plusMinutes(10)) // ?Expires=....
obj.generatePresignedUrl(DateTime.now().plusMinutes(10)) // ?Expires=....
bucket.delete(obj) // or obj.destroy()
}
```
Expand Down
2 changes: 0 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ lazy val core = project
moduleName := "awscala-core",
libraryDependencies ++= Seq(
"com.amazonaws" % "aws-java-sdk-core" % awsJavaSdkVersion,
"joda-time" % "joda-time" % "2.10.10",
"org.joda" % "joda-convert" % "2.2.1",
"org.scala-lang.modules" %% "scala-collection-compat" % "2.4.3",
"org.bouncycastle" % "bcprov-jdk16" % "1.46" % "provided",
"ch.qos.logback" % "logback-classic" % "1.2.3" % "test",
Expand Down
23 changes: 16 additions & 7 deletions core/src/main/scala/awscala/DateTime.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package awscala

import org.joda.time.format.DateTimeFormatter
import java.time.chrono.Chronology
import java.time.format.DateTimeFormatter

object DateTime {
import org.joda.time.{ DateTime => Joda, _ }
import java.time._

def now() = Joda.now()
def now(zone: DateTimeZone) = Joda.now(zone)
def now(chronology: Chronology) = Joda.now(chronology)
private[this] val UTC = ZoneId.of("UTC")

def parse(str: String) = Joda.parse(str)
def parse(str: String, formatter: DateTimeFormatter) = Joda.parse(str, formatter)
def apply(date: java.util.Date): ZonedDateTime =
ZonedDateTime.ofInstant(date.toInstant, UTC)
def toDate(dateTime: ZonedDateTime): java.util.Date =
java.util.Date.from(dateTime.toInstant)

def now(): ZonedDateTime = ZonedDateTime.now()
def now(zone: ZoneId): ZonedDateTime = ZonedDateTime.now(zone)
def now(chronology: Chronology): ZonedDateTime =
ZonedDateTime.ofInstant(chronology.zonedDateTime(Instant.now()).toInstant, UTC)

def parse(str: String): ZonedDateTime = ZonedDateTime.parse(str)
def parse(str: String, formatter: DateTimeFormatter): ZonedDateTime = ZonedDateTime.parse(str, formatter)

}

2 changes: 1 addition & 1 deletion core/src/main/scala/awscala/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package object awscala {
// Workaround for https://issues.scala-lang.org/browse/SI-7139
val Region: Region0.type = Region0
type Region = com.amazonaws.regions.Region
type DateTime = org.joda.time.DateTime
type DateTime = java.time.ZonedDateTime
type ByteBuffer = java.nio.ByteBuffer
type File = java.io.File

Expand Down
17 changes: 9 additions & 8 deletions dynamodb/src/main/scala/awscala/dynamodbv2/TableMeta.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package awscala.dynamodbv2

import awscala.DateTime.toDate
import awscala._
import com.amazonaws.services.{ dynamodbv2 => aws }

Expand All @@ -22,7 +23,7 @@ object TableMeta {
indexes.asScala.map(i => LocalSecondaryIndexMeta(i))
}.getOrElse(Nil).toSeq,
provisionedThroughput = ProvisionedThroughputMeta(t.getProvisionedThroughput),
createdAt = new DateTime(t.getCreationDateTime),
createdAt = DateTime(t.getCreationDateTime),
billingModeSummary = Option(t.getBillingModeSummary).map(BillingModeSummary.apply))
}

Expand Down Expand Up @@ -50,7 +51,7 @@ case class TableMeta(
billingMode = billingModeSummary.map(_.billingMode).map(aws.model.BillingMode.fromValue))

setAttributeDefinitions(attributes.map(_.asInstanceOf[aws.model.AttributeDefinition]).asJava)
setCreationDateTime(createdAt.toDate)
setCreationDateTime(toDate(createdAt))
setItemCount(itemCount)
setKeySchema(keySchema.map(_.asInstanceOf[aws.model.KeySchemaElement]).asJava)
setGlobalSecondaryIndexes(globalSecondaryIndexes.asJava)
Expand Down Expand Up @@ -89,8 +90,8 @@ object ProvisionedThroughputMeta {
numberOfDecreasesToday = p.getNumberOfDecreasesToday,
readCapacityUnits = p.getReadCapacityUnits,
writeCapacityUnits = p.getWriteCapacityUnits,
lastDecreasedAt = new DateTime(p.getLastDecreaseDateTime),
lastIncreasedAt = new DateTime(p.getLastIncreaseDateTime))
lastDecreasedAt = DateTime(p.getLastDecreaseDateTime),
lastIncreasedAt = DateTime(p.getLastIncreaseDateTime))
}
case class ProvisionedThroughputMeta(
numberOfDecreasesToday: Long,
Expand All @@ -99,8 +100,8 @@ case class ProvisionedThroughputMeta(
lastDecreasedAt: DateTime,
lastIncreasedAt: DateTime) extends aws.model.ProvisionedThroughputDescription {

setLastDecreaseDateTime(lastDecreasedAt.toDate)
setLastIncreaseDateTime(lastIncreasedAt.toDate)
setLastDecreaseDateTime(toDate(lastDecreasedAt))
setLastIncreaseDateTime(toDate(lastIncreasedAt))
setNumberOfDecreasesToday(numberOfDecreasesToday)
setReadCapacityUnits(readCapacityUnits)
setWriteCapacityUnits(writeCapacityUnits)
Expand All @@ -109,12 +110,12 @@ case class ProvisionedThroughputMeta(
object BillingModeSummary {
def apply(p: aws.model.BillingModeSummary): BillingModeSummary = new BillingModeSummary(
billingMode = p.getBillingMode,
lastUpdateToPayPerRequestDateTime = new DateTime(p.getLastUpdateToPayPerRequestDateTime))
lastUpdateToPayPerRequestDateTime = DateTime(p.getLastUpdateToPayPerRequestDateTime))
}
case class BillingModeSummary(
billingMode: String,
lastUpdateToPayPerRequestDateTime: DateTime) extends aws.model.BillingModeSummary {

setBillingMode(billingMode)
setLastUpdateToPayPerRequestDateTime(lastUpdateToPayPerRequestDateTime.toDate)
setLastUpdateToPayPerRequestDateTime(toDate(lastUpdateToPayPerRequestDateTime))
}
6 changes: 5 additions & 1 deletion emr/src/main/scala/awscala/emr/EMR.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package awscala.emr

import awscala._

import scala.jdk.CollectionConverters._
import scala.concurrent.duration._
import scala.language.postfixOps
import com.amazonaws.auth.AWSCredentialsProvider
import com.amazonaws.services.{ elasticmapreduce => aws }
import aws.model._
import awscala.DateTime.toDate

import java.time.temporal.ChronoUnit

object EMR {
def apply(credentials: Credentials)(implicit region: Region): EMR = new EMRClient(BasicCredentialsProvider(credentials.getAWSAccessKeyId, credentials.getAWSSecretKey)).at(region)
Expand Down Expand Up @@ -215,7 +219,7 @@ trait EMR extends aws.AmazonElasticMapReduce {
def runningClusters() = clusters(Seq("RUNNING"))

def recentClusters(duration: Duration = 1 hour) =
clusterSummaries(Nil, None, Some(new DateTime().minusMillis(duration.toMillis.toInt).toDate())).toList.sortBy(x => x.getStatus().getTimeline().getCreationDateTime()) map { x => toCluster(x) }
clusterSummaries(Nil, None, Some(toDate(DateTime.now().minus(duration.toMillis, ChronoUnit.MILLIS)))).toList.sortBy(x => x.getStatus().getTimeline().getCreationDateTime()) map { x => toCluster(x) }

def clusterSummaries(clusterStates: Seq[String] = Nil, createdBefore: Option[java.util.Date] = None, createdAfter: Option[java.util.Date] = None): Seq[ClusterSummary] = {
import aws.model.ListClustersResult
Expand Down
5 changes: 3 additions & 2 deletions iam/src/main/scala/awscala/iam/Group.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package awscala.iam

import awscala.DateTime.toDate
import awscala._
import com.amazonaws.services.{ identitymanagement => aws }

Expand All @@ -10,11 +11,11 @@ object Group {
name = g.getGroupName,
arn = g.getArn,
path = g.getPath,
createdAt = new DateTime(g.getCreateDate))
createdAt = DateTime(g.getCreateDate))
}

case class Group(id: String, name: String, arn: String, path: String, createdAt: DateTime)
extends aws.model.Group(path, name, id, arn, createdAt.toDate) {
extends aws.model.Group(path, name, id, arn, toDate(createdAt)) {

def updateName(newName: String)(implicit iam: IAM) = iam.updateGroupName(this, newName)
def updatePath(newPath: String)(implicit iam: IAM) = iam.updateGroupPath(this, newPath)
Expand Down
6 changes: 4 additions & 2 deletions iam/src/main/scala/awscala/iam/InstanceProfile.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package awscala.iam

import awscala.DateTime.toDate
import awscala._

import scala.jdk.CollectionConverters._
import com.amazonaws.services.{ identitymanagement => aws }

Expand All @@ -12,14 +14,14 @@ object InstanceProfile {
arn = g.getArn,
path = g.getPath,
roles = g.getRoles.asScala.map(r => Role(r)).toSeq,
createdAt = new DateTime(g.getCreateDate))
createdAt = DateTime(g.getCreateDate))
}

case class InstanceProfile(id: String, name: String, arn: String, path: String, roles: Seq[Role], createdAt: DateTime)
extends aws.model.InstanceProfile {

setArn(arn)
setCreateDate(createdAt.toDate)
setCreateDate(toDate(createdAt))
setInstanceProfileId(id)
setInstanceProfileName(name)
setPath(path)
Expand Down
7 changes: 4 additions & 3 deletions iam/src/main/scala/awscala/iam/LoginProfile.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package awscala.iam

import awscala.DateTime.toDate
import awscala._
import scala.jdk.CollectionConverters._

import com.amazonaws.services.{ identitymanagement => aws }

object LoginProfile {

def apply(user: User, g: aws.model.LoginProfile): LoginProfile = new LoginProfile(
user = user,
createdAt = new DateTime(g.getCreateDate))
createdAt = DateTime(g.getCreateDate))
}

case class LoginProfile(user: User, createdAt: DateTime)
extends aws.model.LoginProfile {

setUserName(user.name)
setCreateDate(createdAt.toDate)
setCreateDate(toDate(createdAt))

def changePassword(newPassword: String)(implicit iam: IAM) = iam.changeUserPassword(this, newPassword)
def destroy()(implicit iam: IAM) = iam.delete(this)
Expand Down
5 changes: 3 additions & 2 deletions iam/src/main/scala/awscala/iam/Role.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package awscala.iam

import awscala.DateTime.toDate
import awscala._
import com.amazonaws.services.{ identitymanagement => aws }

Expand All @@ -11,15 +12,15 @@ object Role {
arn = g.getArn,
path = g.getPath,
assumeRolePolicyDocument = g.getAssumeRolePolicyDocument,
createdAt = new DateTime(g.getCreateDate))
createdAt = DateTime(g.getCreateDate))
}

case class Role(id: String, name: String, arn: String, path: String, assumeRolePolicyDocument: String, createdAt: DateTime)
extends aws.model.Role {

setArn(arn)
setAssumeRolePolicyDocument(assumeRolePolicyDocument)
setCreateDate(createdAt.toDate)
setCreateDate(toDate(createdAt))
setPath(path)
setRoleId(id)
setRoleName(name)
Expand Down
5 changes: 3 additions & 2 deletions iam/src/main/scala/awscala/iam/User.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package awscala.iam

import awscala.DateTime.toDate
import awscala._
import com.amazonaws.services.{ identitymanagement => aws }

Expand All @@ -10,11 +11,11 @@ object User {
name = g.getUserName,
arn = g.getArn,
path = g.getPath,
createdAt = new DateTime(g.getCreateDate))
createdAt = DateTime(g.getCreateDate))
}

case class User(id: String, name: String, arn: String, path: String, createdAt: DateTime)
extends aws.model.User(path, name, id, arn, createdAt.toDate) {
extends aws.model.User(path, name, id, arn, toDate(createdAt)) {

def updateName(name: String)(implicit iam: IAM) = iam.updateUserName(this, name)
def updatePath(path: String)(implicit iam: IAM) = iam.updateUserPath(this, path)
Expand Down
7 changes: 4 additions & 3 deletions iam/src/main/scala/awscala/iam/VirtualMFADevice.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package awscala.iam

import awscala.DateTime.toDate
import awscala._
import com.amazonaws.services.{ identitymanagement => aws }

object VirtualMFADevice {

def apply(user: User, g: aws.model.MFADevice): VirtualMFADevice = new VirtualMFADevice(
base32StringSeed = None,
enabledAt = new DateTime(g.getEnableDate),
enabledAt = DateTime(g.getEnableDate),
qrCodePng = None,
serialNumber = g.getSerialNumber,
user = user)
def apply(g: aws.model.VirtualMFADevice): VirtualMFADevice = new VirtualMFADevice(
base32StringSeed = Some(g.getBase32StringSeed),
enabledAt = new DateTime(g.getEnableDate),
enabledAt = DateTime(g.getEnableDate),
qrCodePng = Some(g.getQRCodePNG),
serialNumber = g.getSerialNumber,
user = User(g.getUser))
Expand All @@ -25,7 +26,7 @@ case class VirtualMFADevice(
extends aws.model.VirtualMFADevice {

setBase32StringSeed(base32StringSeed.orNull[java.nio.ByteBuffer])
setEnableDate(enabledAt.toDate)
setEnableDate(toDate(enabledAt))
setQRCodePNG(qrCodePng.orNull[java.nio.ByteBuffer])
setSerialNumber(serialNumber)
setUser(user)
Expand Down
6 changes: 4 additions & 2 deletions redshift/src/main/scala/awscala/redshift/Cluster.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package awscala.redshift

import awscala.DateTime.toDate
import awscala._

import scala.jdk.CollectionConverters._
import com.amazonaws.services.{ redshift => aws }

Expand Down Expand Up @@ -28,7 +30,7 @@ object Cluster {
securityGroupMemberships = c.getClusterSecurityGroups.asScala.map(m => ClusterSecurityGroupMembership(m)).toSeq,
vpcId = c.getVpcId,
vpcSecurityGroupMemberships = c.getVpcSecurityGroups.asScala.map(m => VpcSecurityGroupMembership(m)).toSeq,
createdAt = new DateTime(c.getClusterCreateTime))
createdAt = DateTime(c.getClusterCreateTime))
}

class Cluster(
Expand Down Expand Up @@ -59,7 +61,7 @@ class Cluster(
setAllowVersionUpgrade(allowVersionUpgrade)
setAutomatedSnapshotRetentionPeriod(automatedSnapshotRetentionPeriod)
setAvailabilityZone(availabilityZone.name)
setClusterCreateTime(createdAt.toDate)
setClusterCreateTime(toDate(createdAt))
setClusterIdentifier(identifier)
setClusterParameterGroups(parameterGroupStatuses.map(_.asInstanceOf[aws.model.ClusterParameterGroupStatus]).asJava)
setClusterSecurityGroups(securityGroupMemberships.map(_.asInstanceOf[aws.model.ClusterSecurityGroupMembership]).asJava)
Expand Down
5 changes: 3 additions & 2 deletions redshift/src/main/scala/awscala/redshift/Event.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package awscala.redshift

import awscala.DateTime.toDate
import awscala._
import com.amazonaws.services.{ redshift => aws }

Expand All @@ -9,7 +10,7 @@ object Event {
sourceIdentifier = e.getSourceIdentifier,
sourceType = aws.model.SourceType.fromValue(e.getSourceType),
message = e.getMessage,
createdAt = new DateTime(e.getDate))
createdAt = DateTime(e.getDate))
}

case class Event(
Expand All @@ -18,7 +19,7 @@ case class Event(
message: String,
createdAt: DateTime) extends aws.model.Event {

setDate(createdAt.toDate)
setDate(toDate(createdAt))
setMessage(message)
setSourceIdentifier(sourceIdentifier)
setSourceType(sourceType)
Expand Down
8 changes: 5 additions & 3 deletions redshift/src/main/scala/awscala/redshift/Redshift.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package awscala.redshift

import awscala.DateTime.toDate
import awscala._

import scala.jdk.CollectionConverters._
import com.amazonaws.auth.AWSCredentialsProvider
import com.amazonaws.services.{ redshift => aws }
Expand Down Expand Up @@ -123,7 +125,7 @@ trait Redshift extends aws.AmazonRedshift {

val req = new aws.model.DescribeClusterSnapshotsRequest()
.withSnapshotIdentifier(snapshotIdentifier)
.withStartTime(from.toDate)
.withStartTime(toDate(from))
.withMarker(marker)
.withMaxRecords(maxRecords)
.withOwnerAccount(ownerAccount)
Expand All @@ -133,10 +135,10 @@ trait Redshift extends aws.AmazonRedshift {
req.setClusterIdentifier(clusterIdentifier)
}
if (from != null) {
req.setStartTime(from.toDate)
req.setStartTime(toDate(from))
}
if (to != null) {
req.setEndTime(to.toDate)
req.setEndTime(toDate(to))
}
if (marker != null) {
req.setMarker(marker)
Expand Down
Loading