lldate.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * @file lldate.cpp
  3. * @author Phoenix
  4. * @date 2006-02-05
  5. * @brief Implementation of the date class
  6. *
  7. * $LicenseInfo:firstyear=2006&license=viewergpl$
  8. *
  9. * Copyright (c) 2006-2009, Linden Research, Inc.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #include "linden_common.h"
  35. #include "lldate.h"
  36. #include "apr_time.h"
  37. #include <time.h>
  38. #include <locale>
  39. #include <iomanip>
  40. #include <sstream>
  41. #include "lltimer.h"
  42. #include "llstring.h"
  43. // Should be APR_USEC_PER_SEC, but that relies on INT64_C which is not defined
  44. // in glib under our build set up for some reason
  45. constexpr F64 LL_APR_USEC_PER_SEC = 1000000.0;
  46. LLDate::LLDate(const std::string& iso8601_date)
  47. {
  48. if (!fromString(iso8601_date))
  49. {
  50. llwarns << "date " << iso8601_date
  51. << " failed to parse, ZEROING IT OUT" << llendl;
  52. mSecondsSinceEpoch = 0.0;
  53. }
  54. }
  55. std::string LLDate::asString() const
  56. {
  57. std::ostringstream stream;
  58. toStream(stream);
  59. return stream.str();
  60. }
  61. // A more "human readable" timestamp format: same as ISO-8601, but with the "T"
  62. // between date and time replaced with a space and the "Z" replaced with " UTC"
  63. std::string LLDate::asTimeStamp(bool with_utc) const
  64. {
  65. std::string timestr;
  66. if (with_utc)
  67. {
  68. timeToFormattedString((time_t)mSecondsSinceEpoch,
  69. "%Y-%m-%d %H:%M:%S UTC", timestr);
  70. }
  71. else
  72. {
  73. timeToFormattedString((time_t)mSecondsSinceEpoch,
  74. "%Y-%m-%d %H:%M:%S", timestr);
  75. }
  76. return timestr;
  77. }
  78. std::string LLDate::toHTTPDateString(const char* fmt) const
  79. {
  80. llassert_always(fmt && *fmt);
  81. time_t loc_seconds = (time_t)mSecondsSinceEpoch;
  82. return toHTTPDateString(gmtime(&loc_seconds), fmt);
  83. }
  84. std::string LLDate::toHTTPDateString(tm* gmt, const char* fmt)
  85. {
  86. // Avoid calling setlocale() unnecessarily - it is expensive.
  87. static std::string prev_locale = "";
  88. std::string this_locale = LLStringUtil::getLocale();
  89. if (this_locale != prev_locale)
  90. {
  91. setlocale(LC_TIME, this_locale.c_str());
  92. prev_locale = this_locale;
  93. }
  94. // Use strftime() as it appears to be faster than std::time_put
  95. char buffer[128];
  96. strftime(buffer, 128, fmt, gmt);
  97. std::string res(buffer);
  98. #if LL_WINDOWS
  99. // Convert from locale-dependant charset to UTF-8 (EXT-8524).
  100. res = ll_convert_string_to_utf8_string(res);
  101. #endif
  102. return res;
  103. }
  104. void LLDate::toStream(std::ostream& s) const
  105. {
  106. apr_time_t time = (apr_time_t)(mSecondsSinceEpoch * LL_APR_USEC_PER_SEC);
  107. apr_time_exp_t exp_time;
  108. if (apr_time_exp_gmt(&exp_time, time) != APR_SUCCESS)
  109. {
  110. s << "1970-01-01T00:00:00Z";
  111. return;
  112. }
  113. s << std::dec << std::setfill('0');
  114. #if ( LL_WINDOWS || __GNUC__ > 2)
  115. s << std::right;
  116. #else
  117. s.setf(ios::right);
  118. #endif
  119. s << std::setw(4) << (exp_time.tm_year + 1900)
  120. << '-' << std::setw(2) << (exp_time.tm_mon + 1)
  121. << '-' << std::setw(2) << (exp_time.tm_mday)
  122. << 'T' << std::setw(2) << (exp_time.tm_hour)
  123. << ':' << std::setw(2) << (exp_time.tm_min)
  124. << ':' << std::setw(2) << (exp_time.tm_sec);
  125. if (exp_time.tm_usec > 0)
  126. {
  127. s << '.' << std::setw(2)
  128. << (int)(exp_time.tm_usec / (LL_APR_USEC_PER_SEC / 100));
  129. }
  130. s << 'Z' << std::setfill(' ');
  131. }
  132. bool LLDate::split(S32* year, S32* month, S32* day,
  133. S32* hour, S32* min, S32* sec) const
  134. {
  135. apr_time_t time = (apr_time_t)(mSecondsSinceEpoch * LL_APR_USEC_PER_SEC);
  136. apr_time_exp_t exp_time;
  137. if (apr_time_exp_gmt(&exp_time, time) != APR_SUCCESS)
  138. {
  139. return false;
  140. }
  141. if (year)
  142. {
  143. *year = exp_time.tm_year + 1900;
  144. }
  145. if (month)
  146. {
  147. *month = exp_time.tm_mon + 1;
  148. }
  149. if (day)
  150. {
  151. *day = exp_time.tm_mday;
  152. }
  153. if (hour)
  154. {
  155. *hour = exp_time.tm_hour;
  156. }
  157. if (min)
  158. {
  159. *min = exp_time.tm_min;
  160. }
  161. if (sec)
  162. {
  163. *sec = exp_time.tm_sec;
  164. }
  165. return true;
  166. }
  167. bool LLDate::fromString(const std::string& iso8601_date)
  168. {
  169. std::istringstream stream(iso8601_date);
  170. return fromStream(stream);
  171. }
  172. bool LLDate::fromStream(std::istream& s)
  173. {
  174. struct apr_time_exp_t exp_time;
  175. apr_int32_t tm_part;
  176. int c;
  177. s >> tm_part;
  178. exp_time.tm_year = tm_part - 1900;
  179. c = s.get(); // skip the hypen
  180. if (c != '-')
  181. {
  182. return false;
  183. }
  184. s >> tm_part;
  185. exp_time.tm_mon = tm_part - 1;
  186. c = s.get(); // skip the hypen
  187. if (c != '-')
  188. {
  189. return false;
  190. }
  191. s >> tm_part;
  192. exp_time.tm_mday = tm_part;
  193. c = s.get(); // skip the T
  194. if (c != 'T')
  195. {
  196. return false;
  197. }
  198. s >> tm_part;
  199. exp_time.tm_hour = tm_part;
  200. c = s.get(); // skip the :
  201. if (c != ':')
  202. {
  203. return false;
  204. }
  205. s >> tm_part;
  206. exp_time.tm_min = tm_part;
  207. c = s.get(); // skip the :
  208. if (c != ':')
  209. {
  210. return false;
  211. }
  212. s >> tm_part;
  213. exp_time.tm_sec = tm_part;
  214. // Zero out the unused fields
  215. exp_time.tm_usec = 0;
  216. exp_time.tm_wday = 0;
  217. exp_time.tm_yday = 0;
  218. exp_time.tm_isdst = 0;
  219. exp_time.tm_gmtoff = 0;
  220. // Generate a time_t from that
  221. apr_time_t time;
  222. if (apr_time_exp_gmt_get(&time, &exp_time) != APR_SUCCESS)
  223. {
  224. return false;
  225. }
  226. F64 seconds_since_epoch = time / LL_APR_USEC_PER_SEC;
  227. // Check for fractional
  228. c = s.peek();
  229. if (c == '.')
  230. {
  231. F64 fractional = 0.0;
  232. s >> fractional;
  233. seconds_since_epoch += fractional;
  234. }
  235. c = s.peek(); // Check for offset
  236. if (c == '+' || c == '-')
  237. {
  238. S32 offset_sign = (c == '+') ? 1 : -1;
  239. S32 offset_hours = 0;
  240. S32 offset_minutes = 0;
  241. S32 offset_in_seconds = 0;
  242. s >> offset_hours;
  243. c = s.get(); // Skip the colon a get the minutes if there are any
  244. if (c == ':')
  245. {
  246. s >> offset_minutes;
  247. }
  248. offset_in_seconds = (offset_hours * 60 +
  249. offset_sign * offset_minutes) * 60;
  250. seconds_since_epoch -= offset_in_seconds;
  251. }
  252. else if (c != 'Z')
  253. {
  254. // Skip the Z
  255. return false;
  256. }
  257. mSecondsSinceEpoch = seconds_since_epoch;
  258. return true;
  259. }
  260. bool LLDate::fromYMDHMS(S32 year, S32 month, S32 day,
  261. S32 hour, S32 min, S32 sec)
  262. {
  263. struct apr_time_exp_t exp_time;
  264. exp_time.tm_year = year - 1900;
  265. exp_time.tm_mon = month - 1;
  266. exp_time.tm_mday = day;
  267. exp_time.tm_hour = hour;
  268. exp_time.tm_min = min;
  269. exp_time.tm_sec = sec;
  270. // Zero out the unused fields
  271. exp_time.tm_usec = 0;
  272. exp_time.tm_wday = 0;
  273. exp_time.tm_yday = 0;
  274. exp_time.tm_isdst = 0;
  275. exp_time.tm_gmtoff = 0;
  276. // Generate a time_t from that
  277. apr_time_t time;
  278. if (apr_time_exp_gmt_get(&time, &exp_time) != APR_SUCCESS)
  279. {
  280. return false;
  281. }
  282. mSecondsSinceEpoch = time / LL_APR_USEC_PER_SEC;
  283. return true;
  284. }
  285. //static
  286. LLDate LLDate::now()
  287. {
  288. return LLDate(LLTimer::getEpochSeconds());
  289. }
  290. bool LLDate::operator<(const LLDate& rhs) const
  291. {
  292. return mSecondsSinceEpoch < rhs.mSecondsSinceEpoch;
  293. }
  294. std::ostream& operator<<(std::ostream& s, const LLDate& date)
  295. {
  296. date.toStream(s);
  297. return s;
  298. }
  299. std::istream& operator>>(std::istream& s, LLDate& date)
  300. {
  301. date.fromStream(s);
  302. return s;
  303. }