달력 시간
#include <time.h>
time_t time(time_t *calptr);
현재 시간과 날짜를 반환
-1970년 1월 1일 00:00:00 부터 지금까지의 시간을 초단위로 환산
BSD 현재 시간
#include <sys/time.h>
#include <unistd.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);
time() 함수와 매우 비슷하지만 마이크로초단위의 시간 까지 반환
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
int main()
{
struct timeval mytime;
//현재 시간을 얻어온다.
gettimeofday(&mytime,NULL);
printf("%ld:%ld\n",mytime.tv_sec,mytime.tv_usec);
//시간을 1시간 뒤로 돌려서 설정한다.
mytime.tv_sec -=3600;
settimeofday(&mytime,NULL);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
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
|
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
void stprintf_time(time_t,char *);
int main()
{
int i;
time_t the_time;
char buffer[255];
time(&the_time);
printf("현재 시간은 %d초\n",the_time);
stprintf_time(the_time,buffer);
printf("%s\n",buffer);
return 0;
}
void stprintf_time(time_t org_time, char *time_str)
{
struct tm *tm_ptr;
tm_ptr = localtime(&org_time);
sprintf(time_str,"%d/%d/%d %d:%d:%d",tm_ptr->tm_year+1900,tm_ptr->tm_mon+1,tm_ptr->tm_mday,tm_ptr->tm_hour,tm_ptr->tm_min,tm_ptr->tm_sec);
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
지역시간대
#include <time.h>
void tzset(void);
지역 시간대를 TZ 환경 변수에 저장
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/timeb.h>
char *tzstr = "TZ=PST8PDT";
int main(void)
{
struct timeb t;
putenv(tzstr);
tzset();
ftime(&t);
printf("Seconds since 1/1/1970 GMT: %ld\n",t.time);
printf("Thousandths of a second: %d\n",t.millitm);
printf("Difference between local time and GMT: %d\n",t.timezone);
printf("Daylight savings in effect (1) not (0): %d\n",t.dstflag);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
달력 시간 변환 함수
#include <time.h>
struct tm *gmtime(const time_t *calptr);
struct tm *localtime(const time_t *calptr);
달력 시간을 사람이 인식할 수 있는 시간으로 변환 strcut tm 구조체 사용
localtime 은 지역 시간과 일광 절약 시간을 고려하여 변환
gmtime 은 UTC로 표시되는 시간으로 변환
time_t mktime(struct tm *tmptr);
double difftime(time_t t1, time_t t2);
mktime() 함수는 시간 구조체를 달력시간 표현으로 변환시키기 위해 사용
지역 시간으로 표시되는 분해된 시간을 time_t 값으로 변환
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
|
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t tm_st;
time_t tm_nd;
int tm_day, tm_hour, tm_min, tm_sec;
double d_diff;
struct tm user_stime;
user_stime.tm_year = 2007 - 1900; //년은 1900년부터 시작
user_stime.tm_mon =7-1; //월은 0부터 시작
user_stime.tm_mday=1;
user_stime.tm_hour=10;
user_stime.tm_min=12;
user_stime.tm_sec=55;
user_stime.tm_isdst=0; //썸머타임 사용 안함
tm_st=mktime(&user_stime);
time(&tm_nd);
d_diff=difftime(tm_nd, tm_st);
tm_day = d_diff/(60*60*24);
d_diff = d_diff - (tm_day*60*60*24);
tm_hour = d_diff/(60*60);
d_diff = d_diff - (tm_hour*60*60);
tm_min = d_diff/60;
d_diff = d_diff - (tm_min*60);
tm_sec = d_diff;
printf("%d일 %d시 %d분 %d\n",tm_day,tm_hour,tm_min,tm_sec);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
날짜 문자열 형식 지정
#include <time.h>
char *asctime(const struct tm *tmptr);
char *ctime(const time_t *calptr);
사람이 읽을 수 있는 26바이트 문자열 형태의 시간 정보로 변환
asctime은 분해된 시간 값을 ctime은 달력 시간 값을 인자로 갖는다.
size_t strftime(char *buf, size_t maxsize, const char *format, const struct tm *tmptr);
printf 함수와 비슷한 방식으로 시간 정보를 변환. 변환 되어 저장된 문자열의 크기를 리턴
인자
-buf 변환된 문자열을 저장할 메모리
-maxsize buf의 크기
-format 변환 방식을 지정하는 무자열
-tmptr 분해된 시간 값
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
|
#include <locale.h>
#include <stdio.h>
#include <time.h>
char *formats[] = {
"%A, %B %e, %Y, %H:%m:%s",
"%l:%M %p, %d-%b-%y",
"%x %X",
"%C",
"%c",
NULL
};
char *locales[] = {
"C","de","fr","it","sv",NULL
};
char *localeNames[] = {
"UNIX","German","French","Italian","Swedish",NULL
};
int main(int argc,char **argv)
{
int i,j;
time_t clock;
struct tm *tm;
char buf[BUFSIZ];
time(&clock);
tm=gmtime(&clock);
for(i=0;locales[i]!=NULL;i++){
printf("%s:\n",localeNames[i]);
setlocale(LC_TIME,locales[i]);
for(j=0;formats[j]!=NULL;j++){
strftime(buf,sizeof(buf),formats[j],tm);
printf("\t%-25s %s\n",formats[j],buf);
}
printf("\n");
}
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
현재 시간 구하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <stdio.h>
#include <string.h>
#include <time.h>
int main(void)
{
time_t now_time;
struct tm *now_date;
char buf[100];
time(&now_time); //현재 시각을 구한다
now_date=localtime(&now_time);//초 단위 값을 지역 시각(DateTime)을 구한다.
strcpy(buf,asctime(now_date)); //버퍼에 현재 시각을 출력
puts(buf); //표준 출력 스트림에 출력
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
'딥러닝 기반 영상인식 개발 전문가 과정 > 리눅스' 카테고리의 다른 글
5월30일 시스템 환경설정과 자원제한 (0) | 2019.05.30 |
---|---|
5월 30일 사용자와 그룹 (0) | 2019.05.30 |
5월30일 파일과디렉토리, 특수 목적 파일 연산 (0) | 2019.05.30 |
5월29일 실습 (0) | 2019.05.29 |
5월29일 리눅스 2 형기반입출력, 줄단위, 버퍼기반, 읽기/쓰기 오프셋 재배치, 디렉토리 생성과 삭제 (0) | 2019.05.29 |