Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
37f8dbe7
Commit
37f8dbe7
authored
1 year ago
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix chrono wrapper linux build
parent
c9174ca4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
130 additions
and
124 deletions
+130
-124
chrono_wrapper.h
jlib/util/chrono_wrapper.h
+130
-124
No files found.
jlib/util/chrono_wrapper.h
View file @
37f8dbe7
#
pragma
once
#include "../base/config.h"
#include <chrono>
#include <string>
#include <ctime>
#include <sstream>
#include <iomanip>
namespace
jlib
{
#ifdef __GNUG__
inline
void
localtime_s
(
struct
tm
*
tmtm
,
const
time_t
*
t
)
{
memcpy
(
tmtm
,
localtime
(
t
),
sizeof
(
tm
));
}
#else
#endif
// section:
// 0 for YYYY-mm-dd HH:MM:SS
// 1 for YYYY-mm-dd
// 2 for HH:MM:SS
inline
std
::
wstring
time_tToWstring
(
time_t
t
,
int
section
=
0
)
{
wchar_t
wtime
[
32
]
=
{
0
};
struct
tm
tmtm
;
localtime_s
(
&
tmtm
,
&
t
);
if
(
t
==
-
1
)
{
t
=
time
(
nullptr
);
localtime_s
(
&
tmtm
,
&
t
);
}
if
(
section
==
0
)
{
wcsftime
(
wtime
,
32
,
L"%Y-%m-%d %H:%M:%S"
,
&
tmtm
);
}
else
if
(
section
==
1
)
{
wcsftime
(
wtime
,
32
,
L"%Y-%m-%d"
,
&
tmtm
);
}
else
{
wcsftime
(
wtime
,
32
,
L"%H:%M:%S"
,
&
tmtm
);
}
return
std
::
wstring
(
wtime
);
}
// section:
// 0 for YYYY-mm-dd HH:MM:SS
// 1 for YYYY-mm-dd
// 2 for HH:MM:SS
inline
std
::
string
time_tToString
(
time_t
t
,
int
section
=
0
)
{
char
stime
[
32
]
=
{
0
};
struct
tm
tmtm
;
localtime_s
(
&
tmtm
,
&
t
);
if
(
t
==
-
1
)
{
t
=
time
(
nullptr
);
localtime_s
(
&
tmtm
,
&
t
);
}
if
(
section
==
0
)
{
strftime
(
stime
,
32
,
"%Y-%m-%d %H:%M:%S"
,
&
tmtm
);
}
else
if
(
section
==
1
)
{
strftime
(
stime
,
32
,
"%Y-%m-%d"
,
&
tmtm
);
}
else
{
strftime
(
stime
,
32
,
"%H:%M:%S"
,
&
tmtm
);
}
return
std
::
string
(
stime
);
}
inline
std
::
string
timePointToString
(
const
std
::
chrono
::
system_clock
::
time_point
&
tp
,
bool
with_milliseconds
=
false
)
{
std
::
stringstream
ss
;
auto
t
=
std
::
chrono
::
system_clock
::
to_time_t
(
tp
);
std
::
tm
*
tm
=
std
::
localtime
(
&
t
);
ss
<<
std
::
put_time
(
tm
,
"%Y-%m-%d %H:%M:%S"
);
if
(
with_milliseconds
)
{
auto
ms
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
tp
.
time_since_epoch
());
auto
millis
=
ms
.
count
()
%
1000
;
ss
<<
'.'
<<
std
::
setw
(
3
)
<<
std
::
setfill
(
'0'
)
<<
millis
;
}
return
ss
.
str
();
}
inline
std
::
wstring
timePointToWString
(
const
std
::
chrono
::
system_clock
::
time_point
&
tp
,
bool
with_milliseconds
=
false
)
{
std
::
wstringstream
ss
;
auto
t
=
std
::
chrono
::
system_clock
::
to_time_t
(
tp
);
std
::
tm
*
tm
=
std
::
localtime
(
&
t
);
ss
<<
std
::
put_time
(
tm
,
L"%Y-%m-%d %H:%M:%S"
);
if
(
with_milliseconds
)
{
auto
ms
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
tp
.
time_since_epoch
());
auto
millis
=
ms
.
count
()
%
1000
;
ss
<<
L'.'
<<
std
::
setw
(
3
)
<<
std
::
setfill
(
L'0'
)
<<
millis
;
}
return
ss
.
str
();
}
inline
std
::
chrono
::
system_clock
::
time_point
timePointFromString
(
const
std
::
string
&
s
)
{
std
::
tm
tm
=
{
0
};
std
::
istringstream
ss
(
s
);
ss
>>
std
::
get_time
(
&
tm
,
"%Y-%m-%d %H:%M:%S"
);
return
std
::
chrono
::
system_clock
::
from_time_t
(
std
::
mktime
(
&
tm
));
}
inline
std
::
chrono
::
system_clock
::
time_point
timePointFromWString
(
const
std
::
wstring
&
s
)
{
std
::
tm
tm
=
{
0
};
std
::
wistringstream
ss
(
s
);
ss
>>
std
::
get_time
(
&
tm
,
L"%Y-%m-%d %H:%M:%S"
);
return
std
::
chrono
::
system_clock
::
from_time_t
(
std
::
mktime
(
&
tm
));
}
inline
std
::
string
nowToString
(
bool
with_milliseconds
=
false
)
{
return
timePointToString
(
std
::
chrono
::
system_clock
::
now
(),
with_milliseconds
);
}
inline
std
::
wstring
nowToWString
(
bool
with_milliseconds
=
false
)
{
return
timePointToWString
(
std
::
chrono
::
system_clock
::
now
(),
with_milliseconds
);
}
};
#
pragma
once
#include "../base/config.h"
#ifdef __GNUG__
#include <string.h> // memcpy
#endif
#include <chrono>
#include <string>
#include <ctime>
#include <sstream>
#include <iomanip>
namespace
jlib
{
#ifdef __GNUG__
#include <string.h> // memcpy
inline
void
localtime_s
(
struct
tm
*
tmtm
,
const
time_t
*
t
)
{
memcpy
(
tmtm
,
localtime
(
t
),
sizeof
(
tm
));
}
#else
#endif
// section:
// 0 for YYYY-mm-dd HH:MM:SS
// 1 for YYYY-mm-dd
// 2 for HH:MM:SS
inline
std
::
wstring
time_tToWstring
(
time_t
t
,
int
section
=
0
)
{
wchar_t
wtime
[
32
]
=
{
0
};
struct
tm
tmtm
;
localtime_s
(
&
tmtm
,
&
t
);
if
(
t
==
-
1
)
{
t
=
time
(
nullptr
);
localtime_s
(
&
tmtm
,
&
t
);
}
if
(
section
==
0
)
{
wcsftime
(
wtime
,
32
,
L"%Y-%m-%d %H:%M:%S"
,
&
tmtm
);
}
else
if
(
section
==
1
)
{
wcsftime
(
wtime
,
32
,
L"%Y-%m-%d"
,
&
tmtm
);
}
else
{
wcsftime
(
wtime
,
32
,
L"%H:%M:%S"
,
&
tmtm
);
}
return
std
::
wstring
(
wtime
);
}
// section:
// 0 for YYYY-mm-dd HH:MM:SS
// 1 for YYYY-mm-dd
// 2 for HH:MM:SS
inline
std
::
string
time_tToString
(
time_t
t
,
int
section
=
0
)
{
char
stime
[
32
]
=
{
0
};
struct
tm
tmtm
;
localtime_s
(
&
tmtm
,
&
t
);
if
(
t
==
-
1
)
{
t
=
time
(
nullptr
);
localtime_s
(
&
tmtm
,
&
t
);
}
if
(
section
==
0
)
{
strftime
(
stime
,
32
,
"%Y-%m-%d %H:%M:%S"
,
&
tmtm
);
}
else
if
(
section
==
1
)
{
strftime
(
stime
,
32
,
"%Y-%m-%d"
,
&
tmtm
);
}
else
{
strftime
(
stime
,
32
,
"%H:%M:%S"
,
&
tmtm
);
}
return
std
::
string
(
stime
);
}
inline
std
::
string
timePointToString
(
const
std
::
chrono
::
system_clock
::
time_point
&
tp
,
bool
with_milliseconds
=
false
)
{
std
::
stringstream
ss
;
auto
t
=
std
::
chrono
::
system_clock
::
to_time_t
(
tp
);
std
::
tm
*
tm
=
std
::
localtime
(
&
t
);
ss
<<
std
::
put_time
(
tm
,
"%Y-%m-%d %H:%M:%S"
);
if
(
with_milliseconds
)
{
auto
ms
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
tp
.
time_since_epoch
());
auto
millis
=
ms
.
count
()
%
1000
;
ss
<<
'.'
<<
std
::
setw
(
3
)
<<
std
::
setfill
(
'0'
)
<<
millis
;
}
return
ss
.
str
();
}
inline
std
::
wstring
timePointToWString
(
const
std
::
chrono
::
system_clock
::
time_point
&
tp
,
bool
with_milliseconds
=
false
)
{
std
::
wstringstream
ss
;
auto
t
=
std
::
chrono
::
system_clock
::
to_time_t
(
tp
);
std
::
tm
*
tm
=
std
::
localtime
(
&
t
);
ss
<<
std
::
put_time
(
tm
,
L"%Y-%m-%d %H:%M:%S"
);
if
(
with_milliseconds
)
{
auto
ms
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
tp
.
time_since_epoch
());
auto
millis
=
ms
.
count
()
%
1000
;
ss
<<
L'.'
<<
std
::
setw
(
3
)
<<
std
::
setfill
(
L'0'
)
<<
millis
;
}
return
ss
.
str
();
}
inline
std
::
chrono
::
system_clock
::
time_point
timePointFromString
(
const
std
::
string
&
s
)
{
std
::
tm
tm
=
{
0
};
std
::
istringstream
ss
(
s
);
ss
>>
std
::
get_time
(
&
tm
,
"%Y-%m-%d %H:%M:%S"
);
return
std
::
chrono
::
system_clock
::
from_time_t
(
std
::
mktime
(
&
tm
));
}
inline
std
::
chrono
::
system_clock
::
time_point
timePointFromWString
(
const
std
::
wstring
&
s
)
{
std
::
tm
tm
=
{
0
};
std
::
wistringstream
ss
(
s
);
ss
>>
std
::
get_time
(
&
tm
,
L"%Y-%m-%d %H:%M:%S"
);
return
std
::
chrono
::
system_clock
::
from_time_t
(
std
::
mktime
(
&
tm
));
}
inline
std
::
string
nowToString
(
bool
with_milliseconds
=
false
)
{
return
timePointToString
(
std
::
chrono
::
system_clock
::
now
(),
with_milliseconds
);
}
inline
std
::
wstring
nowToWString
(
bool
with_milliseconds
=
false
)
{
return
timePointToWString
(
std
::
chrono
::
system_clock
::
now
(),
with_milliseconds
);
}
};
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment