概述
visual studio 2022测试通过
1 timeformat.h
#pragma once
#ifndef TIME_FORMAT_H
#define TIME_FORMAT_H
#include
<ctype.h>
#include
<string>
#include
<time.h>
#include <string>
#include <time.h>
#include <sstream>
using namespace std;
//source:https ://blog.csdn.net/wisage/article/details/6269997
//https://www.jianshu.com/p/da8fee6702c9
/*
* We do not implement alternate representations. However, we always
* check whether a given modifier is allowed for a certain conversion.
*/
#define
ALT_E
0x01
#define
ALT_O
0x02
//#define LEGAL_ALT(x)
{ if (alt_format & ~(x)) return (0); }
#define
LEGAL_ALT(x)
{ ; }
#define
TM_YEAR_BASE
(1900)
static
int conv_num(const char**, int*, int, int);
static
int strncasecmp(char* s1, char* s2, size_t n);
static
const char* day[7] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"
};
static
const char* abday[7] = {
"Sun","Mon","Tue","Wed","Thu","Fri","Sat"
};
static
const char* mon[12] = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
static
const char* abmon[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static
const char* am_pm[2] = {
"AM", "PM"
};
char* strptime(const char* buf, const char* fmt, struct tm* tm);
static
int conv_num(const char** buf, int* dest, int llim, int ulim);
int
strncasecmp(char* s1, char* s2, size_t n);
time_t str2date(const string& dateStr, const char& dateDiv = '-');
time_t str2time(const string& dateStr, const char& dateDiv = '-', const char& timeDiv = ':');
time_t str2time(const string& dateStr, const string& format);
time_t mkgmtime(tm* pTm);
std::string ShowHM(const time_t& t, const char& timeDiv = ':');
std::string ShowHMS(const time_t& t, const char& timeDiv = ':');
std::string ShowYMD(const time_t& t, const char& dateDiv = '-');
std::string ShowDateTime(const time_t& t, const char& dateDiv = '-', const char& timeDiv = ':');
std::string ShowDateTime(const tm& t, const char& dateDiv = '-', const char& timeDiv = ':');
std::string ShowDateTime(const time_t* t, const string& format);
std::string ShowDateTime(const tm* t, const string& format);
#endif // !TIME_FORMAT_H
2 timeformat.cpp
#include "timeformat.h"
char*
strptime(const char* buf, const char* fmt, struct tm* tm)
{
char c;
const char* bp;
size_t len = 0;
int alt_format, i, split_year = 0;
bp = buf;
while ((c = *fmt) != '