Blame time/test_time.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  time_t t;
Packit 6c4009
  struct tm *tp;
Packit 6c4009
  struct tm tbuf;
Packit 6c4009
  int lose = 0;
Packit 6c4009
Packit 6c4009
  --argc;
Packit 6c4009
  ++argv;
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      char buf[BUFSIZ];
Packit 6c4009
      if (argc > 0)
Packit 6c4009
	{
Packit 6c4009
	  static char buf[BUFSIZ];
Packit 6c4009
	  sprintf(buf, "TZ=%s", *argv);
Packit 6c4009
	  if (putenv(buf))
Packit 6c4009
	    {
Packit 6c4009
	      puts("putenv failed.");
Packit 6c4009
	      lose = 1;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    puts (buf);
Packit 6c4009
	}
Packit 6c4009
      tzset();
Packit 6c4009
      tbuf.tm_year = 72;
Packit 6c4009
      tbuf.tm_mon = 0;
Packit 6c4009
      tbuf.tm_mday = 31;
Packit 6c4009
      tbuf.tm_hour = 6;
Packit 6c4009
      tbuf.tm_min = 14;
Packit 6c4009
      tbuf.tm_sec = 50;
Packit 6c4009
      tbuf.tm_isdst = -1;
Packit 6c4009
    doit:;
Packit 6c4009
      t = mktime(&tbuf);
Packit 6c4009
      if (t == (time_t) -1)
Packit 6c4009
	{
Packit 6c4009
	  puts("mktime() failed?");
Packit 6c4009
	  lose = 1;
Packit 6c4009
	}
Packit 6c4009
      tp = localtime(&t);
Packit 6c4009
      if (tp == NULL)
Packit 6c4009
	{
Packit 6c4009
	  puts("localtime() failed.");
Packit 6c4009
	  lose = 1;
Packit 6c4009
	}
Packit 6c4009
      else if (strftime(buf, sizeof(buf), "%a %b %d %X %Z %Y", tp) == 0)
Packit 6c4009
	{
Packit 6c4009
	  puts("strftime() failed.");
Packit 6c4009
	  lose = 1;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	puts(buf);
Packit 6c4009
      if (tbuf.tm_year == 101)
Packit 6c4009
	{
Packit 6c4009
	  tbuf.tm_year = 97;
Packit 6c4009
	  tbuf.tm_mon = 0;
Packit 6c4009
	  goto doit;
Packit 6c4009
	}
Packit 6c4009
      ++argv;
Packit 6c4009
    } while (--argc > 0);
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
#define	SIZE	256
Packit 6c4009
    char buffer[SIZE];
Packit 6c4009
    time_t curtime;
Packit 6c4009
    struct tm *loctime;
Packit 6c4009
Packit 6c4009
    curtime = time (NULL);
Packit 6c4009
Packit 6c4009
    loctime = localtime (&curtime);
Packit 6c4009
Packit 6c4009
    fputs (asctime (loctime), stdout);
Packit 6c4009
Packit 6c4009
    strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
Packit 6c4009
    fputs (buffer, stdout);
Packit 6c4009
    strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
Packit 6c4009
    fputs (buffer, stdout);
Packit 6c4009
Packit 6c4009
    loctime->tm_year = 72;
Packit 6c4009
    loctime->tm_mon = 8;
Packit 6c4009
    loctime->tm_mday = 12;
Packit 6c4009
    loctime->tm_hour = 20;
Packit 6c4009
    loctime->tm_min = 49;
Packit 6c4009
    loctime->tm_sec = 05;
Packit 6c4009
    curtime = mktime (loctime);
Packit 6c4009
    strftime (buffer, SIZE, "%D %T was %w the %jth.\n", loctime);
Packit 6c4009
    fputs (buffer, stdout);
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  return (lose ? EXIT_FAILURE : EXIT_SUCCESS);
Packit 6c4009
}