tm구조체에서요...

samdochen의 이미지

void write_date_time(void)
{
     unsigned long hour, min, sec, year, month, day;   
     struct tm *tm_ptr, time_struc;
    time_t the_time;
    
    hour = 15;
    min = 35;
    sec = 55;

    year = 2004;
    month = 01;
    day = 07;
    
    (void) time(&the_time);       
    tm_ptr = localtime(&the_time);
    
    time_struc.tm_hour = hour;
    time_struc.tm_min  = min;
    time_struc.tm_sec = sec;
    time_struc.tm_year = year;
    time_struc.tm_mon = month;
    time_struc.tm_mday = day;
            		
    tm_ptr = &time_struc;
    the_time = mktime(tm_ptr);
    
    strftime(buffer, 256, "%Y/%m/%d,%H:%M:%S,%a", tm_ptr);
    printf("time : %s\n",buffer);
}

이렇게 하고..
시간을 확인했을때,
제가 넣은 2004/01/07 15:35:55 이런값으로 바뀌지를 않네요.
이렇게 제가 구조체에 값을 집어넣고
저장할수 있는 방법은 없나요?
시간이 바뀌지가 않아요.
시간을 바꾸고 싶거든요
dhjung의 이미지

시간이 바뀌지 않는다는 의미가 머죠??
strftime()에서 말씀하신, 2004/01/07 15:35:55를 buffer에 넣고 싶다는 건가요??

그렇다면, man page를 보셔야 할듯.. :cry:

man localtime wrote:
The members of the tm structure are:

int tm_sec; /* seconds after the minute - [0, 61] */
/* for leap seconds */
int tm_min; /* minutes after the hour - [0, 59] */
int tm_hour; /* hour since midnight - [0, 23] */
int tm_mday; /* day of the month - [1, 31] */
int tm_mon; /* months since January - [0, 11] */
int tm_year; /* years since 1900 */

맨페이지를 보면, 연도는 1900이후의 값을, 월은 0부터 시작을 하고 있는것을 보실 수 있을껍니다.


--------------------------
Donghyun Jung

samdochen의 이미지

buffer에 그 값은 들어가거든요.

그런데 버퍼에 값넣기전에 보시면 제가 tm구조체에 값을 집어넣잖아요.

    time_struc.tm_hour = hour; 
    time_struc.tm_min  = min; 
    time_struc.tm_sec = sec; 
    time_struc.tm_year = year; 
    time_struc.tm_mon = month; 
    time_struc.tm_mday = day; 

이렇게 구조체에 값을 집어넣고...

컴파일한다음에 다시 시간을 읽어보면 제가 구조체에 넣은값이
나오질 않거든요.
구조체에 집어넣고 저장을 해야하는것인지..
strptime()을 쓰면 ssanf()역할을 한다는데.
이방법말고 또 없나요???

progcom의 이미지

악기와깡다구 wrote:
buffer에 그 값은 들어가거든요.

그런데 버퍼에 값넣기전에 보시면 제가 tm구조체에 값을 집어넣잖아요.

    time_struc.tm_hour = hour; 
    time_struc.tm_min  = min; 
    time_struc.tm_sec = sec; 
    time_struc.tm_year = year; 
    time_struc.tm_mon = month; 
    time_struc.tm_mday = day; 

이렇게 구조체에 값을 집어넣고...

컴파일한다음에 다시 시간을 읽어보면 제가 구조체에 넣은값이
나오질 않거든요.
구조체에 집어넣고 저장을 해야하는것인지..
strptime()을 쓰면 ssanf()역할을 한다는데.
이방법말고 또 없나요???

이런 방법으로 대입해도 값은 제대로 나옵니다. 어떻게 나오지 않는다는건지 궁금하네요.
(물론 위에서 dhjung님이 지적하신 대로 연도와 월은 정확히 쓰셔야합니다)
따로 문제가 될 수 있는 부분은, 현재 프로그램상에서 time_struc을 초기화하지 않았다는 점입니다. tm 구조체에 남은 부분의 쓰레기값이 영향을 미칠수도 있습니다.

