~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to gnulib/mktime.c

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
   with this program; if not, write to the Free Software Foundation,
18
18
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
19
 
 
20
/* Define this to have a standalone program to test this implementation of
 
21
   mktime.  */
 
22
/* #define DEBUG 1 */
 
23
 
20
24
#ifndef _LIBC
21
25
# include <config.h>
22
26
#endif
34
38
 
35
39
#include <string.h>             /* For the real memcpy prototype.  */
36
40
 
 
41
#if defined(DEBUG)
 
42
# include <stdio.h>
 
43
# include <stdlib.h>
 
44
/* Make it work even if the system's libc has its own mktime routine.  */
 
45
# define mktime my_mktime
 
46
#endif /* DEBUG */
37
47
 
38
48
/* Shift A right by B bits portably, by dividing A by 2**B and
39
49
   truncating towards minus infinity.  A and B should be free of side
129
139
  };
130
140
 
131
141
 
 
142
#ifndef mktime_internal
 
143
time_t mktime_internal (struct tm *tp,
 
144
                   struct tm *(*convert) (const time_t *, struct tm *),
 
145
                   time_t *offset);
 
146
#endif
 
147
 
132
148
#ifndef _LIBC
133
149
/* Portable standalone applications should supply a <time.h> that
134
150
   declares a POSIX-compliant localtime_r, for the benefit of older
252
268
}
253
269
 
254
270
 
255
 
extern time_t
256
 
__mktime_internal (struct tm *tp,
257
 
                   struct tm *(*convert) (const time_t *, struct tm *),
258
 
                   time_t *offset);
259
271
/* Convert *TP to a time_t value, inverting
260
272
   the monotonic and mostly-unit-linear conversion function CONVERT.
261
273
   Use *OFFSET to keep track of a guess at the offset of the result,
284
296
  int mday = tp->tm_mday;
285
297
  int mon = tp->tm_mon;
286
298
  int year_requested = tp->tm_year;
287
 
  /* Normalize the value.  */
288
 
  int isdst = ((tp->tm_isdst >> (8 * sizeof (tp->tm_isdst) - 1))
289
 
               | (tp->tm_isdst != 0));
 
299
  int isdst = tp->tm_isdst;
290
300
 
291
301
  /* 1 if the previous probe was DST.  */
292
302
  int dst2;
517
527
libc_hidden_def (mktime)
518
528
libc_hidden_weak (timelocal)
519
529
#endif
 
530
 
 
531
#if defined(DEBUG)
 
532
 
 
533
static int
 
534
not_equal_tm (const struct tm *a, const struct tm *b)
 
535
{
 
536
  return ((a->tm_sec ^ b->tm_sec)
 
537
          | (a->tm_min ^ b->tm_min)
 
538
          | (a->tm_hour ^ b->tm_hour)
 
539
          | (a->tm_mday ^ b->tm_mday)
 
540
          | (a->tm_mon ^ b->tm_mon)
 
541
          | (a->tm_year ^ b->tm_year)
 
542
          | (a->tm_yday ^ b->tm_yday)
 
543
          | (a->tm_isdst ^ b->tm_isdst));
 
544
}
 
545
 
 
546
static void
 
547
print_tm (const struct tm *tp)
 
548
{
 
549
  if (tp)
 
550
    printf ("%04d-%02d-%02d %02d:%02d:%02d yday %03d wday %d isdst %d",
 
551
            tp->tm_year + TM_YEAR_BASE, tp->tm_mon + 1, tp->tm_mday,
 
552
            tp->tm_hour, tp->tm_min, tp->tm_sec,
 
553
            tp->tm_yday, tp->tm_wday, tp->tm_isdst);
 
554
  else
 
555
    printf ("0");
 
556
}
 
557
 
 
558
static int
 
559
check_result (time_t tk, struct tm tmk, time_t tl, const struct tm *lt)
 
