CurrentUptime


/*++


Module Name:


    uptime.c


Abstract:


    This implements the unix "uptime" utility.


Author:


    Michael Wookey 30-Nov-2002 (ntutils@wookey.org)


Compiler:


    VC7


Build:


    cl uptime.c


--*/


#define UNICODE

#define STRICT

#define WIN32_LEAN_AND_MEAN


#include <windows.h>

#include <stdio.h>


//

// Structures required for NtQuerySystemInformation and RtlTimeToElapsedTimeFields.

//


typedef struct _SYSTEM_TIMEOFDAY_INFORMATION

{

    LARGE_INTEGER BootTime;

    LARGE_INTEGER CurrentTime;

    LARGE_INTEGER TimeZoneBias;

    ULONG CurrentTimeZoneId;

} 

SYSTEM_TIMEOFDAY_INFORMATION, *PSYSTEM_TIMEOFDAY_INFORMATION;


typedef struct _TIME_FIELDS 

{

    SHORT Year;

    SHORT Month;

    SHORT Day;

    SHORT Hour;

    SHORT Minute;

    SHORT Second;

    SHORT Milliseconds;

    SHORT Weekday;

} 

TIME_FIELDS, *PTIME_FIELDS;


//

// The information class that we are querying via NtQuerySystemInformation.

//


typedef enum _SYSTEM_INFORMATION_CLASS 

{

    SystemTimeOfDayInformation = 3

} 

SYSTEM_INFORMATION_CLASS;


//

// Function definitions. These are exported from ntdll.dll

//


typedef LONG ( NTAPI *pNtQuerySystemInformation )

    (

    IN  SYSTEM_INFORMATION_CLASS SystemInformationClass,

    OUT PVOID SystemInformation,

    IN  ULONG SystemInformationLength,

    OUT PULONG ReturnLength OPTIONAL 

    );


typedef VOID ( NTAPI *pRtlTimeToElapsedTimeFields )

    (

    IN  PLARGE_INTEGER Time, 

    OUT PTIME_FIELDS TimeFields 

    );


INT

CDECL

wmain( 

    IN INT Argc, 

    IN PWCHAR Argv[], 

    IN PWCHAR Envp[] 

    )

{

    TIME_FIELDS UpTime = { 0 };

    LARGE_INTEGER TimeNow = { 0 };

    SYSTEM_TIMEOFDAY_INFORMATION TimeOfDay = { 0 };

    SYSTEMTIME ClockTime = { 0 };

    WCHAR ClockString[64];

    pNtQuerySystemInformation NtQuerySystemInformation = 0;

    pRtlTimeToElapsedTimeFields RtlTimeToElapsedTimeFields = 0;


    UNREFERENCED_PARAMETER( Argc );

    UNREFERENCED_PARAMETER( Argv );

    UNREFERENCED_PARAMETER( Envp );


    //

    // Obtain the function pointers. NtDll is implicitly mapped into

    // our process space.

    //


    NtQuerySystemInformation = (pNtQuerySystemInformation) 

        GetProcAddress( GetModuleHandleW( L"ntdll" ), "NtQuerySystemInformation" );


    RtlTimeToElapsedTimeFields = (pRtlTimeToElapsedTimeFields) 

        GetProcAddress( GetModuleHandleW( L"ntdll" ), "RtlTimeToElapsedTimeFields" );


    //

    // Obtain the time information from the system.

    //


    NtQuerySystemInformation( 

        SystemTimeOfDayInformation,

        &TimeOfDay,

        sizeof TimeOfDay,

        0 );


    //

    // Calculate the delta between the current time and the boot time.

    //


    TimeNow.QuadPart = TimeOfDay.CurrentTime.QuadPart - TimeOfDay.BootTime.QuadPart;


    //

    // Convert the time.

    //


    RtlTimeToElapsedTimeFields( &TimeNow, &UpTime );


    //

    // Get the current wall time.

    //


    GetLocalTime( &ClockTime );


    //

    // Format the current wall time.

    //


    GetTimeFormatW( 

        LOCALE_USER_DEFAULT, 

        0, 

        &ClockTime, 

        L"HH':'mm':'ss", 

        ClockString, 

        64 );


    //

    // Output to the console.

    //


    wprintf( L"%s, up %d day(s), %d:%02d:%02d",

        ClockString,

        UpTime.Day,

        UpTime.Hour,

        UpTime.Minute,

        UpTime.Second );


    return 0;

}


/* EOF */