time() 과 localtime()

리눅스에서 년, 월, 일, 시, 분, 초 로 나타내는 함수로

time, localtime 있다.

1장. localtime(3)

차례
1.1절. 사용법
1.2절. 설명
1.3절. 반환값
1.4절. 예제

1.1절. 사용법

#include <time.h>

struct tm *localtime(const time_t *timep);

1.2절. 설명

timep 를 입력받아서 유저 시스템의 time zone 에 맞도록
시간 값을 돌려준다. 시간 값은 tm 구조체에 들어간다.
tm 구조체는 다음과 같은 멤버 변수들을 포함한다.

struct tm
{
int tm_sec; /* 초 */
int tm_min; /* 분 */
int tm_hour; /* 시간 */
int tm_mday; /* 일/월 */
int tm_mon; /* 월 */
int tm_year; /* 년 */
int tm_wday; /* 일/주 */
int tm_yday; /* 일/년 */
int tm_isdst; /* 섬마타임 */
};

월은 0부터 시작한다. 그러므로 프로그램상에서 제대로 표현하려면 +1을
해주어야 한다. 또한 년의 경우에는 +1900 해주어야 한다.

아규먼트로 들어가는 timeptr 은 1970년 1월 1일 00:00:00 부터의
계산하고픈 때까지의 시간의 차이를 초로 환산한 값이다.


1.3절. 반환값

tm 구조체를 넘겨준다.


1.4절. 예제

#include <time.h>

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

void swaptime(time_t, char *);
int main()
{
char buffer[255];
time_t the_time;

// time 함수를 이용해서 현재 시간을 얻어온다.
time(&the_time);

memset(buffer, 0x00, 255);
swaptime(the_time, buffer);
printf("%sn", buffer);
}

void swaptime(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);

}

위 프로그램을 컴파일한다음 실행시키면 다음과 같은 결과를 보여줄것이다.

[root@localhost test]# ./localtime
2002-8-23 18:18:32


출처 : http://www.joinc.co.kr/modules/moniwiki/wiki.php/man/3/localtime

알수없는 에러….

 컴파일중 이상한 오류를 발견했다..

/usr/local/arm/arm-linux/sys-include/asm/fcntl.h:74: parse error before “pid_t”
/usr/local/arm/arm-linux/sys-include/asm/fcntl.h:80: parse error before “loff_t”
/usr/local/arm/arm-linux/sys-include/asm/fcntl.h:82: parse error before “l_pid”

 단순한 프로그램이었는데…이상했다.

 이렇게도 바꾸고 저렇게도 바꾸어서 원인을 찾았는데…

 답은 헤더 파일의 입력 순서에 있었다.

 원래는..

#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <asm/fcntl.h>
#include <stdio.h>

였는데…

이를 다음과 같이 바꾸니 문제가 해결되었다..

#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <asm/fcntl.h>
#include <stdio.h>

 개운하지가 않다.

 문제는 해결했는데 어찌 해결했는지를 모르겠다….

 분명 헤더파일의 입력때문에 뭔가가 엉킨것 같은데….꼭 집어내지를 못하겠다.

 

wrong # args: should be “for start test next command”

 Tcl/Tk 스크립트를 실행시키는 도중 에러메시지를 발견했다.

wrong # args: should be “for start test next command”
    while executing
“for {set k 0} {$i < $opt(nn)} {incr i}”
    (file “wireless2.tcl” line 163)

 무슨 내용일까…

 구글을 살펴 보았으나 이렇다 할 내용은 찾지 못했다…

 그러다가….정말 어이없는 실수가 눈에 들어왔다.

 이 구문이 말썽이었는데…

for {set i} {$i < $opt(nn)} {incr i}

 다음과 같이 바꿔 주었다.

for {set i} {$i < $opt(nn)} {incr i} {


 차이점을 알겠는가? 바로 마지막 중광호를 열고 안열고의 차이였다.

 C언어에서라면 금방 잡아 냈을 오류인데…이렇게 사용하는 언어가 다르니 에러메시지가 나타내는 뜻조차도 제대로 파악하지 못하고 있었다….

 반성하자…

error: multiple types in one declaration

 g++ 컴파일 중 다음과 같은 에러 메시지를 확인했다.

 error: multiple types in one declaration

 위와 같은 메시지가 발생하는 이유는 간단하다

 클래스나 구조체의 마지막에 세미콜론을 잊어 버려서 생기는 에러이다. 생각보다 간단하다.

 하지만 이 세미콜론을 잊어버린 곳 찾기가 가끔은 쉽지 않을 경우가 있다. (…나처럼)

^M 없애는 방법

 윈도우에서의 파일을 리눅스에서 열때…

 CR LF 부분에 ^M 표시가 나는 경우가 있다.
 

나는바보다^M 그렇니까 이것도 못하지^M 바부팅

  이럴 때는 다음과 같이 입력해 주면 된다.

%s/^M/^M/g (여기서 ^M은 Ctrl+V <ENTER>로 입력합니다.)

  혹은 이렇게도 가능하다.

:set fileformat=unix
:set fileformat=dos
:set fileformat=mac

  더 많은 해결책을 찾고 싶다면 다음 링크를 참고하면 된다.

 관련 링크 : http://kldp.org/node/36998