example_tc_common.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_common.h"
  17. #include <iterator>
  18. #include <sys/time.h>
  19. using namespace tars;
  20. string tm2GMTstr(const time_t &t)
  21. {
  22. struct tm tt;
  23. gmtime_r(&t, &tt);
  24. return TC_Common::tm2str(tt, "%a, %d %b %Y %H:%M:%S GMT");
  25. }
  26. extern char *tzname[2];
  27. extern long timezone;
  28. struct CmpCase
  29. {
  30. bool operator()(const string &s1, const string &s2)
  31. {
  32. return TC_Common::upper(s1) < TC_Common::upper(s2);
  33. }
  34. };
  35. int main(int argc, char *argv[])
  36. {
  37. try
  38. {
  39. map<string, string, CmpCase> m;
  40. m["abc"] = "def";
  41. m["Abc"] = "def";
  42. m["AbC"] = "def";
  43. m["AbCd"] = "def";
  44. m["AbCD"] = "def";
  45. cout << TC_Common::tostr(m) << endl;
  46. cout << m["ABC"] << endl;
  47. cout << m["ABCD"] << endl;
  48. /*
  49. {
  50. string n = "abc";
  51. vector<char> s;
  52. s.assign(n.c_str(), n.c_str() + n.length());
  53. cout << s.size() << ":" << s.capacity() << endl;
  54. s.insert(s.end(), n.c_str(), n.c_str() + n.length());
  55. cout << s.size() << ":" << s.capacity() << endl;
  56. s.insert(s.end(), n.c_str(), n.c_str() + n.length());
  57. cout << s.size() << ":" << s.capacity() << endl;
  58. s.clear();
  59. cout << s.size() << ":" << s.capacity() << endl;
  60. s.reserve(100);
  61. memcpy((&s[0] + s.size()), "dfetdg", 6);
  62. cout << TC_Common::tostr(s) << endl;
  63. s.resize(s.size() + 10);
  64. cout << s.size() << ":" << s.capacity() << endl;
  65. cout << TC_Common::tostr(s) << endl;
  66. }
  67. return 0;
  68. {
  69. string s;
  70. s = "abc";
  71. cout << s.length() << ":" << s.capacity() << endl;
  72. s += "abcd";
  73. cout << s.length() << ":" << s.capacity() << endl;
  74. s += "abcd";
  75. cout << s.length() << ":" << s.capacity() << endl;
  76. // s.clear();
  77. // s = s.substr(5);
  78. s.erase(s.begin(), s.begin() + min((size_t)100, s.size()));
  79. cout << s.size() << ":" << s.capacity() << endl;
  80. s.reserve(100);
  81. cout << s.length() << ":" << s.capacity() << endl;
  82. }
  83. return 0;
  84. time_t tt = time(NULL);
  85. string ss = tm2GMTstr(tt);
  86. cout << ss << endl;
  87. cout << tt << endl;
  88. // Sat, 02 Feb 2010 08:57:34 GMT
  89. // Fri, 30 Oct 1998 13:19:41 GMT
  90. // tzset();
  91. struct tm stTm;
  92. int ret = TC_Common::strgmt2tm(ss, stTm);
  93. cout << stTm.tm_sec << endl;
  94. cout << stTm.tm_min << endl;
  95. cout << stTm.tm_hour << endl;
  96. cout << stTm.tm_mday << endl;
  97. cout << stTm.tm_mon << endl;
  98. cout << stTm.tm_year << endl;
  99. cout << timezone << endl;
  100. cout << mktime(&stTm) - timezone << endl;
  101. cout << "***" << endl;
  102. cout << stTm.tm_zone << endl;
  103. cout << "***" << endl;
  104. cout << tzname[0] << ":" << tzname[1] << endl;
  105. cout << endl;
  106. cout << mktime(&stTm) - tt << ":" << stTm.tm_year << endl;
  107. cout << ret << endl;
  108. return 0;
  109. vector<string> v = TC_Common::sepstr<string>("|a|b||c|", "|", true);
  110. cout << TC_Common::tostr(v.begin(), v.end(), "|") << endl;
  111. vector<string> v2 = TC_Common::sepstr<string>("|a|b||c|", "|", false);
  112. cout << TC_Common::tostr(v2.begin(), v2.end(), "|") << endl;
  113. vector<int> v1 = TC_Common::sepstr<int>("|3|4||2|", "|", true);
  114. cout << TC_Common::tostr(v1) << endl;
  115. map<string, string> m;
  116. m["abc"] = "";
  117. m["abc1"] = "ef1";
  118. m["abc2"] = "ef2";
  119. cout << TC_Common::tostr(m) << endl;
  120. v = TC_Common::sepstr<string>("|a|b||c|", "|", true);
  121. cout << TC_Common::tostr(v) << endl;
  122. return 0;
  123. struct timeval t;
  124. gettimeofday(&t, NULL);
  125. cout << TC_Common::tm2str(t.tv_sec, "%Y-%m-%d %H:%M:%S.") << outfill(TC_Common::tostr(t.tv_usec/1000), '0', 3) << "|";
  126. string s = "asdfoasdfijfa";
  127. cout << s << endl;
  128. string s1 = TC_Common::bin2str(s);
  129. cout << s1 << endl;
  130. string s2 = TC_Common::bin2str(s, " ", 4);
  131. cout << s2 << endl;
  132. string s3 = TC_Common::str2bin(s1);
  133. cout << s3 << endl;
  134. string s4 = TC_Common::str2bin(s2, " ", 4);
  135. cout << s4 << endl;
  136. cout << "matchPeriod :" << TC_Common::matchPeriod("127.0.0.1", "127.*.*.*") << endl;
  137. cout << "[" << TC_Common::trim("abc\n\t") << "]" << endl;
  138. cout << "[" << TC_Common::trimleft("abc\n\t") << "]" << endl;
  139. cout << "[" << TC_Common::trim("\r\nabc") << "]" << endl;
  140. cout << "[" << TC_Common::trim("\r\nabc\r\n") << "]" << endl;
  141. cout << "[" << TC_Common::trim(" abc ", " \r\t") << "]" << endl;
  142. cout << "[" << TC_Common::trim("abcdefab", "ab", false) << "]" << endl;
  143. cout << TC_Common::toSize("3K", 3000) << endl;
  144. cout << TC_Common::toSize("23M", 3000) << endl;
  145. cout << TC_Common::toSize("15G", 3000) << endl;
  146. cout << TC_Common::toSize("15", 3000) << endl;
  147. cout << TC_Common::toSize("M", 3000) << endl;
  148. cout << TC_Common::toSize("34.32M", 3000) << endl;
  149. cout << TC_Common::toSize("3.2G", 3000) << endl;
  150. cout << TC_Common::toSize("3r,.2G", 3000) << endl;
  151. */
  152. }
  153. catch(exception &ex)
  154. {
  155. cout << ex.what() << endl;
  156. }
  157. return 0;
  158. }