1 /*
2 * $Id: ntime.c,v 1.2 2002/07/06 08:51:42 isizaka Exp isizaka $
3 *
4 * This file is part of "Ngraph for X11".
5 *
6 * Copyright (C) 2002, Satoshi ISHIZAKA. isizaka@msa.biglobe.ne.jp
7 *
8 * "Ngraph for X11" is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * "Ngraph for X11" is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 */
23
24 /**
25 *
26 * $Log: ntime.c,v $
27 * Revision 1.2 2002/07/06 08:51:42 isizaka
28 * change to GPL.
29 *
30 * Revision 1.1 1999/03/17 13:46:09 isizaka
31 * Initial revision
32 *
33 *
34 **/
35
36 #include <time.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "object.h"
40 #include "ntime.h"
41
42 char *weekstr[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
43 char *monthstr[12]={"Jan","Feb","Mar","Apr","May","Jun",
44 "Jul","Aug","Sep","Oct","Nov","Dec"};
45
ndate(time_t * timep,int style)46 char *ndate(time_t *timep,int style)
47 {
48 struct tm *ltime;
49 char *c;
50
51 ltime=localtime(timep);
52 if ((c=memalloc(16))==NULL) return NULL;
53 switch (style) {
54 case 1:
55 sprintf(c,"%d-%d-%d",ltime->tm_mon+1,ltime->tm_mday,1900+ltime->tm_year);
56 break;
57 case 2:
58 sprintf(c,"%s %d %d",monthstr[ltime->tm_mon],
59 ltime->tm_mday,1900+ltime->tm_year);
60 break;
61 case 3:
62 sprintf(c,"%d-%d-%d",ltime->tm_mday,ltime->tm_mon+1,1900+ltime->tm_year);
63 break;
64 case 4:
65 sprintf(c,"%d/%d/%d",ltime->tm_mon+1,ltime->tm_mday,1900+ltime->tm_year);
66 break;
67 default:
68 sprintf(c,"%s %s %d %d",weekstr[ltime->tm_wday],monthstr[ltime->tm_mon],
69 ltime->tm_mday,1900+ltime->tm_year);
70 break;
71 }
72 return c;
73 }
74
ntime(time_t * timep,int style)75 char *ntime(time_t *timep,int style)
76 {
77 struct tm *ltime;
78 char *c;
79
80 ltime=localtime(timep);
81 if ((c=memalloc(12))==NULL) return NULL;
82 switch (style) {
83 case 1:
84 if (ltime->tm_hour<12)
85 sprintf(c,"%02d:%02d:%02d am",ltime->tm_hour,
86 ltime->tm_min,ltime->tm_sec);
87 else
88 sprintf(c,"%02d:%02d:%02d pm",ltime->tm_hour-12,
89 ltime->tm_min,ltime->tm_sec);
90 break;
91 case 2:
92 sprintf(c,"%02d:%02d",ltime->tm_hour,ltime->tm_min);
93 break;
94 case 3:
95 if (ltime->tm_hour<12)
96 sprintf(c,"%02d:%02d am",ltime->tm_hour,ltime->tm_min);
97 else
98 sprintf(c,"%02d:%02d pm",ltime->tm_hour-12,ltime->tm_min);
99 break;
100 default:
101 sprintf(c,"%02d:%02d:%02d",ltime->tm_hour,ltime->tm_min,ltime->tm_sec);
102 break;
103 }
104 return c;
105 }
106
gettimeval(char * s,time_t * time)107 int gettimeval(char *s,time_t *time)
108 {
109 char *endptr;
110 struct tm tm;
111 int year;
112
113 tm.tm_mday=strtol(s,&endptr,10);
114 if (endptr[0]!='-') return -1;
115 s=endptr+1;
116 tm.tm_mon=strtol(s,&endptr,10)-1;
117 if (endptr[0]!='-') return -1;
118 s=endptr+1;
119 year=strtol(s,&endptr,10)-1900;
120 if (year<0) year+=1900;
121 tm.tm_year=year;
122 if (endptr[0]!=' ') return -1;
123 s=endptr+1;
124 tm.tm_hour=strtol(s,&endptr,10);
125 if (endptr[0]!=':') return -1;
126 s=endptr+1;
127 tm.tm_min=strtol(s,&endptr,10);
128 if (endptr[0]!=':') return -1;
129 s=endptr+1;
130 tm.tm_sec=strtol(s,&endptr,10);
131 tm.tm_isdst=0;
132 *time=mktime(&tm);
133 return 0;
134 }
135
gettimeval2(char * s,time_t * time)136 int gettimeval2(char *s,time_t *time)
137 {
138 char *endptr;
139 struct tm tm;
140
141 tm.tm_year=strtol(s,&endptr,10)-1900;
142 if (endptr[0]!='-') return -1;
143 s=endptr+1;
144 tm.tm_mon=strtol(s,&endptr,10)-1;
145 if (endptr[0]!='-') return -1;
146 s=endptr+1;
147 tm.tm_mday=strtol(s,&endptr,10);
148 if (endptr[0]!=' ') return -1;
149 s=endptr+1;
150 tm.tm_hour=strtol(s,&endptr,10);
151 if (endptr[0]!=':') return -1;
152 s=endptr+1;
153 tm.tm_min=strtol(s,&endptr,10);
154 if (endptr[0]!=':') return -1;
155 s=endptr+1;
156 tm.tm_sec=strtol(s,&endptr,10);
157 tm.tm_isdst=0;
158 *time=mktime(&tm);
159 return 0;
160 }
161
162