CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-ktor--ktor-utils-jvm

Ktor utilities library for JVM platform containing common utility functions, cryptographic operations, date handling, logging utilities, pipeline functionality, I/O adapters, encoding/decoding utilities, network address handling, and various platform-specific implementations for the Ktor framework.

Pending
Overview
Eval results
Files

datetime.mddocs/

Date & Time

GMT date handling with HTTP date format support and timezone operations. Provides comprehensive date and time utilities specifically designed for web applications and HTTP protocol compatibility.

Capabilities

GMTDate Class

Core date class representing GMT/UTC time with full date components.

/**
 * Represents a GMT date with all date components
 */
class GMTDate(
    val seconds: Int,      // 0-59
    val minutes: Int,      // 0-59  
    val hours: Int,        // 0-23
    val dayOfMonth: Int,   // 1-31
    val month: Month,      // Month enum
    val year: Int,         // Full year (e.g., 2023)
    val dayOfWeek: WeekDay, // WeekDay enum
    val dayOfYear: Int,    // 1-366
    val timestamp: Long    // Unix timestamp in milliseconds
) {
    /** String representation in HTTP date format */
    override fun toString(): String
    
    /** Equality comparison */
    override fun equals(other: Any?): Boolean
    
    /** Hash code based on timestamp */
    override fun hashCode(): Int
}

Usage Examples:

import io.ktor.util.date.*

// Create GMTDate from components
val date = GMTDate(
    seconds = 30,
    minutes = 45, 
    hours = 14,
    dayOfMonth = 25,
    month = Month.DECEMBER,
    year = 2023,
    dayOfWeek = WeekDay.MONDAY,
    dayOfYear = 359,
    timestamp = System.currentTimeMillis()
)

println(date) // HTTP date format
println("Year: ${date.year}, Month: ${date.month}")
println("Timestamp: ${date.timestamp}")

GMTDate Factory Functions

Functions for creating GMTDate instances from various sources.

/**
 * Create GMTDate from Unix timestamp
 * @param timestamp Unix timestamp in milliseconds
 * @return GMTDate instance
 */
fun GMTDate(timestamp: Long): GMTDate

/**
 * Create GMTDate from current time
 * @return GMTDate representing current GMT time
 */
fun GMTDate(): GMTDate

/**
 * Create GMTDate from individual components
 * @param year Full year
 * @param month Month (1-12)
 * @param day Day of month (1-31)
 * @param hour Hour (0-23)
 * @param minute Minute (0-59)
 * @param second Second (0-59)
 * @return GMTDate instance
 */
fun GMTDate(
    year: Int,
    month: Int,
    day: Int,
    hour: Int = 0,
    minute: Int = 0,
    second: Int = 0
): GMTDate

Usage Examples:

import io.ktor.util.date.*

// Create from timestamp
val fromTimestamp = GMTDate(1703516400000L) // Dec 25, 2023

// Create current date
val now = GMTDate()

// Create from components
val specificDate = GMTDate(
    year = 2024,
    month = 6,    // June
    day = 15,
    hour = 10,
    minute = 30,
    second = 45
)

println("Current time: $now")
println("Specific date: $specificDate")

Month Enumeration

Enumeration representing months of the year.

/**
 * Months of the year
 */
enum class Month(val value: String) {
    JANUARY("Jan"),
    FEBRUARY("Feb"),
    MARCH("Mar"),
    APRIL("Apr"),
    MAY("May"),
    JUNE("Jun"),
    JULY("Jul"),
    AUGUST("Aug"),
    SEPTEMBER("Sep"),
    OCTOBER("Oct"),
    NOVEMBER("Nov"),
    DECEMBER("Dec");
    
    companion object {
        /** Lookup month by ordinal */
        fun from(ordinal: Int): Month
        
        /** Lookup month by short name */
        fun from(value: String): Month
    }
}

Usage Examples:

import io.ktor.util.date.*

// Work with months
val currentMonth = Month.DECEMBER
println("Month: ${currentMonth.name}") // "DECEMBER"
println("Short name: ${currentMonth.value}") // "Dec"

// Iterate through months
for (month in Month.values()) {
    println("${month.name}: ${month.value}")
}

// Find month by short name
val monthByValue = Month.from("Jun") // JUNE
val monthByOrdinal = Month.from(5) // JUNE (0-based)

WeekDay Enumeration

Enumeration representing days of the week.

/**
 * Days of the week
 */
enum class WeekDay(val value: String) {
    MONDAY("Mon"),
    TUESDAY("Tue"),
    WEDNESDAY("Wed"),
    THURSDAY("Thu"),
    FRIDAY("Fri"),
    SATURDAY("Sat"),
    SUNDAY("Sun");
    
    companion object {
        /** Lookup day by ordinal */
        fun from(ordinal: Int): WeekDay
        
        /** Lookup day by short name */
        fun from(value: String): WeekDay
    }
}

Usage Examples:

import io.ktor.util.date.*

// Work with weekdays
val today = WeekDay.FRIDAY
println("Today is: ${today.name}") // "FRIDAY"
println("Short name: ${today.value}") // "Fri"

// Find by short name or ordinal
val dayByName = WeekDay.from("Fri") // FRIDAY
val dayByOrdinal = WeekDay.from(4) // FRIDAY (0-based)

// Check weekend
fun isWeekend(day: WeekDay): Boolean {
    return day == WeekDay.SATURDAY || day == WeekDay.SUNDAY
}

println("Is Friday weekend? ${isWeekend(WeekDay.FRIDAY)}") // false

HTTP Date Formatting

Functions for converting between GMTDate and HTTP date strings.

/**
 * Convert GMTDate to HTTP date string
 * @return HTTP-formatted date string (RFC 7231)
 */
