delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/05/21/05:02:12

From: "Alex Vinokur" <alexvn AT bigfoot DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: mktime() for 1969 & 2106
Date: Tue, 21 May 2002 12:00:19 +0200
Organization: Scopus
Lines: 356
Message-ID: <acd27n$oa801$1@ID-79865.news.dfncis.de>
NNTP-Posting-Host: gateway.scopus.net (62.90.123.5)
X-Trace: fu-berlin.de 1021971511 25501697 62.90.123.5 (16 [79865])
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Here is some wrapper around mktime().

It seems that not 'all results for 1969 and 2106' are valid.

Is something wrong?

Thanks in advance.


######### C++ code : BEGIN #########


// ====================================================
//
// Microsoft Windows 2000 [Version 5.00.2195]
//    Intel(R) Pentium
//    R(4) CPU 1.70GHz
//
// gcc version 3.0.4
//   GNU CPP version 3.0.4 (cpplib) (80386, BSD syntax)
//   GNU C++ version 3.0.4 (djgpp) compiled by GNU C version 3.0.4.
//   Configured with: ../configure i586-pc-msdosdjgpp --prefix=/dev/env/DJDIR --disable-nls
//   Thread model: single
//
// ====================================================


#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
#include <errno.h>

// =================
#include <iostream>
#include <iomanip>
using namespace std;

// =========================
time_t get_mktime (
                int year_i,
                int month_i,
                int day_i,
                int hour_i,
                int min_i,
                int sec_i
                )
{
struct tm       tm_time;
time_t          ret_mktime;

  // ---------------------
  assert (year_i >= 1900);
  assert (month_i >= 1);
  assert (month_i <= 12);
  assert (day_i >= 1);
  assert (day_i <= 31);
  assert (hour_i >= 0);
  assert (hour_i <= 24);
  assert (min_i >= 0);
  assert (min_i <= 60);
  assert (sec_i >= 0);
  assert (sec_i <= 60);

  // ---------------------
  tm_time.tm_year = year_i - 1900;
  tm_time.tm_mon  = month_i - 1;
  tm_time.tm_mday = day_i;

  tm_time.tm_hour = hour_i;
  tm_time.tm_min  = min_i;
  tm_time.tm_sec  = sec_i;

  // ---------------------
  tm_time.tm_isdst = -1;


  // ##############################################
  // ############## man mktime ####################
  // ######## DJGPP Version 2.03 ##################
  // ##############################################
  // <quote>
  //
  // mktime
  // ======
  //
  // Syntax
  // ------
  //
  //      #include <time.h>
  //
  //      time_t mktime(struct tm *tptr);
  //
  // Description
  // -----------
  //
  // This function converts a time structure into the number of seconds since
  // 00:00:00 GMT 1/1/1970.  It also attempts to normalize the fields of
  // TPTR.  The layout of a `struct tm' is as follows:
  //
  //      struct tm {
  //        int    tm_sec;    /* seconds after the minute [0-60] */
  //        int    tm_min;    /* minutes after the hour [0-59] */
  //        int    tm_hour;   /* hours 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 */
  //        int    tm_wday;   /* days since Sunday [0-6] */
  //        int    tm_yday;   /* days since January 1 [0-365] */
  //        int    tm_isdst;  /* Daylight Savings Time flag */
  //        long   tm_gmtoff; /* offset from GMT in seconds */
  //        char * tm_zone;   /* timezone abbreviation */
  //      };
  //
  // If you don't know whether daylight saving is in effect at the moment
  // specified by the contents of TPTR, set the `tm_isdst' member to -1,
  // which will cause `mktime' to compute the DST flag using the data base
  // in the `zoneinfo' subdirectory of your main DJGPP installation.  This
  // requires that you set the environment variable `TZ' to a file in that
  // directory which corresponds to your geographical area.
  //
  // Return Value
  // ------------
  //
  // The resulting time, or -1 if the time in TPTR cannot be described in
  // that format.
  //
  // Portability
  // -----------
  //
  // ANSI, POSIX
  //
  // </quote>
  // ##############################################

  // ---------------------
  tzset ();
  ret_mktime = mktime (&tm_time);
  if (ret_mktime < 0)
  {
    cout << "Cannot execute mktime, return value = "
         << ret_mktime
         << endl;

  }

  // ---------------------
  return ret_mktime;

} // get_mktime ()

