GoloScript Time Functions

Complete guide to time manipulation functions in GoloScript.

Introduction

GoloScript provides a comprehensive set of functions for working with time, dates, and timestamps. These functions allow you to:

Getting Current Time

Unix Timestamps

# Milliseconds since Unix epoch (1970-01-01)
let millis = currentTimeMillis()
println(millis)  # 1768608150316

# Seconds since Unix epoch
let seconds = currentTime()
println(seconds)  # 1768608150

# Nanoseconds since Unix epoch
let nanos = currentTimeNano()
println(nanos)  # 1768608150316700000

Detailed Time Information

# Get all components of current time
let timeInfo = now()

println("Year:        " + timeInfo: get("year"))        # 2026
println("Month:       " + timeInfo: get("month"))       # 1 (January)
println("Day:         " + timeInfo: get("day"))         # 17
println("Hour:        " + timeInfo: get("hour"))        # 14 (0-23)
println("Minute:      " + timeInfo: get("minute"))      # 30 (0-59)
println("Second:      " + timeInfo: get("second"))      # 45 (0-59)
println("Millisecond: " + timeInfo: get("millisecond")) # 316 (0-999)
println("Weekday:     " + timeInfo: get("weekday"))     # 6 (0=Sunday)
println("Year day:    " + timeInfo: get("yearday"))     # 17 (1-366)
println("Unix:        " + timeInfo: get("unix"))        # 1768608150

Quick Formatted Strings

# Date in YYYY-MM-DD format
let date = dateString()
println(date)  # "2026-01-17"

# Time in HH:MM:SS format
let time = timeString()
println(time)  # "14:30:45"

# Date and time in YYYY-MM-DD HH:MM:SS format
let datetime = dateTimeString()
println(datetime)  # "2026-01-17 14:30:45"

Time Formatting

formatTime Function

The formatTime() function formats a Unix timestamp (in seconds) according to a specific layout.

let timestamp = currentTime()

# Date formats
formatTime(timestamp, "2006-01-02")           # "2026-01-17"
formatTime(timestamp, "02/01/2006")           # "17/01/2026"
formatTime(timestamp, "01/02/2006")           # "01/17/2026"
formatTime(timestamp, "January 02, 2006")     # "January 17, 2026"
formatTime(timestamp, "Jan 02, 06")           # "Jan 17, 26"

# Time formats
formatTime(timestamp, "15:04:05")             # "14:30:45" (24h)
formatTime(timestamp, "3:04:05 PM")           # "2:30:45 PM" (12h)
formatTime(timestamp, "3:04PM")               # "2:30PM"

# Combined formats
formatTime(timestamp, "2006-01-02 15:04:05")  # "2026-01-17 14:30:45"
formatTime(timestamp, "Monday, January 2, 2006 at 3:04PM")
# "Saturday, January 17, 2026 at 2:30PM"

Go Time Layouts

GoloScript uses Go’s time layouts. The reference layout is:

Mon Jan 2 15:04:05 MST 2006

Components:

Standard Formats

let ts = currentTime()

# RFC3339
formatTime(ts, "2006-01-02T15:04:05Z07:00")
# "2026-01-17T14:30:45+01:00"

# RFC1123
formatTime(ts, "Mon, 02 Jan 2006 15:04:05 MST")
# "Sat, 17 Jan 2026 14:30:45 CET"

# ISO 8601
formatTime(ts, "2006-01-02 15:04:05")
# "2026-01-17 14:30:45"

# File-safe format
formatTime(ts, "2006-01-02_15-04-05")
# "2026-01-17_14-30-45"

Time Parsing

parseTime Function

Parse a time string according to a layout and return a Unix timestamp.

# Parse ISO date
let iso = "2026-01-17 14:30:00"
let timestamp = parseTime(iso, "2006-01-02 15:04:05")
println(timestamp)  # 1768660200

# Parse US date
let us = "01/17/2026"
let ts = parseTime(us, "01/02/2006")

# Parse with time
let full = "January 17, 2026 at 2:30PM"
let ts = parseTime(full, "January 2, 2006 at 3:04PM")

Converting Between Formats

# Convert from US format to ISO format
let us = "01/17/2026 2:30PM"
let ts = parseTime(us, "01/02/2006 3:04PM")
let iso = formatTime(ts, "2006-01-02 15:04:05")
println(iso)  # "2026-01-17 14:30:00"

Timing Operations

Sleep (Pause)

# Pause for 1 second (1000 milliseconds)
println("Start")
sleep(1000)
println("End")

# Pause for 500ms
sleep(500)

# Pause for 2.5 seconds
sleep(2500)

Measuring Execution Time

# Measure in milliseconds
let start = currentTimeMillis()
# ... code to measure ...
let end = currentTimeMillis()
let elapsed = end - start
println("Time: " + elapsed + "ms")

# Measure in nanoseconds (more precise)
let startNano = currentTimeNano()
# ... code to measure ...
let endNano = currentTimeNano()
let elapsedNano = endNano - startNano
let elapsedMs = elapsedNano / 1000000
println("Time: " + elapsedMs + "ms (" + elapsedNano + "ns)")

