Technical article 109721

mktime(S) does not normalize the tm structure correctly.

OpenServer 5.0.0OpenServer 5.0.2OpenServer 5.0.4OpenServer 5.0.5

Written · Last revised

Problem

The OpenServer 5 mktime(S) function does not correctly normalize its input structure. For example, it does not correctly calculate going forward from the 24th of September to the 24th of October.

When going back 1 day from the start of a month, mktime subtracts 1 from tm_mon and adds 31 to tm_mday without taking into account the actual number of days in that particular month. This will only produce a correct result for those months that actually have 31 days.

This can be shown using the example below. If executed on SCO OpenServer 5.0.4, it produces:

0 883612800 Thu Jan  1 00:00:00 1998
0 886291200 Sun Feb  1 00:00:00 1998  <-- Correct - 31d
0 886204800 Sat Jan 31 00:00:00 1998
0 886291200 Sun Feb  1 00:00:00 1998
0 888710400 Sun Mar  1 00:00:00 1998  <-- Wrong - 28d
0 888883200 Tue Mar  3 00:00:00 1998
0 888710400 Sun Mar  1 00:00:00 1998
1 891385200 Wed Apr  1 00:00:00 1998  <-- Correct - 31d
1 891298800 Tue Mar 31 00:00:00 1998
1 891385200 Wed Apr  1 00:00:00 1998
1 893977200 Fri May  1 00:00:00 1998  <-- Wrong - 30d
1 893977200 Fri May  1 00:00:00 1998
1 893977200 Fri May  1 00:00:00 1998
1 896655600 Mon Jun  1 00:00:00 1998  <-- Correct - 31d
1 896569200 Sun May 31 00:00:00 1998
1 896655600 Mon Jun  1 00:00:00 1998
1 899247600 Wed Jul  1 00:00:00 1998  <-- Wrong - 30d
1 899247600 Wed Jul  1 00:00:00 1998
etc.
...
...
------------------------------------
#include <time.h>
#include <stdio.h>

/* * When going 1 day back from the start of a month, * mktime subtracts 1 from tm_mon and adds 31 to tm_mday * not considering the month it is going back to. */

int main(void) { struct tm when; time_t result; int month;

for (month = 1 - 1; month < 12; month++) {

/*
 * First day in month
 */

when.tm_isdst = -1; when.tm_year = 1998 - 1900; when.tm_mon = month; when.tm_mday = 1; when.tm_hour = 0; when.tm_min = 0; when.tm_sec = 0;

if ((result = mktime(&when)) != (time_t)-1) { printf("\n%d %d %s", when.tm_isdst, result, asctime(&when)); } else perror("mktime failed");

/*
 * First day in next month
 */

when.tm_mon++; when.tm_isdst = -1; if ((result = mktime(&when)) != (time_t)-1) { printf("%d %d %s", when.tm_isdst, result, asctime(&when)); } else perror("mktime failed");

/* * Go back 1 day to last day in month */

when.tm_mday--;

when.tm_isdst = -1; if ((result = mktime(&when)) != (time_t)-1) { printf("%d %d %s", when.tm_isdst, result, asctime(&when)); } else perror("mktime failed"); }

  return 0;
}

Solution

This problem has been addressed with the following supplements:

SLS OSS603A for OpenServer 5.0.0
SLS OSS601A for OpenServer 5.0.2 and 5.0.4
Release Supplement 505A (rs505a) for OpenServer 5.0.5

See also

ta110166, "What is addressed by SLS OSS603A, the Year 2000 Supplement for SCO OpenServer 5.0.0 with rs500d and net100?"

ta110077, "What is addressed by SLS OSS601A, the Year 2000 Supplement for SCO OpenServer 5.0.2 and 5.0.4?".

ta109909, "What is Release Supplement 505A for SCO OpenServer Release 5.0.5?"

ta109280, "OpenServer 5.0.4 mktime(S) doesn't correctly handle the leap year in year 2000."

manual page: mktime(S)

From the Xinuos technical article database, record 109721. Reproduced with permission. The original is at www.sco.com/ta/109721, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.