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:
- Get current time and date
- Format timestamps in various formats
- Parse time strings
- Measure execution time
- Create delays/pauses
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:
2006- Year (4 digits)06- Year (2 digits)01- Month (2 digits)1- Month (no leading zero)January- Month name (full)Jan- Month name (abbreviated)02- Day (2 digits)2or_2- Day (no leading zero)Monday- Day name (full)Mon- Day name (abbreviated)15- Hour (24h, 2 digits)3- Hour (12h, no leading zero)04- Minute (2 digits)05- Second (2 digits)PMorpm- AM/PMMST- Time zone
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:
year- Year (2026)month- Month (1-12)day- Day of month (1-31)hour- Hour (0-23)minute- Minute (0-59)second- Second (0-59)millisecond- Millisecond (0-999)weekday- Day of week (0-6, 0=Sunday)yearday- Day of year (1-366)unix- Unix timestamp in seconds
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:
timestamp(Integer) - Unix timestamp in secondslayout(String) - Go formatting layout
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:
timeString(String) - String to parselayout(String) - Go parsing layout
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:
milliseconds(Integer) - Duration in milliseconds
Returns: Null
Example:
sleep(1000) # 1 second pause
sleep(500) # 0.5 second pause