560
{
 
561
  if (tk != tl || !lt || not_equal_tm (&tmk, lt))
 
562
    {
 
563
      printf ("mktime (");
 
564
      print_tm (lt);
 
565
      printf (")\nyields (");
 
566
      print_tm (&tmk);
 
567
      printf (") == %ld, should be %ld\n", (long int) tk, (long int) tl);
 
568
      return 1;
 
569
    }
 
570
 
 
571
  return 0;
 
572
}
 
573
 
 
574
int
 
575
main (int argc, char **argv)
 
576
{
 
577
  int status = 0;
 
578
  struct tm tm, tmk, tml;
 
579
  struct tm *lt;
 
580
  time_t tk, tl, tl1;
 
581
  char trailer;
 
582
 
 
583
  if ((argc == 3 || argc == 4)
 
584
      && (sscanf (argv[1], "%d-%d-%d%c",
 
585
                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &trailer)
 
586
          == 3)
 
587
      && (sscanf (argv[2], "%d:%d:%d%c",
 
588
                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &trailer)
 
589
          == 3))
 
590
    {
 
591
      tm.tm_year -= TM_YEAR_BASE;
 
592
      tm.tm_mon--;
 
593
      tm.tm_isdst = argc == 3 ? -1 : atoi (argv[3]);
 
594
      tmk = tm;
 
595
      tl = mktime (&tmk);
 
596
      lt = localtime (&tl);
 
597
      if (lt)
 
598
        {
 
599
          tml = *lt;
 
600
          lt = &tml;
 
601
        }
 
602
      printf ("mktime returns %ld == ", (long int) tl);
 
603
      print_tm (&tmk);
 
604
      printf ("\n");
 
605
      status = check_result (tl, tmk, tl, lt);
 
606
    }
 
607
  else if (argc == 4 || (argc == 5 && strcmp (argv[4], "-") == 0))
 
608
    {
 
609
      time_t from = atol (argv[1]);
 
610
      time_t by = atol (argv[2]);
 
611
      time_t to = atol (argv[3]);
 
612
 
 
613
      if (argc == 4)
 
614
        for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1)
 
615
          {
 
616
            lt = localtime (&tl);
 
617
            if (lt)
 
618
              {
 
619
                tmk = tml = *lt;
 
620
                tk = mktime (&tmk);
 
621
                status |= check_result (tk, tmk, tl, &tml);
 
622
              }
 
623
            else
 
624
              {
 
625
                printf ("localtime (%ld) yields 0\n", (long int) tl);
 
626
                status = 1;
 
627
              }
 
628
            tl1 = tl + by;
 
629
            if ((tl1 < tl) != (by < 0))
 
630
              break;
 
631
          }
 
632
      else
 
633
        for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1)
 
634
          {
 
635
            /* Null benchmark.  */
 
636
            lt = localtime (&tl);
 
637
            if (lt)
 
638
              {
 
639
                tmk = tml = *lt;
 
640
                tk = tl;
 
641
                status |= check_result (tk, tmk, tl, &tml);
 
642
              }
 
643
            else
 
644
              {
 
645
                printf ("localtime (%ld) yields 0\n", (long int) tl);
 
646
                status = 1;
 
647
              }
 
648
            tl1 = tl + by;
 
649
            if ((tl1 < tl) != (by < 0))
 
650
              break;
 
651
          }
 
652
    }
 
653
  else
 
654
    printf ("Usage:\
 
655
\t%s YYYY-MM-DD HH:MM:SS [ISDST] # Test given time.\n\
 
656
\t%s FROM BY TO # Test values FROM, FROM+BY, ..., TO.\n\
 
657
\t%s FROM BY TO - # Do not test those values (for benchmark).\n",
 
658
            argv[0], argv[0], argv[0]);
 
659
 
 
660
  return status;
 
661
}
 
662
 
 
663
#endif /* DEBUG */
 
664
 
 
665
/*
 
666
Local Variables:
 
667
compile-command: "gcc -DDEBUG -Wall -W -O -g mktime.c -o mktime"
 
668
End:
 
669
*/