|
| 1 | +#include "unit_test_date_time_format.h" |
| 2 | +#include <vector> |
| 3 | +#include <wsjcpp_core.h> |
| 4 | +#include <regex> |
| 5 | + |
| 6 | +REGISTRY_WSJCPP_UNIT_TEST(UnitTestDateTimeFormat) |
| 7 | + |
| 8 | +UnitTestDateTimeFormat::UnitTestDateTimeFormat() |
| 9 | + : WsjcppUnitTestBase("UnitTestDateTimeFormat") { |
| 10 | +} |
| 11 | + |
| 12 | +// --------------------------------------------------------------------- |
| 13 | + |
| 14 | +bool UnitTestDateTimeFormat::doBeforeTest() { |
| 15 | + // nothing |
| 16 | + return true; |
| 17 | +} |
| 18 | + |
| 19 | +// --------------------------------------------------------------------- |
| 20 | + |
| 21 | +void UnitTestDateTimeFormat::executeTest() { |
| 22 | + |
| 23 | + // format like '2020-09-17 02:22:40.755' |
| 24 | + { |
| 25 | + std::string sNow = WsjcppCore::getCurrentTimeForLogFormat(); |
| 26 | + std::string sRegexp = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}$"; |
| 27 | + std::regex rx(sRegexp); |
| 28 | + bool bResult = std::regex_match(sNow, rx); |
| 29 | + compare("now for log format: " + sNow + ", regexp: " + sRegexp, bResult, true); |
| 30 | + } |
| 31 | + |
| 32 | + // like this '20200917_023347' |
| 33 | + { |
| 34 | + std::string sNow = WsjcppCore::getCurrentTimeForFilename(); |
| 35 | + // std::cout << sNow << std::endl; |
| 36 | + std::string sRegexp = "^\\d{8}_\\d{6}$"; |
| 37 | + std::regex rx(sRegexp); |
| 38 | + bool bResult = std::regex_match(sNow, rx); |
| 39 | + compare("now for filename: " + sNow + ", regexp: " + sRegexp, bResult, true); |
| 40 | + } |
| 41 | + |
| 42 | + { |
| 43 | + long nMs = WsjcppCore::getCurrentTimeInMilliseconds(); |
| 44 | + long nSec = WsjcppCore::getCurrentTimeInSeconds(); |
| 45 | + // std::cout << nMs << std::endl; |
| 46 | + // std::cout << nSec << std::endl; |
| 47 | + long nMsToSec = nMs / 1000; |
| 48 | + compare("now sec and ms", nMsToSec, nSec); |
| 49 | + } |
| 50 | + |
| 51 | + // like this '20200917_023347' |
| 52 | + { |
| 53 | + long nNow = WsjcppCore::getCurrentTimeInSeconds(); |
| 54 | + std::string sNow = WsjcppCore::formatTimeForFilename(nNow); |
| 55 | + std::string sRegexp = "^\\d{8}_\\d{6}$"; |
| 56 | + std::regex rx(sRegexp); |
| 57 | + bool bResult = std::regex_match(sNow, rx); |
| 58 | + compare("now for filename (2): " + sNow + ", regexp: " + sRegexp, bResult, true); |
| 59 | + } |
| 60 | + |
| 61 | + // like 'Thu, 17 Sep 2020 02:39:52 GMT' |
| 62 | + { |
| 63 | + std::map<long, std::string> mapTimes; |
| 64 | + |
| 65 | + // monthes |
| 66 | + // 2592000 - 30 days |
| 67 | + mapTimes[1578105758] = "Sat, 04 Jan 2020 02:42:38 GMT"; |
| 68 | + mapTimes[1580697758] = "Mon, 03 Feb 2020 02:42:38 GMT"; |
| 69 | + mapTimes[1583289758] = "Wed, 04 Mar 2020 02:42:38 GMT"; |
| 70 | + mapTimes[1585881758] = "Fri, 03 Apr 2020 02:42:38 GMT"; |
| 71 | + mapTimes[1588473758] = "Sun, 03 May 2020 02:42:38 GMT"; |
| 72 | + mapTimes[1591065758] = "Tue, 02 Jun 2020 02:42:38 GMT"; |
| 73 | + mapTimes[1593657758] = "Thu, 02 Jul 2020 02:42:38 GMT"; |
| 74 | + mapTimes[1596249758] = "Sat, 01 Aug 2020 02:42:38 GMT"; |
| 75 | + mapTimes[1598841758] = "Mon, 31 Aug 2020 02:42:38 GMT"; |
| 76 | + mapTimes[1601433758] = "Wed, 30 Sep 2020 02:42:38 GMT"; |
| 77 | + mapTimes[1604025758] = "Fri, 30 Oct 2020 02:42:38 GMT"; |
| 78 | + mapTimes[1606617758] = "Sun, 29 Nov 2020 02:42:38 GMT"; |
| 79 | + mapTimes[1609209758] = "Tue, 29 Dec 2020 02:42:38 GMT"; |
| 80 | + |
| 81 | + // week days |
| 82 | + mapTimes[1599964958] = "Sun, 13 Sep 2020 02:42:38 GMT"; |
| 83 | + mapTimes[1600051358] = "Mon, 14 Sep 2020 02:42:38 GMT"; |
| 84 | + mapTimes[1600137758] = "Tue, 15 Sep 2020 02:42:38 GMT"; |
| 85 | + mapTimes[1600224158] = "Wed, 16 Sep 2020 02:42:38 GMT"; |
| 86 | + mapTimes[1600310558] = "Thu, 17 Sep 2020 02:42:38 GMT"; |
| 87 | + mapTimes[1600396958] = "Fri, 18 Sep 2020 02:42:38 GMT"; |
| 88 | + mapTimes[1600483358] = "Sat, 19 Sep 2020 02:42:38 GMT"; |
| 89 | + |
| 90 | + std::map<long, std::string>::iterator it; |
| 91 | + for (it = mapTimes.begin(); it != mapTimes.end(); ++it) { |
| 92 | + long nTime = it->first; |
| 93 | + std::string sTime = WsjcppCore::formatTimeForWeb(nTime); |
| 94 | + // std::cout << sTime << std::endl; |
| 95 | + // std::string sRegexp = "^(Sun|Mon|Tue|Wed|Thu|Fri|Sat){1}, \\d{1,2} .* \\d{1,4} GMT$"; |
| 96 | + std::string sRegexp = "^(Sun|Mon|Tue|Wed|Thu|Fri|Sat){1}, \\d{2} " |
| 97 | + "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) " |
| 98 | + "\\d{1,4} \\d{2}:\\d{2}:\\d{2} GMT$"; |
| 99 | + std::regex rx(sRegexp); |
| 100 | + bool bResult = std::regex_match(sTime, rx); |
| 101 | + compare("now for web: " + sTime + ", regexp: " + sRegexp, bResult, true); |
| 102 | + compare("now for web str:", sTime, it->second); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // like "2020-09-17 02:39:52" |
| 107 | + { |
| 108 | + long nNow = WsjcppCore::getCurrentTimeInSeconds(); |
| 109 | + std::string sNow = WsjcppCore::formatTimeUTC(nNow); |
| 110 | + std::string sRegexp = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$"; |
| 111 | + std::regex rx(sRegexp); |
| 112 | + bool bResult = std::regex_match(sNow, rx); |
| 113 | + compare("now for time utc: " + sNow + ", regexp: " + sRegexp, bResult, true); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// --------------------------------------------------------------------- |
| 118 | + |
| 119 | +bool UnitTestDateTimeFormat::doAfterTest() { |
| 120 | + // nothing |
| 121 | + return true; |
| 122 | +} |
0 commit comments