All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atlan.pkg.serde.cell.TimestampXformer.kt Maven / Gradle / Ivy

There is a newer version: 3.1.2
Show newest version
/* SPDX-License-Identifier: Apache-2.0
   Copyright 2023 Atlan Pte. Ltd. */
package com.atlan.pkg.serde.cell

import java.time.Instant
import java.time.format.DateTimeFormatter

object TimestampXformer {
    private val FORMATTER = DateTimeFormatter.ISO_INSTANT

    /**
     * Encodes (serializes) an epoch-based numeric timestamp into a human-readable date
     * and time string form.
     *
     * @param ts epoch-based numeric timestamp
     * @return a human-readable date and time
     */
    fun encode(ts: Long?): String {
        return if (ts == null || ts == 0L) {
            ""
        } else {
            FORMATTER.format(Instant.ofEpochMilli(ts))
        }
    }

    /**
     * Decodes (deserializes) a human-readable date and time string form into an epoch-based
     * numeric timestamp.
     *
     * @param ts human-readable date time string
     * @param fieldName name of the field where the string appears
     * @return an epoch-based numeric timestamp
     */
    fun decode(
        ts: String?,
        fieldName: String,
    ): Long? {
        return if (ts.isNullOrBlank()) {
            null
        } else if (ts.toLongOrNull() != null) {
            return ts.toLong()
        } else {
            FORMATTER.parse(ts, Instant::from).toEpochMilli()
        }
    }

    /**
     * Decodes (deserializes) a numeric timestamp into an epoch-based numeric timestamp.
     *
     * @param ts epoch-based numeric timestamp
     * @param fieldName name of the field where the timestamp appears
     * @return the epoch-based numeric timestamp
     */
    fun decode(
        ts: Long?,
        fieldName: String,
    ): Long? {
        return ts
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy