microtime
microtime(bool $as_float = false): float|string
Returns the current Unix timestamp (a 'microsec sec' string by default, a float in seconds when passed true).
C C23
#include <stdio.h>
#include <sys/time.h>
int main(void) {
/* gettimeofday fills seconds + microseconds; here a fixed pair shows the format */
struct timeval tv = {1700000000, 123456};
printf("%ld.%06ld\n", (long)tv.tv_sec, (long)tv.tv_usec);
return 0;
}
1700000000.123456
C++ C++26
#include <chrono>
#include <iomanip>
#include <iostream>
int main() {
// chrono models time as durations; format a fixed one with 6 decimals
std::chrono::duration<double> d{1700000000.123456};
std::cout << std::fixed << std::setprecision(6) << d.count() << "\n";
return 0;
}
1700000000.123456
C# v11.0.100
// Stopwatch is the monotonic clock; here a fixed millisecond value shows sub-second formatting
Console.WriteLine(DateTimeOffset.FromUnixTimeMilliseconds(1700000000123)
.UtcDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
2023-11-14 22:13:20.123
Go v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
// time.Now embeds a monotonic clock; a fixed value shows sub-second formatting
t := time.Unix(1700000000, 123456000).UTC()
fmt.Println(t.Format("2006-01-02 15:04:05.000000"))
}
2023-11-14 22:13:20.123456
Java v27.0.1
import java.time.Instant;
public class Main {
public static void main(String[] args) {
// System.nanoTime() is the monotonic clock; a fixed value shows sub-second printing
System.out.println(Instant.ofEpochSecond(1700000000, 123456000));
}
}
2023-11-14T22:13:20.123456Z
JavaScript v26.13.1
// performance.now() is the monotonic clock (ms float); Date.now() is wall time in ms
console.log(new Date(1700000000123).toISOString()); // fixed sample
2023-11-14T22:13:20.123Z
PHP v8.6.0
$parts = explode(' ', microtime(false)); // "usec sec" two-part string; values change per call
echo count($parts), PHP_EOL; // structure is deterministic: 2 parts
echo number_format(1700000000.123456, 6, '.', ''), PHP_EOL; // the float form (as_float=true), fixed sample
2 1700000000.123456
Python v3.15.2
from datetime import datetime, timezone
# time.monotonic() is the monotonic clock; a fixed value shows sub-second formatting
print(datetime.fromtimestamp(1700000000.123456, tz=timezone.utc)
.strftime('%Y-%m-%d %H:%M:%S.%f'))
2023-11-14 22:13:20.123456
Rust v1.99.1
use std::time::{Duration, SystemTime, UNIX_EPOCH};
fn main() {
// Instant is the monotonic clock; std has no date formatting, so show the raw split
let t = UNIX_EPOCH + Duration::new(1700000000, 123456000);
let d = t.duration_since(UNIX_EPOCH).unwrap();
println!("{}.{:09}", d.as_secs(), d.subsec_nanos());
}
1700000000.123456000
TypeScript v26.13.1
// performance.now() is the monotonic clock (ms float); Date.now() is wall time in ms
console.log(new Date(1700000000123).toISOString()); // fixed sample
2023-11-14T22:13:20.123Z
Language differences
C: gettimeofday is the traditional read (example, seconds + microseconds as two fields); modern POSIX code uses clock_gettime — CLOCK_MONOTONIC for elapsed measurement, CLOCK_REALTIME for wall time, nanosecond resolution.
C++: chrono is a pure duration/clock model (the example formats a duration): system_clock is wall time and steady_clock is the monotonic must-use for measuring; calendar conversion needs C++20 calendar types or the C API.
C#: wall-clock DateTimeOffset.Now precision depends on the OS (Windows granularity ~15ms; calibrate against Stopwatch.Frequency); use Stopwatch for measuring (per the example comment).
Go: the Time returned by time.Now embeds a monotonic clock reading — Sub/Since within one process survive clock adjustments; the monotonic part is stripped when timestamps leave the process.
Java: System.currentTimeMillis() is wall-clock milliseconds and System.nanoTime() is monotonic nanoseconds (per the example comment) — never mix them; nanoTime is not a calendar time; jsr310's Instant carries nanosecond fields.
JavaScript: Date.now() is wall-clock milliseconds only; use performance.now() for measuring (monotonic, sub-millisecond) — the two APIs differ in both precision and monotonicity.
PHP: microtime(false) returns the "microsec sec" string (a 1970s-compatibility format), microtime(true) returns float seconds (example) — modern code takes the float form or hrtime(true) directly (nanosecond integer, monotonic).
Python: time.time() is float wall-clock seconds; measure with time.monotonic()/perf_counter() (per the example comment) — fromtimestamp with a float second limits sub-second precision to the double (~0.2µs).
Rust: std's Instant (monotonic) and SystemTime (wall) have clear roles (the example splits secs + nanos from SystemTime); date formatting needs chrono, while Instant::elapsed suffices for measuring.
TypeScript: same as JavaScript; performance.now() exists in browsers and Node (perf_hooks/global) — origins differ but monotonicity is the same.