PageRenderTime 89ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/DateTime.cpp

https://gitlab.com/garbotron/stepmania
C++ | 352 lines | 256 code | 42 blank | 54 comment | 14 complexity | 479030284de473f8e92d60377b3774b6 MD5 | raw file
  1. #include "global.h"
  2. #include "DateTime.h"
  3. #include "RageUtil.h"
  4. #include "EnumHelper.h"
  5. #include "LuaManager.h"
  6. #include "LocalizedString.h"
  7. DateTime::DateTime()
  8. {
  9. Init();
  10. }
  11. void DateTime::Init()
  12. {
  13. ZERO( *this );
  14. }
  15. bool DateTime::operator<( const DateTime& other ) const
  16. {
  17. #define COMPARE( v ) if(v!=other.v) return v<other.v;
  18. COMPARE( tm_year );
  19. COMPARE( tm_mon );
  20. COMPARE( tm_mday );
  21. COMPARE( tm_hour );
  22. COMPARE( tm_min );
  23. COMPARE( tm_sec );
  24. #undef COMPARE
  25. // they're equal
  26. return false;
  27. }
  28. bool DateTime::operator==( const DateTime& other ) const
  29. {
  30. #define COMPARE(x) if( x!=other.x ) return false;
  31. COMPARE( tm_year );
  32. COMPARE( tm_mon );
  33. COMPARE( tm_mday );
  34. COMPARE( tm_hour );
  35. COMPARE( tm_min );
  36. COMPARE( tm_sec );
  37. #undef COMPARE
  38. return true;
  39. }
  40. bool DateTime::operator>( const DateTime& other ) const
  41. {
  42. #define COMPARE( v ) if(v!=other.v) return v>other.v;
  43. COMPARE( tm_year );
  44. COMPARE( tm_mon );
  45. COMPARE( tm_mday );
  46. COMPARE( tm_hour );
  47. COMPARE( tm_min );
  48. COMPARE( tm_sec );
  49. #undef COMPARE
  50. // they're equal
  51. return false;
  52. }
  53. DateTime DateTime::GetNowDateTime()
  54. {
  55. time_t now = time(NULL);
  56. tm tNow;
  57. localtime_r( &now, &tNow );
  58. DateTime dtNow;
  59. #define COPY_M( v ) dtNow.v = tNow.v;
  60. COPY_M( tm_year );
  61. COPY_M( tm_mon );
  62. COPY_M( tm_mday );
  63. COPY_M( tm_hour );
  64. COPY_M( tm_min );
  65. COPY_M( tm_sec );
  66. #undef COPY_M
  67. return dtNow;
  68. }
  69. DateTime DateTime::GetNowDate()
  70. {
  71. DateTime tNow = GetNowDateTime();
  72. tNow.StripTime();
  73. return tNow;
  74. }
  75. void DateTime::StripTime()
  76. {
  77. tm_hour = 0;
  78. tm_min = 0;
  79. tm_sec = 0;
  80. }
  81. // Common SQL/XML format: "YYYY-MM-DD HH:MM:SS"
  82. RString DateTime::GetString() const
  83. {
  84. RString s = ssprintf( "%d-%02d-%02d",
  85. tm_year+1900,
  86. tm_mon+1,
  87. tm_mday );
  88. if( tm_hour != 0 ||
  89. tm_min != 0 ||
  90. tm_sec != 0 )
  91. {
  92. s += ssprintf( " %02d:%02d:%02d",
  93. tm_hour,
  94. tm_min,
  95. tm_sec );
  96. }
  97. return s;
  98. }
  99. bool DateTime::FromString( const RString sDateTime )
  100. {
  101. Init();
  102. int ret;
  103. ret = sscanf( sDateTime, "%d-%d-%d %d:%d:%d",
  104. &tm_year,
  105. &tm_mon,
  106. &tm_mday,
  107. &tm_hour,
  108. &tm_min,
  109. &tm_sec );
  110. if( ret != 6 )
  111. {
  112. ret = sscanf( sDateTime, "%d-%d-%d",
  113. &tm_year,
  114. &tm_mon,
  115. &tm_mday );
  116. if( ret != 3 )
  117. {
  118. return false;
  119. }
  120. }
  121. tm_year -= 1900;
  122. tm_mon -= 1;
  123. return true;
  124. }
  125. RString DayInYearToString( int iDayInYear )
  126. {
  127. return ssprintf("DayInYear%03d",iDayInYear);
  128. }
  129. int StringToDayInYear( RString sDayInYear )
  130. {
  131. int iDayInYear;
  132. if( sscanf( sDayInYear, "DayInYear%d", &iDayInYear ) != 1 )
  133. return -1;
  134. return iDayInYear;
  135. }
  136. static const RString LAST_DAYS_NAME[NUM_LAST_DAYS] =
  137. {
  138. "Today",
  139. "Yesterday",
  140. "Day2Ago",
  141. "Day3Ago",
  142. "Day4Ago",
  143. "Day5Ago",
  144. "Day6Ago",
  145. };
  146. RString LastDayToString( int iLastDayIndex )
  147. {
  148. return LAST_DAYS_NAME[iLastDayIndex];
  149. }
  150. static const char *DAY_OF_WEEK_TO_NAME[DAYS_IN_WEEK] =
  151. {
  152. "Sunday",
  153. "Monday",
  154. "Tuesday",
  155. "Wednesday",
  156. "Thursday",
  157. "Friday",
  158. "Saturday",
  159. };
  160. RString DayOfWeekToString( int iDayOfWeekIndex )
  161. {
  162. return DAY_OF_WEEK_TO_NAME[iDayOfWeekIndex];
  163. }
  164. RString HourInDayToString( int iHourInDayIndex )
  165. {
  166. return ssprintf("Hour%02d", iHourInDayIndex);
  167. }
  168. static const char *MonthNames[] =
  169. {
  170. "January",
  171. "February",
  172. "March",
  173. "April",
  174. "May",
  175. "June",
  176. "July",
  177. "August",
  178. "September",
  179. "October",
  180. "November",
  181. "December",
  182. };
  183. XToString( Month );
  184. XToLocalizedString( Month );
  185. LuaXType( Month );
  186. RString LastWeekToString( int iLastWeekIndex )
  187. {
  188. switch( iLastWeekIndex )
  189. {
  190. case 0: return "ThisWeek"; break;
  191. case 1: return "LastWeek"; break;
  192. default: return ssprintf("Week%02dAgo",iLastWeekIndex); break;
  193. }
  194. }
  195. RString LastDayToLocalizedString( int iLastDayIndex )
  196. {
  197. RString s = LastDayToString( iLastDayIndex );
  198. s.Replace( "Day", "" );
  199. s.Replace( "Ago", " Ago" );
  200. return s;
  201. }
  202. RString LastWeekToLocalizedString( int iLastWeekIndex )
  203. {
  204. RString s = LastWeekToString( iLastWeekIndex );
  205. s.Replace( "Week", "" );
  206. s.Replace( "Ago", " Ago" );
  207. return s;
  208. }
  209. RString HourInDayToLocalizedString( int iHourIndex )
  210. {
  211. int iBeginHour = iHourIndex;
  212. iBeginHour--;
  213. wrap( iBeginHour, 24 );
  214. iBeginHour++;
  215. return ssprintf("%02d:00+", iBeginHour );
  216. }
  217. tm AddDays( tm start, int iDaysToMove )
  218. {
  219. /*
  220. * This causes problems on OS X, which doesn't correctly handle range that are below
  221. * their normal values (eg. mday = 0). According to the manpage, it should adjust them:
  222. *
  223. * "If structure members are outside their legal interval, they will be normalized (so
  224. * that, e.g., 40 October is changed into 9 November)."
  225. *
  226. * Instead, it appears to simply fail.
  227. *
  228. * Refs:
  229. * http://bugs.php.net/bug.php?id=10686
  230. * http://sourceforge.net/tracker/download.php?group_id=37892&atid=421366&file_id=79179&aid=91133
  231. *
  232. * Note "Log starting 2004-03-07 03:50:42"; mday is 7, and PrintCaloriesBurned calls us
  233. * with iDaysToMove = -7, resulting in an out-of-range value 0. This seems legal, but
  234. * OS X chokes on it.
  235. */
  236. /* start.tm_mday += iDaysToMove;
  237. time_t seconds = mktime( &start );
  238. ASSERT( seconds != (time_t)-1 );
  239. */
  240. /* This handles DST differently: it returns the time that was exactly n*60*60*24 seconds
  241. * ago, where the above code always returns the same time of day. I prefer the above
  242. * behavior, but I'm not sure that it mattersmatters. */
  243. time_t seconds = mktime( &start );
  244. seconds += iDaysToMove*60*60*24;
  245. tm time;
  246. localtime_r( &seconds, &time );
  247. return time;
  248. }
  249. tm GetYesterday( tm start )
  250. {
  251. return AddDays( start, -1 );
  252. }
  253. int GetDayOfWeek( tm time )
  254. {
  255. int iDayOfWeek = time.tm_wday;
  256. ASSERT( iDayOfWeek < DAYS_IN_WEEK );
  257. return iDayOfWeek;
  258. }
  259. tm GetNextSunday( tm start )
  260. {
  261. return AddDays( start, DAYS_IN_WEEK-GetDayOfWeek(start) );
  262. }
  263. tm GetDayInYearAndYear( int iDayInYearIndex, int iYear )
  264. {
  265. /* If iDayInYearIndex is 200, set the date to Jan 200th, and let mktime
  266. * round it. This shouldn't suffer from the OSX mktime() issue described
  267. * above, since we're not giving it negative values. */
  268. tm when;
  269. ZERO( when );
  270. when.tm_mon = 0;
  271. when.tm_mday = iDayInYearIndex+1;
  272. when.tm_year = iYear - 1900;
  273. time_t then = mktime( &when );
  274. localtime_r( &then, &when );
  275. return when;
  276. }
  277. LuaFunction( MonthToString, MonthToString( Enum::Check<Month>(L, 1) ) );
  278. LuaFunction( MonthToLocalizedString, MonthToLocalizedString( Enum::Check<Month>(L, 1) ) );
  279. LuaFunction( MonthOfYear, GetLocalTime().tm_mon );
  280. LuaFunction( DayOfMonth, GetLocalTime().tm_mday );
  281. LuaFunction( Hour, GetLocalTime().tm_hour );
  282. LuaFunction( Minute, GetLocalTime().tm_min );
  283. LuaFunction( Second, GetLocalTime().tm_sec );
  284. LuaFunction( Year, GetLocalTime().tm_year+1900 );
  285. LuaFunction( Weekday, GetLocalTime().tm_wday );
  286. LuaFunction( DayOfYear, GetLocalTime().tm_yday );
  287. /*
  288. * (c) 2001-2004 Chris Danford
  289. * All rights reserved.
  290. *
  291. * Permission is hereby granted, free of charge, to any person obtaining a
  292. * copy of this software and associated documentation files (the
  293. * "Software"), to deal in the Software without restriction, including
  294. * without limitation the rights to use, copy, modify, merge, publish,
  295. * distribute, and/or sell copies of the Software, and to permit persons to
  296. * whom the Software is furnished to do so, provided that the above
  297. * copyright notice(s) and this permission notice appear in all copies of
  298. * the Software and that both the above copyright notice(s) and this
  299. * permission notice appear in supporting documentation.
  300. *
  301. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  302. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  303. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
  304. * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
  305. * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
  306. * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  307. * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  308. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  309. * PERFORMANCE OF THIS SOFTWARE.
  310. */