Remove gflags dependency on osquery
[platform/core/security/vist.git] / src / osquery / tables / utility / time.cpp
1 /**
2  *  Copyright (c) 2014-present, Facebook, Inc.
3  *  All rights reserved.
4  *
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.
7  */
8
9 #include <ctime>
10
11 #include <boost/algorithm/string/trim.hpp>
12
13 #include <osquery/utils/system/time.h>
14
15 #include <osquery/system.h>
16 #include <osquery/tables.h>
17
18 namespace osquery {
19
20 namespace tables {
21
22 QueryData genTime(QueryContext& context) {
23   Row r;
24   time_t local_time = getUnixTime();
25   auto osquery_time = getUnixTime();
26   auto osquery_timestamp = getAsciiTime();
27
28   // The concept of 'now' is configurable.
29   struct tm gmt;
30   gmtime_r(&local_time, &gmt);
31
32   struct tm now;
33   localtime_r(&local_time, &now);
34
35   struct tm local;
36   localtime_r(&local_time, &local);
37   local_time = std::mktime(&local);
38
39   char weekday[10] = {0};
40   strftime(weekday, sizeof(weekday), "%A", &now);
41
42   char timezone[5] = {0};
43   strftime(timezone, sizeof(timezone), "%Z", &now);
44
45   char local_timezone[5] = {0};
46   strftime(local_timezone, sizeof(local_timezone), "%Z", &local);
47
48   char iso_8601[21] = {0};
49   strftime(iso_8601, sizeof(iso_8601), "%FT%TZ", &gmt);
50 #ifdef WIN32
51   if (context.isColumnUsed("win_timestamp")) {
52     FILETIME ft = {0};
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);
59   }
60 #endif
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";
71   }
72
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";
77   }
78
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);
84
85   QueryData results;
86   results.push_back(r);
87   return results;
88 }
89 } // namespace tables
90 } // namespace osquery