2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2001 Kimmo Pekkola
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-02-10 18:37:48 +00:00
|
|
|
*/
|
|
|
|
|
2009-10-07 16:45:14 +00:00
|
|
|
#include "StdAfx.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
#include "MeasureTime.h"
|
|
|
|
#include "Rainmeter.h"
|
2010-08-07 19:03:58 +00:00
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
int GetYearDay(int year, int month, int day)
|
|
|
|
{
|
2012-08-29 19:02:00 +00:00
|
|
|
static const int dates[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
|
|
|
|
int yearDay = dates[month - 1] + day;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-08-29 19:02:00 +00:00
|
|
|
if (month > 2 && ((((year % 4) == 0) && ((year % 100) != 0)) || (year % 400) == 0))
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-03-30 22:37:05 +00:00
|
|
|
++yearDay;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return yearDay - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
MeasureTime::MeasureTime(MeterWindow* meterWindow, const WCHAR* name) : Measure(meterWindow, name),
|
2011-01-29 00:11:01 +00:00
|
|
|
m_DeltaTime(),
|
2012-08-29 15:59:36 +00:00
|
|
|
m_Time(),
|
|
|
|
m_TimeStamp(-1)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
/* Set time zone from TZ environment variable. If TZ is not set,
|
2011-03-29 19:21:57 +00:00
|
|
|
* the operating system is queried to obtain the default value
|
|
|
|
* for the variable.
|
2010-09-21 16:45:29 +00:00
|
|
|
*/
|
2010-09-21 22:47:53 +00:00
|
|
|
_tzset();
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
MeasureTime::~MeasureTime()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-07 19:03:58 +00:00
|
|
|
/*
|
|
|
|
** Converts given time to string.
|
|
|
|
** This function is a wrapper function for wcsftime.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureTime::TimeToString(WCHAR* buf, size_t bufLen, const WCHAR* format, const struct tm* time)
|
2010-08-07 19:03:58 +00:00
|
|
|
{
|
|
|
|
if (bufLen > 0)
|
|
|
|
{
|
2010-12-15 22:03:36 +00:00
|
|
|
_invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(RmNullCRTInvalidParameterHandler);
|
2010-08-07 19:03:58 +00:00
|
|
|
_CrtSetReportMode(_CRT_ASSERT, 0);
|
|
|
|
|
|
|
|
errno = 0;
|
2012-04-07 14:42:39 +00:00
|
|
|
wcsftime(buf, bufLen, format, time);
|
2010-08-07 19:03:58 +00:00
|
|
|
if (errno == EINVAL)
|
|
|
|
{
|
2013-06-29 16:59:18 +00:00
|
|
|
LogErrorF(this, L"Time: \"Format=%s\" invalid", format);
|
2010-08-07 19:03:58 +00:00
|
|
|
buf[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_set_invalid_parameter_handler(oldHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureTime::FillCurrentTime()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-08-29 15:59:36 +00:00
|
|
|
if (m_TimeStamp < 0.0)
|
|
|
|
{
|
|
|
|
FILETIME ftUTCTime;
|
|
|
|
GetSystemTimeAsFileTime(&ftUTCTime);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-08-29 15:59:36 +00:00
|
|
|
// Modify the ltime to match the current timezone
|
|
|
|
// This way we can use the value also for the clock
|
|
|
|
m_Time.HighPart = ftUTCTime.dwHighDateTime;
|
|
|
|
m_Time.LowPart = ftUTCTime.dwLowDateTime;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-08-29 15:59:36 +00:00
|
|
|
m_Time.QuadPart += m_DeltaTime.QuadPart;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Time.QuadPart = (LONGLONG)(m_TimeStamp * 10000000);
|
|
|
|
}
|
2012-08-15 02:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Updates the current time
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureTime::UpdateValue()
|
2012-08-15 02:51:53 +00:00
|
|
|
{
|
|
|
|
FillCurrentTime();
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-11-08 17:21:29 +00:00
|
|
|
if (!m_Format.empty())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
// If there is some date format, parse the value from it instead
|
2011-02-15 13:22:19 +00:00
|
|
|
WCHAR* tmpSz = new WCHAR[MAX_LINE_LENGTH];
|
2009-02-10 18:37:48 +00:00
|
|
|
SYSTEMTIME sysToday;
|
|
|
|
FILETIME ftToday;
|
|
|
|
|
2010-08-07 19:03:58 +00:00
|
|
|
tmpSz[0] = 0;
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
ftToday.dwHighDateTime = m_Time.HighPart;
|
|
|
|
ftToday.dwLowDateTime = m_Time.LowPart;
|
|
|
|
|
|
|
|
FileTimeToSystemTime(&ftToday, &sysToday);
|
|
|
|
|
2011-11-16 16:47:20 +00:00
|
|
|
const WCHAR* format = m_Format.c_str();
|
|
|
|
if (_wcsicmp(L"locale-time", format) == 0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysToday, nullptr, tmpSz, MAX_LINE_LENGTH);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2011-11-16 16:47:20 +00:00
|
|
|
else if (_wcsicmp(L"locale-date", format) == 0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
GetDateFormat(LOCALE_USER_DEFAULT, 0, &sysToday, nullptr, tmpSz, MAX_LINE_LENGTH);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct tm today;
|
|
|
|
today.tm_isdst = 0;
|
|
|
|
today.tm_hour = sysToday.wHour;
|
|
|
|
today.tm_mday = sysToday.wDay;
|
|
|
|
today.tm_min = sysToday.wMinute;
|
|
|
|
today.tm_mon = sysToday.wMonth - 1;
|
|
|
|
today.tm_sec = sysToday.wSecond;
|
|
|
|
today.tm_wday = sysToday.wDayOfWeek;
|
|
|
|
today.tm_yday = GetYearDay(sysToday.wYear, sysToday.wMonth, sysToday.wDay);
|
|
|
|
today.tm_year = sysToday.wYear - 1900;
|
|
|
|
|
2011-11-16 16:47:20 +00:00
|
|
|
TimeToString(tmpSz, MAX_LINE_LENGTH, format, &today);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:28:39 +00:00
|
|
|
m_Value = wcstod(tmpSz, nullptr);
|
2011-02-15 13:22:19 +00:00
|
|
|
|
|
|
|
delete [] tmpSz;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2011-01-24 10:15:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Value = (double)(m_Time.QuadPart / 10000000);
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Returns the time as string.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
const WCHAR* MeasureTime::GetStringValue()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
static WCHAR tmpSz[MAX_LINE_LENGTH];
|
|
|
|
struct tm today;
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2010-08-07 19:03:58 +00:00
|
|
|
tmpSz[0] = 0;
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
SYSTEMTIME sysToday;
|
|
|
|
FILETIME ftToday;
|
|
|
|
ftToday.dwHighDateTime = m_Time.HighPart;
|
|
|
|
ftToday.dwLowDateTime = m_Time.LowPart;
|
|
|
|
|
|
|
|
FileTimeToSystemTime(&ftToday, &sysToday);
|
|
|
|
|
|
|
|
today.tm_isdst = 0;
|
|
|
|
today.tm_hour = sysToday.wHour;
|
|
|
|
today.tm_mday = sysToday.wDay;
|
|
|
|
today.tm_min = sysToday.wMinute;
|
|
|
|
today.tm_mon = sysToday.wMonth - 1;
|
|
|
|
today.tm_sec = sysToday.wSecond;
|
|
|
|
today.tm_wday = sysToday.wDayOfWeek;
|
|
|
|
today.tm_yday = GetYearDay(sysToday.wYear, sysToday.wMonth, sysToday.wDay);
|
|
|
|
today.tm_year = sysToday.wYear - 1900;
|
|
|
|
|
|
|
|
// Create the string
|
2011-11-08 17:21:29 +00:00
|
|
|
if (!m_Format.empty())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-11-16 16:47:20 +00:00
|
|
|
const WCHAR* format = m_Format.c_str();
|
|
|
|
if (_wcsicmp(L"locale-time", format) == 0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysToday, nullptr, tmpSz, MAX_LINE_LENGTH);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2011-11-16 16:47:20 +00:00
|
|
|
else if (_wcsicmp(L"locale-date", format) == 0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
GetDateFormat(LOCALE_USER_DEFAULT, 0, &sysToday, nullptr, tmpSz, MAX_LINE_LENGTH);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-16 16:47:20 +00:00
|
|
|
TimeToString(tmpSz, MAX_LINE_LENGTH, format, &today);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-07 17:05:16 +00:00
|
|
|
TimeToString(tmpSz, MAX_LINE_LENGTH, L"%H:%M:%S", &today);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2012-04-07 17:05:16 +00:00
|
|
|
|
|
|
|
return CheckSubstitute(tmpSz);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-06-01 13:06:36 +00:00
|
|
|
** Read the options specified in the ini file.
|
2009-02-10 18:37:48 +00:00
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureTime::ReadOptions(ConfigParser& parser, const WCHAR* section)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
Measure::ReadOptions(parser, section);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
m_Format = parser.ReadString(section, L"Format", L"");
|
|
|
|
|
2012-08-29 15:59:36 +00:00
|
|
|
m_TimeStamp = parser.ReadFloat(section, L"TimeStamp", -1);
|
|
|
|
|
2012-08-29 19:02:00 +00:00
|
|
|
if (m_TimeStamp < 0.0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-08-29 19:02:00 +00:00
|
|
|
const WCHAR* timezone = parser.ReadString(section, L"TimeZone", L"local").c_str();
|
|
|
|
if (_wcsicmp(L"local", timezone) == 0)
|
|
|
|
{
|
|
|
|
SYSTEMTIME sysLocalTime, sysUTCTime;
|
|
|
|
GetLocalTime(&sysLocalTime);
|
|
|
|
GetSystemTime(&sysUTCTime);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-08-29 19:02:00 +00:00
|
|
|
FILETIME ftLocalTime, ftUTCTime;
|
|
|
|
SystemTimeToFileTime(&sysLocalTime, &ftLocalTime);
|
|
|
|
SystemTimeToFileTime(&sysUTCTime, &ftUTCTime);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-08-29 19:02:00 +00:00
|
|
|
LARGE_INTEGER largeInt1, largeInt2;
|
|
|
|
largeInt1.HighPart = ftLocalTime.dwHighDateTime;
|
|
|
|
largeInt1.LowPart = ftLocalTime.dwLowDateTime;
|
|
|
|
largeInt2.HighPart = ftUTCTime.dwHighDateTime;
|
|
|
|
largeInt2.LowPart = ftUTCTime.dwLowDateTime;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-08-29 19:02:00 +00:00
|
|
|
m_DeltaTime.QuadPart = largeInt1.QuadPart - largeInt2.QuadPart;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-24 14:22:46 +00:00
|
|
|
double zone = parser.ParseDouble(timezone, 0.0);
|
2013-07-31 10:36:53 +00:00
|
|
|
bool dst = parser.ReadBool(section, L"DaylightSavingTime", true);
|
2012-08-29 19:02:00 +00:00
|
|
|
|
|
|
|
struct tm* today;
|
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
today = localtime(&now);
|
|
|
|
|
|
|
|
if (dst && today->tm_isdst)
|
|
|
|
{
|
|
|
|
// Add DST
|
|
|
|
TIME_ZONE_INFORMATION tzi;
|
|
|
|
GetTimeZoneInformation(&tzi);
|
|
|
|
|
|
|
|
m_DeltaTime.QuadPart = (LONGLONG)((zone * 3600) - tzi.DaylightBias * 60) * 10000000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_DeltaTime.QuadPart = (LONGLONG)(zone * 3600) * 10000000;
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-29 19:02:00 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_DeltaTime.QuadPart = 0;
|
|
|
|
}
|
2012-08-15 02:51:53 +00:00
|
|
|
|
|
|
|
if (!m_Initialized)
|
|
|
|
{
|
|
|
|
// Initialize m_Time to avoid causing EINVAL in TimeToString() until calling UpdateValue()
|
|
|
|
FillCurrentTime();
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|