1

変数に別々に 2 つの日付と時刻の文字列があります。これら 2 つの日付と時刻の値の差をミリ秒単位で計算する必要があります。C でそれを取得する方法。このソリューションは、プラットフォーム間 (少なくとも Windows と UNIX) で機能するはずです。

char date1[] = {"26/11/2015"};
char time1[] = {"20:22:19"};
char date2[] = {"26/11/2015"};
char time2[] = {"20:23:19"};

まず、これを時間構造に保存してから、2 つの時間構造を比較して違いを得る必要があります。これを行うために C ライブラリで利用できる時間構造は何ですか。

4

2 に答える 2

2

使用mktime()してdifftime()

このmktime関数は、 type の値としてエンコードされた指定されたカレンダー時間を返しますtime_t。カレンダー時間を表すことができない場合、関数は値を返します(time_t)(-1)。C11dr §7.27.2.3 4

このdifftime関数は、double §7.27.2.2 として秒単位で表された差を返します 2

#include <time.h>
#include <stdlib.h>
#include <string.h>

time_t parse_dt(const char *mdy, const char *hms) {
  struct tm tm;
  memset(&tm, 0, sizeof tm);
  if (3 != sscanf(mdy, "%d/%d/%d", &tm.tm_mon, &tm.tm_mday, &tm.tm_year)) return -1;
  tm.tm_year -= 1900;
  tm.tm_mday++;
  if (3 != sscanf(hms, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec)) return -1;
  tm.tm_isdst = -1;  // Assume local time
  return mktime(&tm);
}

int main() {
  // application
  char date1[] = { "26/11/2015" };
  char time1[] = { "20:22:19" };
  char date2[] = { "26/11/2015" };
  char time2[] = { "20:23:19" };
  time_t t1 = parse_dt(date1, time1);
  time_t t2 = parse_dt(date2, time2);
  if (t1 == -1 || t2 == -1) return 1;
  printf("Time difference %.3f\n", difftime(t2, t1) * 1000.0);
  return 0;
}

出力

Time difference 60000.000
于 2015-11-26T16:46:34.893 に答える
1

mktime() のマニュアルページから

これは mktime() のプロトタイプです

time_t mktime(struct tm *tm);

これは関数 mktime() の説明です

  The mktime() function takes an argument representing
   broken-down time which is a representation separated into year,  month,
   day, and so on.

   Broken-down  time  is  stored  in  the structure tm which is defined in
   <time.h> as follows:

       struct tm {
           int tm_sec;         /* seconds */
           int tm_min;         /* minutes */
           int tm_hour;        /* hours */
           int tm_mday;        /* day of the month */
           int tm_mon;         /* month */
           int tm_year;        /* year */
           int tm_wday;        /* day of the week */
           int tm_yday;        /* day in the year */
           int tm_isdst;       /* daylight saving time */
       };

   The members of the tm structure are:

   tm_sec    The number of seconds after the minute, normally in the range
             0 to 59, but can be up to 60 to allow for leap seconds.

   tm_min    The number of minutes after the hour, in the range 0 to 59.

   tm_hour   The number of hours past midnight, in the range 0 to 23.

   tm_mday   The day of the month, in the range 1 to 31.

   tm_mon    The number of months since January, in the range 0 to 11.

   tm_year   The number of years since 1900.

   tm_wday   The number of days since Sunday, in the range 0 to 6.

   tm_yday   The number of days since January 1, in the range 0 to 365.

   tm_isdst  A  flag  that  indicates  whether  daylight saving time is in
             effect at the time described.  The value is positive if  day‐
             light  saving time is in effect, zero if it is not, and nega‐
             tive if the information is not available.


   The  mktime() function converts a broken-down time structure, expressed
   as local time, to calendar time representation.  The  function  ignores
   the  values  supplied  by the caller in the tm_wday and tm_yday fields.
   The value specified in the tm_isdst field informs mktime()  whether  or
   not  daylight  saving  time (DST) is in effect for the time supplied in
   the tm structure: a positive value means DST is in effect;  zero  means
   that  DST  is  not  in effect; and a negative value means that mktime()
   should (use timezone information and system databases  to)  attempt  to
   determine whether DST is in effect at the specified time.

   The  mktime()  function modifies the fields of the tm structure as fol‐
   lows: tm_wday and tm_yday are set to values determined  from  the  con‐
   tents of the other fields; if structure members are outside their valid
   interval, they will be normalized (so that, for example, 40 October  is
   changed  into  9  November); tm_isdst is set (regardless of its initial
   value) to a positive value or to 0, respectively, to  indicate  whether
   DST  is  or  is  not in effect at the specified time.  Calling mktime()
   also sets the external variable tzname with information about the  cur‐
   rent timezone.

   If  the  specified  broken-down  time cannot be represented as calendar
   time (seconds since the Epoch), mktime() returns (time_t) -1  and  does
   not alter the members of the broken-down time structure.

==========================================

これはmanページからのものですdifftime()

これはプロトタイプです:

double difftime(time_t time1, time_t time0);

これは説明です:

   The  difftime()  function returns the number of seconds elapsed between
   time time1 and time time0, represented as a double.  Each of the  times
   is  specified  in calendar time, which means its value is a measurement
   (in seconds) relative to the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

数秒で結果を取得します。結果をミリ秒で取得するには、1000.0 を掛けます。

于 2015-11-26T21:57:13.313 に答える