fun GMTDate.toHttpDate(): String

/**
 * Parse HTTP date string to GMTDate
 * @param httpDate HTTP-formatted date string
 * @return Parsed GMTDate instance
 * @throws IllegalArgumentException if format is invalid
 */
fun String.fromHttpDate(): GMTDate

Usage Examples:

import io.ktor.util.date.*

// Convert to HTTP date format
val date = GMTDate(2023, 12, 25, 14, 30, 0)
val httpDate = date.toHttpDate()
println(httpDate) // "Mon, 25 Dec 2023 14:30:00 GMT"

// Parse HTTP date string
val httpDateString = "Tue, 15 Nov 1994 08:12:31 GMT"
val parsedDate = httpDateString.fromHttpDate()
println("Parsed: ${parsedDate.year}-${parsedDate.month.value}-${parsedDate.dayOfMonth}")

// Use in HTTP headers
val headers = mutableMapOf<String, String>()
headers["Date"] = GMTDate().toHttpDate()
headers["Last-Modified"] = lastModified.toHttpDate()
headers["Expires"] = expirationDate.toHttpDate()

GMT Date Parser

Advanced parsing utilities for various date formats.

/**
 * GMT date parser with support for multiple formats
 */
object GMTDateParser {
    /** Parse date from various common formats */
    fun parse(dateString: String): GMTDate
    
    /** Parse HTTP date format (RFC 7231) */
    fun parseHttpDate(httpDate: String): GMTDate
    
    /** Parse ISO 8601 format */
    fun parseISODate(isoDate: String): GMTDate
    
    /** Try parsing with multiple format attempts */
    fun tryParse(dateString: String): GMTDate?
}

Usage Examples:

import io.ktor.util.date.*

// Parse different date formats
val httpDate = GMTDateParser.parseHttpDate("Mon, 25 Dec 2023 14:30:00 GMT")
val isoDate = GMTDateParser.parseISODate("2023-12-25T14:30:00Z")

// Try parsing unknown format
val unknownFormat = "25/12/2023 14:30:00"
val parsed = GMTDateParser.tryParse(unknownFormat)
if (parsed != null) {
    println("Successfully parsed: $parsed")
} else {
    println("Could not parse date")
}

// Generic parsing
val flexibleDate = GMTDateParser.parse("December 25, 2023")

Date Arithmetic

Utilities for date calculations and comparisons.

/**
 * Date arithmetic extension functions
 */

/** Add milliseconds to GMTDate */
fun GMTDate.plus(milliseconds: Long): GMTDate

/** Subtract milliseconds from GMTDate */
fun GMTDate.minus(milliseconds: Long): GMTDate

/** Get difference in milliseconds between dates */
operator fun GMTDate.minus(other: GMTDate): Long

/** Check if date is before another */
fun GMTDate.isBefore(other: GMTDate): Boolean

/** Check if date is after another */
fun GMTDate.isAfter(other: GMTDate): Boolean

/** Check if dates are on same day */
fun GMTDate.isSameDay(other: GMTDate): Boolean

Usage Examples:

import io.ktor.util.date.*

val baseDate = GMTDate(2023, 12, 25, 12, 0, 0)

// Date arithmetic
val oneHourLater = baseDate.plus(60 * 60 * 1000) // Add 1 hour
val oneDayEarlier = baseDate.minus(24 * 60 * 60 * 1000) // Subtract 1 day

// Date comparisons
val now = GMTDate()
if (baseDate.isBefore(now)) {
    println("Base date is in the past")
}

// Calculate duration
val start = GMTDate(2023, 1, 1, 0, 0, 0)
val end = GMTDate(2023, 12, 31, 23, 59, 59) 
val durationMs = end - start
val durationDays = durationMs / (24 * 60 * 60 * 1000)
println("Duration: $durationDays days")

// Check same day
val morning = GMTDate(2023, 12, 25, 9, 0, 0)
val evening = GMTDate(2023, 12, 25, 21, 0, 0)
println("Same day: ${morning.isSameDay(evening)}") // true

Time Zone Utilities

Additional utilities for timezone-aware operations.

/**
 * Time zone conversion utilities
 */
object TimeZoneUtils {
    /** Convert GMT date to local time zone */
    fun GMTDate.toLocalTime(): LocalDateTime
    
    /** Convert local time to GMT */
    fun LocalDateTime.toGMTDate(): GMTDate
    
    /** Get current timezone offset from GMT */
    fun getCurrentTimezoneOffset(): Int
    
    /** Format date with timezone information */
    fun GMTDate.formatWithTimezone(zoneId: String): String
}

Usage Examples:

import io.ktor.util.date.*

// Timezone conversions
val gmtDate = GMTDate()
val localTime = TimeZoneUtils.run { gmtDate.toLocalTime() }
val backToGmt = TimeZoneUtils.run { localTime.toGMTDate() }

// Get timezone offset
val offsetMinutes = TimeZoneUtils.getCurrentTimezoneOffset()
println("Current timezone offset: $offsetMinutes minutes from GMT")

// Format with timezone
val formatted = TimeZoneUtils.run { 
    gmtDate.formatWithTimezone("America/New_York") 
}
println("Date in NY timezone: $formatted")

Constants and Utilities

/** Milliseconds in a second */
const val MILLIS_PER_SECOND = 1000L

/** Milliseconds in a minute */
const val MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND

/** Milliseconds in an hour */
const val MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE

/** Milliseconds in a day */
const val MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR

Install with Tessl CLI

npx tessl i tessl/maven-io-ktor--ktor-utils-jvm

docs

attributes.md

collections.md

compression.md

conversion.md

crypto.md

datetime.md

index.md

io.md

logging.md

pipeline.md

strings.md

tile.json