// =========================
void print_mktime (
                int year_i,
                int month_i,
                int day_i,
                int hour_i,
                int min_i,
                int sec_i
                )
{

  cout << "DATA-TIME  "

       << setw(4)
       << setfill('0')
       << year_i
       << ":"

       << setw(2)
       << setfill('0')
       << month_i
       << ":"

       << setw(2)
       << setfill('0')
       << day_i
       << "::"

       << setw(2)
       << setfill('0')
       << hour_i
       << ":"

       << setw(2)
       << setfill('0')
       << min_i
       << ":"

       << setw(2)
       << setfill('0')
       << sec_i
       << " ---> ";

time_t result = get_mktime (year_i, month_i, day_i, hour_i, min_i, sec_i);

  if (result == static_cast<time_t> (-1))
  {
    cout << ""
         << "mktime() can't return required time"
         << endl;
  }
  else
  {
     cout << setw(12)
          << result
          << endl;
  }


} // print_mktime

//#############################################
// =========================

int main ()
{

  print_mktime (1969, 12, 31, 0, 0, 57);
  print_mktime (1969, 12, 31, 0, 0, 58);
  print_mktime (1969, 12, 31, 0, 0, 59);
  print_mktime (1969, 12, 31, 0, 1, 0);
  print_mktime (1969, 12, 31, 0, 1, 1);
  print_mktime (1969, 12, 31, 0, 1, 2);
  cout << endl;

  print_mktime (1969, 12, 31, 0, 59, 57);
  print_mktime (1969, 12, 31, 0, 59, 58);
  print_mktime (1969, 12, 31, 0, 59, 59);
  print_mktime (1969, 12, 31, 1, 0, 0);
  print_mktime (1969, 12, 31, 1, 0, 1);
  print_mktime (1969, 12, 31, 1, 0, 2);
  cout << endl;

  print_mktime (1969, 12, 31, 1, 0, 57);
  print_mktime (1969, 12, 31, 1, 0, 58);
  print_mktime (1969, 12, 31, 1, 0, 59);
  print_mktime (1969, 12, 31, 1, 1, 0);
  print_mktime (1969, 12, 31, 1, 1, 1);
  print_mktime (1969, 12, 31, 1, 1, 2);
  cout << endl;


  print_mktime (1969, 12, 31, 23, 0, 57);
  print_mktime (1969, 12, 31, 23, 0, 58);
  print_mktime (1969, 12, 31, 23, 0, 59);
  print_mktime (1969, 12, 31, 23, 1, 0);
  print_mktime (1969, 12, 31, 23, 1, 1);
  print_mktime (1969, 12, 31, 23, 1, 2);
  cout << endl;

  print_mktime (1969, 12, 31, 23, 59, 57);
  print_mktime (1969, 12, 31, 23, 59, 58);
  print_mktime (1969, 12, 31, 23, 59, 59);
  print_mktime (1970, 1, 1, 0, 0, 0);
  print_mktime (1970, 1, 1, 0, 0, 1);
  print_mktime (1970, 1, 1, 0, 0, 2);
  cout << endl;
  cout << endl;


  print_mktime (2106, 2, 7, 6, 28, 12);
  print_mktime (2106, 2, 7, 6, 28, 13);
  print_mktime (2106, 2, 7, 6, 28, 14);
  print_mktime (2106, 2, 7, 6, 28, 15);
  print_mktime (2106, 2, 7, 6, 28, 16);
  print_mktime (2106, 2, 7, 6, 28, 17);
  print_mktime (2106, 2, 7, 6, 28, 18);
  cout << endl;

  print_mktime (2106, 2, 7, 6, 28, 57);
  print_mktime (2106, 2, 7, 6, 28, 58);
  print_mktime (2106, 2, 7, 6, 28, 59);
  print_mktime (2106, 2, 7, 6, 29, 0);
  print_mktime (2106, 2, 7, 6, 29, 1);
  print_mktime (2106, 2, 7, 6, 29, 2);

  return 0;
}

