2016-09-14 3 views
0

두 datetime (년, 월, 일,시, 분, 초)의 차이를 계산할 수있는 함수가 필요합니다. 차이를 동일한 형식으로 반환하십시오. 이 작은 유닛 인 것에C에서 datetime 차이 계산

는 (제 케이스의) datetime 값을 나타내는 일체형 (바람직 long long 또는 unsigned long long)로 datetime 변환 :

int main(){ 

    struct datetime dt_from; 
    init_datetime(&dt_from, 1995, 9, 15, 10, 40, 15); 

    struct datetime dt_to; 
    init_datetime(&dt_to, 2004, 6, 15, 10, 40, 20); 

    struct datetime dt_res; 

    datetime_diff(&dt_from, &dt_to, &dt_res); 

    return 0; 

} 

void datetime_diff(struct datetime *dt_from, struct datetime *dt_to 
, struct datetime *dt_res) { 

    //What can I do here to calculate the difference, and get it in the dt_res? 

} 
+5

하는 당신은 표준 ['difftime' (같은 것을 의미 HTTP : //en.cppreference.com/w/c/chrono/difftime) function? –

+0

struct datetime은 이식 가능하지 않습니다. "time.h"라이브러리를 사용해야하므로 struct tm – jurhas

+0

을 사용해야합니다. 1)'struct datetime'의 정의를 게시하십시오. 2)'init_datetime()()'의 포스트 정의 3) 코드가 어떻게 오버 플로우를 처리해야 하는지를 설명하는 것이 유용 할 것이다. (최대 시간 - 최소 시간) – chux

답변

0

여기 기본적인 아이디어이다. 그것을 성취하는 방법? 단일 값을 초 단위로 쉽게 변환하고 모든 것을 함께 추가하십시오. (seconds + minutes * 60 + hours * 3600 ...)

이 값을 두 값에 대해 수행 한 다음 정수 값을 뺍니다.

이제 단일 정수 값인 시차를 datetime으로 변환하십시오. 방법? 가장 큰 단위 (년)로 시작하여 1 년 (60 * 60 * 24 * 365) 내의 초의 양으로 그 차이를 나눕니다. 이제 2 년 사이에 얼마나 많은 시간이 있는지 알 수 있습니다. datetime 나머지를 가지고 매월 초 단위로하여 분할, 등등 ...


(분명 내가 예를 들어 일광 절약 시간처럼, 오히려 복잡한 모든 것을 무시) 그러나 내가보기 엔 것 의견에서 언급 한 바와 같이 time.h에서 struct tm을 사용하는 것이 좋습니다. 휴대용이며 difftime을 사용할 수 있습니다.

0

time.h을 사용하고 휴대해야하는이 예제를 시도해보십시오. 그것은 귀하의 질문에 날짜 사이의 일 차이를 계산합니다. 프로그램을 조금만 변경하여 원하는 방식으로 작동시킬 수 있습니다.

#include <stdio.h> 
#include <time.h> 
#include <math.h> 

int main() { 
    time_t start_daylight, start_standard, end_daylight, end_standard;; 
    struct tm start_date = {0}; 
    struct tm end_date = {0}; 
    double diff; 

    printf("Start date: "); 
    scanf("%d %d %d", &start_date.tm_mday, &start_date.tm_mon, &start_date.tm_year); 
    printf("End date: "); 
    scanf("%d %d %d", &end_date.tm_mday, &end_date.tm_mon, &end_date.tm_year); 

    /* first with standard time */ 
    start_date.tm_isdst = 0; 
    end_date.tm_isdst = 0; 
    start_standard = mktime(&start_date); 
    end_standard = mktime(&end_date); 
    diff = difftime(end_standard, start_standard); 

    printf("%.0f days difference\n", round(diff/(60.0 * 60 * 24))); 

    /* now with daylight time */ 
    start_date.tm_isdst = 1; 
    end_date.tm_isdst = 1; 
    start_daylight = mktime(&start_date); 
    end_daylight = mktime(&end_date); 
    diff = difftime(end_daylight, start_daylight); 

    printf("%.0f days difference\n", round(diff/(60.0 * 60 * 24))); 

    return 0; 
} 

테스트

Start date: 15 9 1995 
End date: 15 6 2004 
3195 days difference 

또는 대화 형이 아닌 코드 및 표준 또는 일광 절약 시간이 더 간단 :

#include <stdio.h> 
#include <time.h> 
#include <math.h> 
int main() 
{ 
    time_t start_daylight, start_standard, end_daylight, end_standard;; 
    struct tm start_date = {0}; 
    struct tm end_date = {0}; 
    double diff; 

    start_date.tm_year = 1995; 
    start_date.tm_mon = 9; 
    start_date.tm_mday = 15; 
    start_date.tm_hour = 10; 
    start_date.tm_min = 40; 
    start_date.tm_sec = 15; 

    end_date.tm_mday = 15; 
    end_date.tm_mon = 6; 
    end_date.tm_year = 2004; 
    end_date.tm_hour = 10; 
    end_date.tm_min = 40; 
    end_date.tm_sec = 20; 

    /* first with standard time */ 
    start_date.tm_isdst = 0; 
    end_date.tm_isdst = 0; 
    start_standard = mktime(&start_date); 
    end_standard = mktime(&end_date); 
    diff = difftime(end_standard, start_standard); 

    printf("%.0f days difference\n", round(diff/(60.0 * 60 * 24))); 

    /* now with daylight time */ 
    start_date.tm_isdst = 1; 
    end_date.tm_isdst = 1; 
    start_daylight = mktime(&start_date); 
    end_daylight = mktime(&end_date); 
    diff = difftime(end_daylight, start_daylight); 

    printf("%.0f days difference\n", round(diff/(60.0 * 60 * 24))); 

    return 0; 
} 
+1

유효하지 않은 사용자 입력에 대한 코드의 정의되지 않은 동작이 있습니다. 이는 일반적으로 사람들이 복사 할 수있는 예제 코드에서 나쁜 것입니다. – hyde

+0

@hyde 오류 검사를 추가해야합니까? –

+1

'mktime()'은'struct tm'의'tm_yday, tm_wday'을 제외한 모든 필드를 읽습니다. 이 코드는 6 개만 설정합니다 - 다른 설정은 없습니다. 모든 필드를 설정하고'tm_isdst'를 연구하기 위해'struct tm start_date = {0};'으로 초기화하는 것을 제안하십시오. – chux