2015-11-12 16:31:30 -08:00
|
|
|
//! @file setdate.cpp
|
|
|
|
|
//! @author George FLeming <v-geflem@microsoft.com>
|
|
|
|
|
//! @brief set local/system date and time
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <langinfo.h>
|
|
|
|
|
#include <locale.h>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include "setdate.h"
|
|
|
|
|
|
|
|
|
|
//! @brief SetDate sets the date and time on local computer. You must
|
|
|
|
|
//! be super-user to set the time.
|
|
|
|
|
//!
|
|
|
|
|
//! SetDate
|
|
|
|
|
//!
|
|
|
|
|
//! @param[in] info
|
|
|
|
|
//! @parblock
|
|
|
|
|
//! A struct that contains program to execute and its parameters
|
|
|
|
|
//!
|
|
|
|
|
//! @exception errno Passes these errors via errno to GetLastError:
|
|
|
|
|
//! - ERROR_BAD_ENVIRONMENT: locale is not UTF-8
|
|
|
|
|
//! - ERROR_INVALID_PARAMETER: time was not passed in correctly
|
|
|
|
|
//! - ERROR_ACCESS_DENIED: you must be super-user to set the date
|
|
|
|
|
//!
|
2015-11-13 10:05:55 -08:00
|
|
|
//! @retval 0 successfully set date
|
2015-11-12 16:31:30 -08:00
|
|
|
//! @retval -1 if failure occurred. To get extended error information, call GetLastError.
|
|
|
|
|
//!
|
|
|
|
|
|
2015-11-13 10:05:55 -08:00
|
|
|
int32_t SetDate(const SetDateInfo &info)
|
2015-11-12 16:31:30 -08:00
|
|
|
{
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
|
|
// Select locale from environment
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
// Check that locale is UTF-8
|
|
|
|
|
if (nl_langinfo(CODESET) != std::string("UTF-8"))
|
|
|
|
|
{
|
|
|
|
|
errno = ERROR_BAD_ENVIRONMENT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct tm bdTime;
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
|
|
bdTime.tm_year = info.Year - 1900;
|
|
|
|
|
bdTime.tm_mon = info.Month - 1; // This is zero-based
|
|
|
|
|
bdTime.tm_mday = info.Day;
|
|
|
|
|
bdTime.tm_hour = info.Hour;
|
|
|
|
|
bdTime.tm_min = info.Minute;
|
2015-11-13 10:05:55 -08:00
|
|
|
bdTime.tm_sec = info.Second;
|
2015-11-12 16:31:30 -08:00
|
|
|
bdTime.tm_isdst = info.DST;
|
|
|
|
|
|
|
|
|
|
time_t newTime = mktime(&bdTime);
|
|
|
|
|
if (newTime == -1)
|
|
|
|
|
{
|
|
|
|
|
errno = ERROR_INVALID_PARAMETER;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tv.tv_sec = newTime;
|
2015-11-13 10:05:55 -08:00
|
|
|
tv.tv_usec = 0;
|
2015-11-12 16:31:30 -08:00
|
|
|
|
|
|
|
|
int result = settimeofday(&tv, NULL);
|
|
|
|
|
if (result == -1)
|
|
|
|
|
{
|
|
|
|
|
errno = ERROR_ACCESS_DENIED;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|