xref: /haiku/src/tests/system/libroot/posix/test_time.c (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 /* Copyright (C) 1991, 1992, 1994, 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <time.h>
22 
23 
24 int
25 main (int argc, char **argv)
26 {
27   time_t t;
28   register struct tm *tp;
29   struct tm tbuf;
30   int lose = 0;
31 
32   --argc;
33   ++argv;
34 
35   do
36     {
37       char buf[BUFSIZ];
38       if (argc > 0)
39 	{
40 	  static char buf[BUFSIZ];
41 	  sprintf(buf, "TZ=%s", *argv);
42 	  if (putenv(buf))
43 	    {
44 	      puts("putenv failed.");
45 	      lose = 1;
46 	    }
47 	  else
48 	    puts (buf);
49 	}
50       tzset();
51       tbuf.tm_year = 72;
52       tbuf.tm_mon = 0;
53       tbuf.tm_mday = 31;
54       tbuf.tm_hour = 6;
55       tbuf.tm_min = 14;
56       tbuf.tm_sec = 50;
57       tbuf.tm_isdst = -1;
58     doit:;
59       t = mktime(&tbuf);
60       if (t == (time_t) -1)
61 	{
62 	  puts("mktime() failed?");
63 	  lose = 1;
64 	}
65       tp = localtime(&t);
66       if (tp == NULL)
67 	{
68 	  puts("localtime() failed.");
69 	  lose = 1;
70 	}
71       else if (strftime(buf, sizeof(buf), "%a %b %d %X %Z %Y", tp) == 0)
72 	{
73 	  puts("strftime() failed.");
74 	  lose = 1;
75 	}
76       else
77 	puts(buf);
78       if (tbuf.tm_year == 101)
79 	{
80 	  tbuf.tm_year = 97;
81 	  tbuf.tm_mon = 0;
82 	  goto doit;
83 	}
84       ++argv;
85     } while (--argc > 0);
86 
87   {
88 #define	SIZE	256
89     char buffer[SIZE];
90     time_t curtime;
91     struct tm *loctime;
92 
93     curtime = time (NULL);
94 
95     loctime = localtime (&curtime);
96 
97     fputs (asctime (loctime), stdout);
98 
99     strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
100     fputs (buffer, stdout);
101     strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
102     fputs (buffer, stdout);
103 
104     loctime->tm_year = 72;
105     loctime->tm_mon = 8;
106     loctime->tm_mday = 12;
107     loctime->tm_hour = 20;
108     loctime->tm_min = 49;
109     loctime->tm_sec = 05;
110     curtime = mktime (loctime);
111     strftime (buffer, SIZE, "%D %T was %w the %jth.\n", loctime);
112     fputs (buffer, stdout);
113   }
114 
115   return (lose ? EXIT_FAILURE : EXIT_SUCCESS);
116 }
117