Windows 驅動:擷取系統目前時間,產生格式字串

來源:互聯網
上載者:User

可以按照以下步驟:

1. 用 KeQuerySystemTime() 獲得當前的 GMT System Time. 這是一個從 1601-01-01 以來的計數(單位是 100ns)。

2. 調用 ExSystemTimeToLocalTime() 將 GMT System Time 值轉換成當前時區的 Local System Time.

3. 用 RtlTimeToTimeFields() 將 System Time 值轉換成 年:月:日:時:分:秒 的形式,儲存在一個 TIME_FIELDS 結構中。

typedef struct TIME_FIELDS 

CSHORT Year; 
CSHORT Month; 
CSHORT Day; 
CSHORT Hour; 
CSHORT Minute; 
CSHORT Second; 
CSHORT Milliseconds; 
CSHORT Weekday; 
} TIME_FIELDS; 


範例程式碼,擷取時間串函數:

(本函數將會在後文中使用:Windows 驅動:向 DbgPrintf 一樣將調試資訊輸出到檔案 )

(PS:原始碼有更好的格式發表嗎)

//----------------------------------------------------------------------
//
// GetCurrentTimeString
//
// Get current time string. (format: %d-%02d-%02d %02d:%02d:%02d)
//
//----------------------------------------------------------------------
PCHAR
GetCurrentTimeString()
{
 static CHAR  szTime[128];
 LARGE_INTEGER SystemTime;
 LARGE_INTEGER LocalTime;
 TIME_FIELDS  timeFiled;

 KeQuerySystemTime(&SystemTime);
 ExSystemTimeToLocalTime(&SystemTime, &LocalTime);
 RtlTimeToTimeFields(&LocalTime, &timeFiled);
 sprintf(szTime, "%d-%02d-%02d %02d:%02d:%02d"
  , timeFiled.Year
  , timeFiled.Month
  , timeFiled.Day
  , timeFiled.Hour
  , timeFiled.Minute
  , timeFiled.Second
  );

    return szTime;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.