forked from YeeYoungHan/cppftpstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeString.cpp
More file actions
157 lines (132 loc) · 4.1 KB
/
TimeString.cpp
File metadata and controls
157 lines (132 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "SipPlatformDefine.h"
#include <stdio.h>
#include "TimeString.h"
#include "StringUtility.h"
#include "MemoryDebug.h"
/**
* @ingroup SipPlatform
* @brief 현재 시간을 년,월,일,시,분,초 구조체에 저장한다.
* @param iTime 현재 시간
* @param sttTm 년,월,일,시,분,초 구조체
*/
void LocalTime( time_t iTime, struct tm & sttTm )
{
#ifdef WIN32
localtime_s( &sttTm, &iTime );
#else
localtime_r( &iTime, &sttTm );
#endif
}
/**
* @ingroup SipPlatform
* @brief 시간 변수를 시간 문자열 변수에 저장한다.
* @param iTime 시간 변수
* @param pszTime 시간 문자열이 저장될 변수
* @param iTimeSize pszTime 변수의 크기
*/
void GetDateTimeString( time_t iTime, char * pszTime, int iTimeSize )
{
struct tm sttTm;
LocalTime( iTime, sttTm );
snprintf( pszTime, iTimeSize, "%04d%02d%02d%02d%02d%02d", sttTm.tm_year + 1900, sttTm.tm_mon + 1, sttTm.tm_mday
, sttTm.tm_hour, sttTm.tm_min, sttTm.tm_sec );
}
/**
* @ingroup SipPlatform
* @brief 년월일시분초 문자열을 저장한다.
* @param pszTime 년월일시분초 문자열 저장 변수
* @param iTimeSize pszTime 변수의 크기
*/
void GetDateTimeString( char * pszTime, int iTimeSize )
{
time_t iTime;
time( &iTime );
GetDateTimeString( iTime, pszTime, iTimeSize );
}
/**
* @ingroup SipPlatform
* @brief 년월일 문자열을 저장한다.
* @param iTime 시간
* @param pszDate 년월일 문자열을 저장 변수
* @param iDateSize pszDate 변수의 크기
*/
void GetDateString( time_t iTime, char * pszDate, int iDateSize )
{
struct tm sttTm;
LocalTime( iTime, sttTm );
snprintf( pszDate, iDateSize, "%04d%02d%02d", sttTm.tm_year + 1900, sttTm.tm_mon + 1, sttTm.tm_mday );
}
/**
* @ingroup SipPlatform
* @brief 년월일 문자열을 저장한다.
* @param pszDate 년월일 문자열을 저장 변수
* @param iDateSize pszDate 변수의 크기
*/
void GetDateString( char * pszDate, int iDateSize )
{
time_t iTime;
time( &iTime );
GetDateString( iTime, pszDate, iDateSize );
}
/**
* @ingroup SipPlatform
* @brief 시분초 문자열을 저장한다.
* @param iTime 시간
* @param pszTime 시분초 문자열 저장 변수
* @param iTimeSize pszTime 변수의 크기
*/
void GetTimeString( time_t iTime, char * pszTime, int iTimeSize )
{
struct tm sttTm;
LocalTime( iTime, sttTm );
snprintf( pszTime, iTimeSize, "%02d%02d%02d", sttTm.tm_hour, sttTm.tm_min, sttTm.tm_sec );
}
/**
* @ingroup SipPlatform
* @brief 시분초 문자열을 저장한다.
* @param pszTime 시분초 문자열 저장 변수
* @param iTimeSize pszTime 변수의 크기
*/
void GetTimeString( char * pszTime, int iTimeSize )
{
time_t iTime;
time( &iTime );
GetTimeString( iTime, pszTime, iTimeSize );
}
/**
* @ingroup SipPlatform
* @brief 년월일시분초 문자열을 time_t 로 변환한다.
* @param pszTime 년월일시분초 문자열
* @returns 성공하면 time_t 값이 리턴되고 실패하면 0 이 리턴된다.
*/
time_t ParseDateTimeString( const char * pszTime )
{
struct tm sttTm;
int iLen = (int)strlen( pszTime );
if( iLen < 14 ) return 0;
memset( &sttTm, 0, sizeof(sttTm) );
sttTm.tm_year = GetInt( pszTime, 4 ) - 1900;
sttTm.tm_mon = GetInt( pszTime+4, 2 ) - 1;
sttTm.tm_mday = GetInt( pszTime+6, 2 );
sttTm.tm_hour = GetInt( pszTime+8, 2 );
sttTm.tm_min = GetInt( pszTime+10, 2 );
sttTm.tm_sec = GetInt( pszTime+12, 2 );
return mktime( &sttTm );
}