2 * Copyright (c) 2014-present, Facebook, Inc.
5 * This source code is licensed in accordance with the terms specified in
6 * the LICENSE file found in the root directory of this source tree.
11 #include <boost/algorithm/string/trim.hpp>
13 #include <osquery/utils/system/time.h>
15 #include <osquery/system.h>
16 #include <osquery/tables.h>
22 QueryData genTime(QueryContext& context) {
24 time_t local_time = getUnixTime();
25 auto osquery_time = getUnixTime();
26 auto osquery_timestamp = getAsciiTime();
28 // The concept of 'now' is configurable.
30 gmtime_r(&local_time, &gmt);
33 localtime_r(&local_time, &now);
36 localtime_r(&local_time, &local);
37 local_time = std::mktime(&local);
39 char weekday[10] = {0};
40 strftime(weekday, sizeof(weekday), "%A", &now);
42 char timezone[5] = {0};
43 strftime(timezone, sizeof(timezone), "%Z", &now);
45 char local_timezone[5] = {0};
46 strftime(local_timezone, sizeof(local_timezone), "%Z", &local);
48 char iso_8601[21] = {0};
49 strftime(iso_8601, sizeof(iso_8601), "%FT%TZ", &gmt);
51 if (context.isColumnUsed("win_timestamp")) {
53 GetSystemTimeAsFileTime(&ft);
54 LARGE_INTEGER li = {0};
55 li.LowPart = ft.dwLowDateTime;
56 li.HighPart = ft.dwHighDateTime;
57 long long int hns = li.QuadPart;
58 r["win_timestamp"] = BIGINT(hns);
61 r["weekday"] = SQL_TEXT(weekday);
62 r["year"] = INTEGER(now.tm_year + 1900);
63 r["month"] = INTEGER(now.tm_mon + 1);
64 r["day"] = INTEGER(now.tm_mday);
65 r["hour"] = INTEGER(now.tm_hour);
66 r["minutes"] = INTEGER(now.tm_min);
67 r["seconds"] = INTEGER(now.tm_sec);
68 r["timezone"] = SQL_TEXT(timezone);
69 if (r["timezone"].empty()) {
70 r["timezone"] = "UTC";
73 r["local_time"] = INTEGER(local_time);
74 r["local_timezone"] = SQL_TEXT(local_timezone);
75 if (r["local_timezone"].empty()) {
76 r["local_timezone"] = "UTC";
79 r["unix_time"] = INTEGER(osquery_time);
80 r["timestamp"] = SQL_TEXT(osquery_timestamp);
81 // Date time is provided in ISO 8601 format, then duplicated in iso_8601.
82 r["datetime"] = SQL_TEXT(iso_8601);
83 r["iso_8601"] = SQL_TEXT(iso_8601);
90 } // namespace osquery