Git Product home page Git Product logo

Comments (1)

gaddlord avatar gaddlord commented on August 24, 2024

Here is an FFI implementation for Windows.

import 'dart:ffi' as ffi;
import 'package:ffi/ffi.dart';

/// Frees allocated memory.
///
/// `calloc.free` and `malloc.free` do the same thing, so this works regardless
/// of whether memory was zero-allocated on creation or not.
void free(final ffi.Pointer pointer) => calloc.free(pointer);

final _kernel32 = ffi.DynamicLibrary.open('kernel32.dll');

/// Contains a 64-bit value representing the number of 100-nanosecond
/// intervals since January 1, 1601 (UTC).
class FILETIME extends ffi.Struct {
  @ffi.Uint32()
  external int dwLowDateTime;

  @ffi.Uint32()
  external int dwHighDateTime;
}

/// Specifies a date and time, using individual members for the month, day,
/// year, weekday, hour, minute, second, and millisecond. The time is
/// either in coordinated universal time (UTC) or local time, depending on
/// the function that is being called.
class SYSTEMTIME extends ffi.Struct {
  @ffi.Uint16()
  external int wYear;

  @ffi.Uint16()
  external int wMonth;

  @ffi.Uint16()
  external int wDayOfWeek;

  @ffi.Uint16()
  external int wDay;

  @ffi.Uint16()
  external int wHour;

  @ffi.Uint16()
  external int wMinute;

  @ffi.Uint16()
  external int wSecond;

  @ffi.Uint16()
  external int wMilliseconds;
}

class Kernel32 {
  /// windows.h
  /// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess
  static int getCurrentProcess() => _getCurrentProcess();
  static final _getCurrentProcess =
      _kernel32.lookupFunction<ffi.IntPtr Function(), int Function()>(
    'GetCurrentProcess',
  );

  /// processthreadsapi.h
  /// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesstimes
  static bool getProcessTimes(
    final int hProcess,
    final ffi.Pointer<FILETIME> lpCreationTime,
    final ffi.Pointer<FILETIME> lpExitTime,
    final ffi.Pointer<FILETIME> lpKernelTime,
    final ffi.Pointer<FILETIME> lpUserTime,
  ) =>
      _getProcessTimes(
        hProcess,
        lpCreationTime,
        lpExitTime,
        lpKernelTime,
        lpUserTime,
      );
  static final _getProcessTimes = _kernel32.lookupFunction<
      ffi.Bool Function(
    ffi.IntPtr hProcess,
    ffi.Pointer<FILETIME> lpCreationTime,
    ffi.Pointer<FILETIME> lpExitTime,
    ffi.Pointer<FILETIME> lpKernelTime,
    ffi.Pointer<FILETIME> lpUserTime,
  ),
      bool Function(
    int hProcess,
    ffi.Pointer<FILETIME> lpCreationTime,
    ffi.Pointer<FILETIME> lpExitTime,
    ffi.Pointer<FILETIME> lpKernelTime,
    ffi.Pointer<FILETIME> lpUserTime,
  )>('GetProcessTimes');

  /// Retrieves the current local date and time.
  /// ```c
  /// void GetSystemTime(LPSYSTEMTIME lpSystemTime);
  /// ```
  static void getSystemTime(final ffi.Pointer<SYSTEMTIME> lpSystemTime) =>
      _getSystemTime(lpSystemTime);

  static final _getSystemTime = _kernel32.lookupFunction<
      ffi.Void Function(ffi.Pointer<SYSTEMTIME> lpSystemTime),
      void Function(ffi.Pointer<SYSTEMTIME> lpSystemTime)>('GetSystemTime');

  // ignore: lines_longer_than_80_chars
  /// Converts a file time to system time format. System time is based on
  /// Coordinated Universal Time (UTC).
  /// ```c
  /// BOOL FileTimeToSystemTime(const FILETIME *lpFileTime,
  ///   LPSYSTEMTIME lpSystemTime);
  /// ```
  static int fileTimeToSystemTime(
    final ffi.Pointer<FILETIME> lpFileTime,
    final ffi.Pointer<SYSTEMTIME> lpSystemTime,
  ) =>
      _fileTimeToSystemTime(lpFileTime, lpSystemTime);

  static final _fileTimeToSystemTime = _kernel32.lookupFunction<
      ffi.Int32 Function(
    ffi.Pointer<FILETIME> lpFileTime,
    ffi.Pointer<SYSTEMTIME> lpSystemTime,
  ),
      int Function(
    ffi.Pointer<FILETIME> lpFileTime,
    ffi.Pointer<SYSTEMTIME> lpSystemTime,
  )>('FileTimeToSystemTime');
}

class OsHelper {
  static DateTime systemTimeToDateTime(
    final ffi.Pointer<SYSTEMTIME> systemTime,
  ) =>
      DateTime(
        systemTime.ref.wYear,
        systemTime.ref.wMonth,
        systemTime.ref.wDay,
        systemTime.ref.wHour,
        systemTime.ref.wMinute,
        systemTime.ref.wSecond,
        systemTime.ref.wMilliseconds,
      );

  static Duration getUpTimeInSeconds() {
    final lpCreationTime = calloc<FILETIME>();
    final lpExitTime = calloc<FILETIME>();
    final lpKernelTime = calloc<FILETIME>();
    final lpUserTime = calloc<FILETIME>();
    try {
      if (Kernel32.getProcessTimes(
        Kernel32.getCurrentProcess(),
        lpCreationTime,
        lpExitTime,
        lpKernelTime,
        lpUserTime,
      )) {
        final lpSystemTime = calloc<SYSTEMTIME>();
        try {
          Kernel32.getSystemTime(lpSystemTime);

          final dtCurrentTime = systemTimeToDateTime(lpSystemTime);
          print(dtCurrentTime);
          Kernel32.fileTimeToSystemTime(lpCreationTime, lpSystemTime);
          final dtCreationTime = systemTimeToDateTime(lpSystemTime);
          print(dtCreationTime);
          return dtCurrentTime.difference(dtCreationTime);
        } finally {
          free(lpSystemTime);
        }
      }
    } finally {
      free(lpCreationTime);
      free(lpExitTime);
      free(lpKernelTime);
      free(lpUserTime);
    }
    return Duration.zero;
  }
}

from system_clock.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.