Back to index

glibc  2.9
test_time.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1994, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <time.h>
00022 
00023 
00024 int
00025 main (int argc, char **argv)
00026 {
00027   time_t t;
00028   register struct tm *tp;
00029   struct tm tbuf;
00030   int lose = 0;
00031 
00032   --argc;
00033   ++argv;
00034 
00035   do
00036     {
00037       char buf[BUFSIZ];
00038       if (argc > 0)
00039        {
00040          static char buf[BUFSIZ];
00041          sprintf(buf, "TZ=%s", *argv);
00042          if (putenv(buf))
00043            {
00044              puts("putenv failed.");
00045              lose = 1;
00046            }
00047          else
00048            puts (buf);
00049        }
00050       tzset();
00051       tbuf.tm_year = 72;
00052       tbuf.tm_mon = 0;
00053       tbuf.tm_mday = 31;
00054       tbuf.tm_hour = 6;
00055       tbuf.tm_min = 14;
00056       tbuf.tm_sec = 50;
00057       tbuf.tm_isdst = -1;
00058     doit:;
00059       t = mktime(&tbuf);
00060       if (t == (time_t) -1)
00061        {
00062          puts("mktime() failed?");
00063          lose = 1;
00064        }
00065       tp = localtime(&t);
00066       if (tp == NULL)
00067        {
00068          puts("localtime() failed.");
00069          lose = 1;
00070        }
00071       else if (strftime(buf, sizeof(buf), "%a %b %d %X %Z %Y", tp) == 0)
00072        {
00073          puts("strftime() failed.");
00074          lose = 1;
00075        }
00076       else
00077        puts(buf);
00078       if (tbuf.tm_year == 101)
00079        {
00080          tbuf.tm_year = 97;
00081          tbuf.tm_mon = 0;
00082          goto doit;
00083        }
00084       ++argv;
00085     } while (--argc > 0);
00086 
00087   {
00088 #define       SIZE   256
00089     char buffer[SIZE];
00090     time_t curtime;
00091     struct tm *loctime;
00092 
00093     curtime = time (NULL);
00094 
00095     loctime = localtime (&curtime);
00096 
00097     fputs (asctime (loctime), stdout);
00098 
00099     strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
00100     fputs (buffer, stdout);
00101     strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
00102     fputs (buffer, stdout);
00103 
00104     loctime->tm_year = 72;
00105     loctime->tm_mon = 8;
00106     loctime->tm_mday = 12;
00107     loctime->tm_hour = 20;
00108     loctime->tm_min = 49;
00109     loctime->tm_sec = 05;
00110     curtime = mktime (loctime);
00111     strftime (buffer, SIZE, "%D %T was %w the %jth.\n", loctime);
00112     fputs (buffer, stdout);
00113   }
00114 
00115   return (lose ? EXIT_FAILURE : EXIT_SUCCESS);
00116 }