@@ -77,4 +77,37 @@ void Date::toCustomedFormattedString(const std::string &fmtStr, char *str, size_
7777 gmtime_r (&seconds, &tm_time);
7878 strftime (str, len, fmtStr.c_str (), &tm_time);
7979}
80+ std::string Date::toFormattedStringLocal (bool showMicroseconds) const
81+ {
82+ // std::cout<<"toFormattedString"<<std::endl;
83+ char buf[128 ] = {0 };
84+ time_t seconds = static_cast <time_t >(microSecondsSinceEpoch_ / MICRO_SECONDS_PRE_SEC);
85+ struct tm tm_time;
86+ localtime_r (&seconds, &tm_time);
87+
88+ if (showMicroseconds)
89+ {
90+ int microseconds = static_cast <int >(microSecondsSinceEpoch_ % MICRO_SECONDS_PRE_SEC);
91+ snprintf (buf, sizeof (buf), " %4d%02d%02d %02d:%02d:%02d.%06d" ,
92+ tm_time.tm_year + 1900 , tm_time.tm_mon + 1 , tm_time.tm_mday ,
93+ tm_time.tm_hour , tm_time.tm_min , tm_time.tm_sec ,
94+ microseconds);
95+ }
96+ else
97+ {
98+ snprintf (buf, sizeof (buf), " %4d%02d%02d %02d:%02d:%02d" ,
99+ tm_time.tm_year + 1900 , tm_time.tm_mon + 1 , tm_time.tm_mday ,
100+ tm_time.tm_hour , tm_time.tm_min , tm_time.tm_sec );
101+ }
102+ return buf;
103+ }
104+ std::string Date::toCustomedFormattedStringLocal (const std::string &fmtStr) const
105+ {
106+ char buf[256 ] = {0 };
107+ time_t seconds = static_cast <time_t >(microSecondsSinceEpoch_ / MICRO_SECONDS_PRE_SEC);
108+ struct tm tm_time;
109+ localtime_r (&seconds, &tm_time);
110+ strftime (buf, sizeof (buf), fmtStr.c_str (), &tm_time);
111+ return std::string (buf);
112+ }
80113}; // namespace trantor
0 commit comments