Annotation of sys/lib/libsa/ctime.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: ctime.c,v 1.5 2003/08/11 06:23:09 deraadt Exp $ */
2:
3: /*
4: * Copyright (c) 1998 Michael Shalayeff
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26: */
27:
28: #include <sys/time.h>
29: #include "stand.h"
30:
31: #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
32:
33: char *
34: ctime(const time_t *clock)
35: {
36: static const char wdays[][4] = {
37: "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
38: };
39: static const char months[][4] = {
40: "Jan", "Feb", "Mar", "Apr", "May", "Jun",
41: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
42: };
43: static const u_int monthcnt[] = {
44: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
45: };
46: static char buf[64];
47: int ss, mm, hh, wday, month, year;
48: time_t tt = *clock;
49:
50: ss = tt % 60;
51: tt /= 60; /* minutes */
52: mm = tt % 60;
53: tt /= 60; /* hours */
54: hh = tt % 24;
55: tt /= 24; /* days */
56: wday = (4 + tt) % 7; /* weekday, 'twas thursday when time started */
57:
58: for (year = 1970; tt >= 365; year++)
59: tt -= isleap(year)? 366: 365;
60:
61: tt++; /* days are 1-based */
62:
63: for (month = 0; tt > monthcnt[month]; month++)
64: tt -= monthcnt[month];
65:
66: if (month > 2 && isleap(year))
67: tt--;
68:
69: /* no field widths in printf() */
70: snprintf(buf, sizeof buf, "%s %s %d %d:%d:%d %d\n",
71: ((wday < 0 || wday >= 7)? "???": wdays[wday]),
72: ((month < 0 || month >= 12)? "???": months[month]),
73: (int)tt, hh, mm, ss, year);
74: return buf;
75: }
CVSweb