Stopwatch

function fibonacci = |n| {
  if n <= 1 {
    return n
  }
  return fibonacci(n - 1) + fibonacci(n - 2)
}

let start = currentTimeMillis()
let result = fibonacci(30)
let end = currentTimeMillis()

println("Result: " + result)
println("Time: " + (end - start) + "ms")

Practical Examples

Digital Clock

function clock = {
  let count = 0
  while count < 10 {
    let time = now()
    let h = time: get("hour")
    let m = time: get("minute")
    let s = time: get("second")

    let hStr = if h < 10 { "0" + h } else { h }
    let mStr = if m < 10 { "0" + m } else { m }
    let sStr = if s < 10 { "0" + s } else { s }

    print("\r⏰ " + hStr + ":" + mStr + ":" + sStr)
    sleep(1000)
    let count = count + 1
  }
  println("")
}

Countdown

function countdown = |seconds| {
  let count = seconds
  while count > 0 {
    println(count + "...")
    sleep(1000)
    let count = count - 1
  }
  println("🚀 GO!")
}

countdown(5)  # 5-second countdown

Logger with Timestamps

function log = |message| {
  let timestamp = dateTimeString()
  println("[" + timestamp + "] " + message)
}

log("Application started")
sleep(1000)
log("Processing...")
sleep(2000)
log("Processing complete")

Function Benchmark

function benchmark = |name, func, iterations| {
  println("Benchmark: " + name)

  let start = currentTimeNano()
  let i = 0
  while i < iterations {
    func()
    let i = i + 1
  }
  let end = currentTimeNano()

  let total = end - start
  let average = total / iterations
  let totalMs = total / 1000000
  let avgMs = average / 1000000

  println("  Total:   " + totalMs + "ms")
  println("  Average: " + avgMs + "ms per iteration")
  println("  Iterations: " + iterations)
}

# Usage
function test1 = { sleep(10) }
function test2 = { fibonacci(20) }

benchmark("Sleep 10ms", test1, 10)
benchmark("Fibonacci(20)", test2, 100)

Timestamped Filename

function createLogFilename = || {
  let timestamp = currentTime()
  let formatted = formatTime(timestamp, "2006-01-02_15-04-05")
  return "log_" + formatted + ".txt"
}

let filename = createLogFilename()
println(filename)  # "log_2026-01-17_14-30-45.txt"

Calculate Durations

function showDuration = |startTime, endTime| {
  let duration = endTime - startTime
  let seconds = duration
  let minutes = seconds / 60
  let hours = minutes / 60
  let days = hours / 24

  if days > 0 {
    println(days + " days")
  } else if hours > 0 {
    println(hours + " hours")
  } else if minutes > 0 {
    println(minutes + " minutes")
  } else {
    println(seconds + " seconds")
  }
}

let start = parseTime("2026-01-01 00:00:00", "2006-01-02 15:04:05")
let end = parseTime("2026-01-17 14:30:00", "2006-01-02 15:04:05")
showDuration(start, end)  # "16 days"

API Reference

Get Current Time

currentTimeMillis()

Returns the current Unix timestamp in milliseconds.

Returns: Integer - Milliseconds since 1970-01-01

Example:

let ms = currentTimeMillis()  # 1768608150316

currentTime()

Returns the current Unix timestamp in seconds.

Returns: Integer - Seconds since 1970-01-01

Example:

let s = currentTime()  # 1768608150

currentTimeNano()

Returns the current Unix timestamp in nanoseconds.

Returns: Integer - Nanoseconds since 1970-01-01

Example:

let ns = currentTimeNano()  # 1768608150316700000

now()

Returns a map with all components of the current time.

Returns: Map with keys:

Example:

let time = now()
let year = time: get("year")
let hour = time: get("hour")

Quick Formatting

dateString()

Returns the current date in YYYY-MM-DD format.

Returns: String - Formatted date

Example:

let date = dateString()  # "2026-01-17"

timeString()

Returns the current time in HH:MM:SS format.

Returns: String - Formatted time

Example:

let time = timeString()  # "14:30:45"

dateTimeString()

Returns the current date and time in YYYY-MM-DD HH:MM:SS format.

Returns: String - Formatted date/time

Example:

let dt = dateTimeString()  # "2026-01-17 14:30:45"

Custom Formatting

formatTime(timestamp, layout)

Formats a Unix timestamp according to a specific layout.

Parameters:

Returns: String - Formatted time

Example:

let ts = currentTime()
let formatted = formatTime(ts, "2006-01-02 15:04:05")

Parsing

parseTime(timeString, layout)

Parses a time string and returns a Unix timestamp.

Parameters:

Returns: Integer - Unix timestamp in seconds

Exception: Throws an exception if parsing fails

Example:

let ts = parseTime("2026-01-17 14:30:00", "2006-01-02 15:04:05")

Time Control

sleep(milliseconds)

Pauses execution for the specified duration.

Parameters:

Returns: Null

Example:

sleep(1000)  # 1 second pause
sleep(500)   # 0.5 second pause

© 2026 GoloScript Project | Built with Gu10berg

Subscribe: 📡 RSS | ⚛️ Atom