gettimeofday method

int gettimeofday(
  1. TimeVal tv, [
  2. TimeZone? tz
])

The gettimeofday() function obtains the current time, expressed as seconds and microseconds since the Epoch, and stores it in the TimeVal structure pointed to by tv.

The tz argument is often null (or omitted) in modern C, but provided here for completeness. Returns 0 on success, or -1 on failure.

Implementation

int gettimeofday(TimeVal tv, [TimeZone? tz]) {
  final now = DateTime.now();
  final microsecondsSinceEpoch = now.microsecondsSinceEpoch;

  tv.tv_sec = microsecondsSinceEpoch ~/ 1000000;
  tv.tv_usec = microsecondsSinceEpoch % 1000000;

  if (tz != null) {
    tz.tz_minuteswest = -now.timeZoneOffset.inMinutes;
    tz.tz_dsttime = 0; // Dart doesn't natively expose DST type directly without heavy computation, 0 is safe obsolete default.
  }

  return 0; // Success
}