운형의 이미지

신텍스 문제군요...

tm_ptr = localtime();

tm_struct. xxxx = yyyyy (구조체 내용 채운부분)

tm_ptr = & tm_struct;

이렇게 쓰셨는데... 결국 localtime에 의해 구해진 값은 버린결과.. -_-
localtime에 의해 나온 값을 나중에 쓰기위해서 보관해야 한다면
tm_ptr = localtime();
memcpy(&tm_struct, tm_ptr, sizeof(tm));

정도로 쓰셔야 할 것 같은데요.

Do you think that's the air you are breathing now?

bw001730의 이미지

저도 한동안 삽질했던...부분이었는데요
2004년을 저장할려면 2004-1900을 해야되드라구요
1900년 이후로 경과한 년도...머 이런 의미겠죠
아래에 간단한 함수를 만들어보았습니다.

일단 time_t 타입의 데이타만 만들어 내면
strftime()나 ctime(), asctime() 머 이런 함수로
원하는 포맷으로 시간을 만들어 내실수 있을것 같네요
순서는
1. 문자열로된 시간을 struct tm 타입의 시간으로 바꾼다.
2. struct tm을 mktime()으로 time_t타입으로 바꾼다.
3. strftime()를 호출한다.

중요한 부분은 1번과 2번 과정이겠구요
아래는 그 부분입니다.

time_t get_timet
          (
             char *yyyy, char *mm, char *dd,
             char *hour24, char *min, char *sec)
{
    struct   tm    t;
    memset(&t,0,sizeof(struct tm));
    t.tm_year    = strtol(yyyy,     NULL, 10) - 1900;   // 1900 이상
    t.tm_mon     = strtol(mm,       NULL, 10) - 1;  // 7월이면 6으로 저장
    t.tm_mday    = strtol(dd,       NULL, 10);      // 1~31
    t.tm_hour    = strtol(hour24,   NULL, 10)%24;   // 0~23
    t.tm_min     = strtol(min,      NULL, 10)%60;   // 0~59
    t.tm_sec     = strtol(sec,      NULL, 10)%61;   // 0~61
    t.tm_isdst   = -1;
    t.tm_wday    = -1;        /* day of the week */
    t.tm_yday    = -1;

    return mktime(&t);
}

잘못된 것이 있으면 답변주시길..

teatime의 이미지

운형 wrote:
신텍스 문제군요...
tm_ptr = localtime();

tm_struct. xxxx = yyyyy (구조체 내용 채운부분)

tm_ptr = & tm_struct;

이렇게 쓰셨는데... 결국 localtime에 의해 구해진 값은 버린결과.. -_-
localtime에 의해 나온 값을 나중에 쓰기위해서 보관해야 한다면
tm_ptr = localtime();
memcpy(&tm_struct, tm_ptr, sizeof(tm));

정도로 쓰셔야 할 것 같은데요.


printf로 각 변수들을 불러와서 확인해보면

 printf("date : %02d/%02d/%02d\n",tm_ptr->tm_year,
            tm_ptr->tm_mon,tm_ptr->tm_mday);
 printf("time : %02d:%02d:%02d\n",tm_ptr->tm_hour,
            tm_ptr->tm_min,tm_ptr->tm_sec);
    

넣은 값들이 제대로 나오지만...
date명령어로 사용해서 확인해보면...
시간은 바뀌어 있지 않던데요..
잘 몰라서 그러는데, date명령어로 확인해서 나오는 값을
바꿔 주려면 tm구조체를 건드리는게 아닌가요
서지훈의 이미지

System Calls                                             stime(2)

NAME
     stime - set system time and date

SYNOPSIS
     #include <unistd.h>

     int stime(const time_t *tp);

아마도 원하시는게 이거인것 같은데...-_-ㅋ

<어떠한 역경에도 굴하지 않는 '하양 지훈'>

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);

teatime의 이미지

감사합니다..

stime() ...왜 그냥 간과하고 지나갔는지..

제 불찰이었네요..^^

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.