std.conv — Lilush API

←index

← std

Overview

Conversion utilities for formatting byte sizes, IP addresses, timestamps, and human-readable time spans.

Functions

NameSignature
bytes_humanbytes_human(size) -> formatted
hex_ipv4hex_ipv4(hex_ip_str) -> ip, port
date_to_tsdate_to_ts(date_str) -> timestamp
ts_to_strts_to_str(ts, fmt) -> date
time_span_humantime_span_human(seconds, precision) -> span
time_diff_humantime_diff_human(date, precision, start_ts) -> span

bytes_human(size) -> formatted

Format a byte count as a human-readable string with units

Selects the largest unit for which size is at least 1 unit: TB (≥ 1024⁴), GB (≥ 1024³), MB (≥ 1024²), KB (≥ 1024). Values below 1024 are returned as "N B" (no decimal). All other values are formatted as "%.2f UNIT" (two decimal places).

hex_ipv4(hex_ip_str) -> ip, port

Convert a hex-encoded IPv4 address from /proc/net/route format

Parses a hex-encoded IPv4 address (e.g., from /proc/net/route) and returns the dotted-decimal IP address and port number as strings.

date_to_ts(date_str) -> timestamp

Parse a date string into a Unix timestamp

Supports multiple date formats: Unix timestamp (numeric string), ISO 8601 (2024-01-15T10:30:00), compact (240115103000), and HTTP date (Mon, 15 Jan 2024 10:30:00).

ts_to_str(ts, fmt) -> date

Format a Unix timestamp as a date string

Formats a Unix timestamp as a date string using os.date with the given format. Default format is ISO 8601 (%Y-%m-%dT%H:%M:%S). See RFC3339 for details.

time_span_human(seconds, precision) -> span

Convert a duration in seconds to a human-readable time span

Converts seconds into a string like "1 year, 2 months, 3 days". Precision controls the smallest unit shown (default "second"). Note: results are approximate, months are treated as 30 days.

time_diff_human(date, precision, start_ts) -> span

Get the human-readable time difference from a date to now

Calculates the time difference between a given date and the current time (or start_ts if provided), returning a human-readable string like "2 days, 3 hours". Precision controls the smallest unit shown.