######### C++ code : END ###########




######### Run Log : BEGIN #########

%a.exe


DATA-TIME  1969:12:31::00:00:57 ---> mktime() can't return required time
DATA-TIME  1969:12:31::00:00:58 ---> mktime() can't return required time
DATA-TIME  1969:12:31::00:00:59 ---> mktime() can't return required time
DATA-TIME  1969:12:31::00:01:00 ---> 004294880956
DATA-TIME  1969:12:31::00:01:01 ---> 004294880957
DATA-TIME  1969:12:31::00:01:02 ---> 004294880958

DATA-TIME  1969:12:31::00:59:57 ---> 004294884493
DATA-TIME  1969:12:31::00:59:58 ---> 004294884494
DATA-TIME  1969:12:31::00:59:59 ---> 004294884495
DATA-TIME  1969:12:31::01:00:00 ---> mktime() can't return required time
DATA-TIME  1969:12:31::01:00:01 ---> mktime() can't return required time
DATA-TIME  1969:12:31::01:00:02 ---> mktime() can't return required time

DATA-TIME  1969:12:31::01:00:57 ---> mktime() can't return required time
DATA-TIME  1969:12:31::01:00:58 ---> mktime() can't return required time
DATA-TIME  1969:12:31::01:00:59 ---> mktime() can't return required time
DATA-TIME  1969:12:31::01:01:00 ---> 004294884556
DATA-TIME  1969:12:31::01:01:01 ---> 004294884557
DATA-TIME  1969:12:31::01:01:02 ---> 004294884558

DATA-TIME  1969:12:31::23:00:57 ---> mktime() can't return required time
DATA-TIME  1969:12:31::23:00:58 ---> mktime() can't return required time
DATA-TIME  1969:12:31::23:00:59 ---> mktime() can't return required time
DATA-TIME  1969:12:31::23:01:00 ---> 004294963756
DATA-TIME  1969:12:31::23:01:01 ---> 004294963757
DATA-TIME  1969:12:31::23:01:02 ---> 004294963758

DATA-TIME  1969:12:31::23:59:57 ---> 004294967293
DATA-TIME  1969:12:31::23:59:58 ---> 004294967294
DATA-TIME  1969:12:31::23:59:59 ---> mktime() can't return required time
DATA-TIME  1970:01:01::00:00:00 ---> 000000000000
DATA-TIME  1970:01:01::00:00:01 ---> 000000000001
DATA-TIME  1970:01:01::00:00:02 ---> 000000000002


DATA-TIME  2106:02:07::06:28:12 ---> 004294967292
DATA-TIME  2106:02:07::06:28:13 ---> 004294967293
DATA-TIME  2106:02:07::06:28:14 ---> 004294967294
DATA-TIME  2106:02:07::06:28:15 ---> mktime() can't return required time
DATA-TIME  2106:02:07::06:28:16 ---> 000000000000
DATA-TIME  2106:02:07::06:28:17 ---> 000000000001
DATA-TIME  2106:02:07::06:28:18 ---> 000000000002

DATA-TIME  2106:02:07::06:28:57 ---> 000000000041
DATA-TIME  2106:02:07::06:28:58 ---> 000000000042
DATA-TIME  2106:02:07::06:28:59 ---> 000000000043
DATA-TIME  2106:02:07::06:29:00 ---> mktime() can't return required time
DATA-TIME  2106:02:07::06:29:01 ---> mktime() can't return required time
DATA-TIME  2106:02:07::06:29:02 ---> mktime() can't return required time

######### Run Log : END ###########


--
====================
  Alex Vinokur
    http://up.to/alexvn
    http://go.to/alexv_math
    mailto:alexvn AT bigfoot DOT com
    mailto:alexvn AT go DOT to
  ====================



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019