Remote Script Executor Component
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / utils / DateUtils.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.core.utils
18
19 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
20 import java.text.SimpleDateFormat
21 import java.time.LocalDateTime
22 import java.time.ZoneId
23 import java.time.format.DateTimeFormatter
24 import java.util.Date
25
26 fun controllerDate(): Date {
27     val localDateTime = LocalDateTime.now(ZoneId.systemDefault())
28     return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant())
29 }
30
31 fun currentTimestamp(): String {
32     val localDateTime = LocalDateTime.now(ZoneId.systemDefault())
33     val formatter = DateTimeFormatter.ofPattern(BluePrintConstants.DATE_TIME_PATTERN)
34     return formatter.format(localDateTime)
35 }
36
37 /** Parse string date in CDS string format */
38 fun String.toControllerDate(): Date {
39     val formatter = SimpleDateFormat(BluePrintConstants.DATE_TIME_PATTERN)
40     return formatter.parse(this)
41 }
42
43 /** Return date to CDS string format */
44 fun Date.currentTimestamp(): String {
45     val formatter = SimpleDateFormat(BluePrintConstants.DATE_TIME_PATTERN)
46     return formatter.format(this)
47 }