Rainmeter-0.14-src\Library\MeasureTime.cpp Rainmeter-0.14-r2-src\Library\MeasureTime.cpp
49#include "MeasureTime.h" 49#include "MeasureTime.h"
50#include "Rainmeter.h" 50#include "Rainmeter.h"
51#include <time.h> 51#include <time.h>
52  52 
  53int GetYearDay(int year, int month, int day)
  54{
  55    int yearDay = 0;
  56    UINT dates[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  57 
  58    for (int i = 0; i < month - 1; i++)  
  59    {
  60        yearDay += dates[i];
  61    }
  62 
  63    if (month > 2 && ((((year % 4) == 0) && ((year % 100) != 0)) || (year  % 400) == 0))
  64    {
  65        yearDay++;
  66    }
  67 
  68    yearDay += day;
  69 
  70    return yearDay - 1;
  71}
  72 
53/* 73/*
54** CMeasureTime 74** CMeasureTime
55** 75**
56** The constructor 76** The constructor
57** 77**
58*/  78*/ 
59CMeasureTime::CMeasureTime(CMeterWindow* meterWindow) : CMeasure(meterWindow) 79CMeasureTime::CMeasureTime(CMeterWindow* meterWindow) : CMeasure(meterWindow)
60{ 80{
61    /* Set time zone from TZ environment variable. If TZ is not set, 81    /* Set time zone from TZ environment variable. If TZ is not set,
62     * the operating system is queried to obtain the default value 82     * the operating system is queried to obtain the default value
63     * for the variable. 83     * for the variable.
64     */  84     */ 
65    _tzset(); 85    _tzset();
66  86 
67    m_DeltaTime.QuadPart = 0; 87    m_DeltaTime.QuadPart = 0;
68    m_Time.QuadPart = 0; 88    m_Time.QuadPart = 0;
69} 89}
70  90 
71/* 91/*
72** ~CMeasureTime 92** ~CMeasureTime
73** 93**
74** The destructor 94** The destructor
75** 95**
76*/  96*/ 
77CMeasureTime::~CMeasureTime() 97CMeasureTime::~CMeasureTime()
78{ 98{
79} 99}
80  100 
81/* 101/*
82** Update 102** Update
83** 103**
84** Updates the current time 104** Updates the current time
85** 105**
86*/  106*/ 
87bool CMeasureTime::Update() 107bool CMeasureTime::Update()
88{ 108{
89    if (!CMeasure::PreUpdate()) return false; 109    if (!CMeasure::PreUpdate()) return false;
90  110 
91    FILETIME ftUTCTime; 111    FILETIME ftUTCTime;
92    GetSystemTimeAsFileTime(&ftUTCTime); 112    GetSystemTimeAsFileTime(&ftUTCTime);
93  113 
94    // Modify the ltime to match the current timezone 114    // Modify the ltime to match the current timezone
95    // This way we can use the value also for the clock 115    // This way we can use the value also for the clock
96    m_Time.HighPart = ftUTCTime.dwHighDateTime; 116    m_Time.HighPart = ftUTCTime.dwHighDateTime;
97    m_Time.LowPart = ftUTCTime.dwLowDateTime; 117    m_Time.LowPart = ftUTCTime.dwLowDateTime;
98  118 
99    m_Time.QuadPart += m_DeltaTime.QuadPart; 119    m_Time.QuadPart += m_DeltaTime.QuadPart;
100    m_Value = (double)(m_Time.QuadPart / 10000000); 120    m_Value = (double)(m_Time.QuadPart / 10000000);
101  121 
102    if (m_Format.size() != 0) 122    if (m_Format.size() != 0)
103    { 123    {
104        // If there is some date format, parse the value from it instead 124        // If there is some date format, parse the value from it instead
105        WCHAR tmpSz[MAX_LINE_LENGTH]; 125        WCHAR tmpSz[MAX_LINE_LENGTH];
106        SYSTEMTIME sysToday; 126        SYSTEMTIME sysToday;
107        FILETIME ftToday; 127        FILETIME ftToday;
108  128 
109        ftToday.dwHighDateTime = m_Time.HighPart; 129        ftToday.dwHighDateTime = m_Time.HighPart;
110        ftToday.dwLowDateTime = m_Time.LowPart; 130        ftToday.dwLowDateTime = m_Time.LowPart;
111  131 
112        FileTimeToSystemTime(&ftToday, &sysToday); 132        FileTimeToSystemTime(&ftToday, &sysToday);
113  133 
114        if (wcsicmp(L"locale-time", m_Format.c_str()) == 0) 134        if (wcsicmp(L"locale-time", m_Format.c_str()) == 0)
115        { 135        {
116            GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysToday, NULL, tmpSz, MAX_LINE_LENGTH); 136            GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysToday, NULL, tmpSz, MAX_LINE_LENGTH);
117        } 137        }
118        else if (wcsicmp(L"locale-date", m_Format.c_str()) == 0) 138        else if (wcsicmp(L"locale-date", m_Format.c_str()) == 0)
119        { 139        {
120            GetDateFormat(LOCALE_USER_DEFAULT, 0, &sysToday, NULL, tmpSz, MAX_LINE_LENGTH); 140            GetDateFormat(LOCALE_USER_DEFAULT, 0, &sysToday, NULL, tmpSz, MAX_LINE_LENGTH);
121        } 141        }
122        else 142        else
123        { 143        {
124            struct tm today; 144            struct tm today;
125            today.tm_isdst = 0; 145            today.tm_isdst = 0;
126            today.tm_hour = sysToday.wHour; 146            today.tm_hour = sysToday.wHour;
127            today.tm_mday = sysToday.wDay; 147            today.tm_mday = sysToday.wDay;
128            today.tm_min = sysToday.wMinute; 148            today.tm_min = sysToday.wMinute;
129            today.tm_mon = sysToday.wMonth - 1; 149            today.tm_mon = sysToday.wMonth - 1;
130            today.tm_sec = sysToday.wSecond; 150            today.tm_sec = sysToday.wSecond;
131            today.tm_wday = sysToday.wDayOfWeek; 151            today.tm_wday = sysToday.wDayOfWeek;
132            today.tm_yday  = 0; 152            today.tm_yday = GetYearDay(sysToday.wYear, sysToday.wMonth, sysToday.wDay);
133            today.tm_year = sysToday.wYear - 1900; 153            today.tm_year = sysToday.wYear - 1900;
134  154 
135            wcsftime(tmpSz, MAX_LINE_LENGTH, m_Format.c_str(), &today); 155            wcsftime(tmpSz, MAX_LINE_LENGTH, m_Format.c_str(), &today);
136        } 156        }
137  157 
138        m_Value = wcstod(tmpSz, NULL); 158        m_Value = wcstod(tmpSz, NULL);
139    } 159    }
140  160 
141    return PostUpdate(); 161    return PostUpdate();
142} 162}
143  163 
144int GetYearDay(int year, int month, int day)  
145{  
146    int yearDay = 0;  
147    UINT dates[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  
148   
149    for (int i = 0; i < month - 1; i++)    
150    {  
151        yearDay += dates[i];  
152    }  
153   
154    if (month > 2 && ((((year % 4) == 0) && ((year % 100) != 0)) || (year  % 400) == 0))  
155    {  
156        yearDay++;  
157    }  
158   
159    yearDay += day;  
160   
161    return yearDay - 1;  
162}