XINU
ascdate.c
Go to the documentation of this file.
1 /* ascdate.c - ascdate */
2 
3 #include <xinu.h>
4 #include <stdio.h>
5 
7  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
8  {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
9  "Aug", "Sep","Oct","Nov","Dec"},
10  {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}};
11 
12 /*------------------------------------------------------------------------
13  * ascdate - Format a date in ascii including day, year hours:mins:secs
14  *------------------------------------------------------------------------
15  */
17  uint32 now, /* Date and time in xinu format */
18  char *str /* String ( >= 20 chars long) */
19  )
20 {
21  uint32 tmp; /* Counts remaining seconds */
22  /* during computations */
23  int32 year, month, day, /* Values for various parts of */
24  hour, minute, second; /* the date */
25  uint32 days; /* Number of days in a year */
26  uint32 leapyrs; /* Number of leap years between */
27  /* 1970 and now */
28  uint32 shift; /* Number of times the calendar */
29  /* shifted by a day */
30  uint32 dayofweek; /* Day of the week (0 - 6) */
31  /* For DST, day of the week for:*/
32  int32 jan1; /* January 1 this year */
33  int32 mar1; /* March 1 this year */
34  int32 nov1; /* November 1 this year */
35  int32 marss; /* Day of second sunday in march*/
36  int32 novfs; /* Day of second sunday in march*/
37  bool8 dst; /* Should we adjust for DST? */
38  int32 i; /* Indexes through months */
39 
40  char *zones[] = {"EST", "CST", "MST", "PST"};
41  char *dzones[] = {"EDT", "CDT", "MDT", "PDT"};
42 
43  /* Compute the year (1970-2099) */
44 
45  for (year=1970 ; TRUE ; year++) {
46  days = isleap(year) ? 366 : 365;
47  tmp = days * SECPERDY;
48  if (tmp > now)
49  break;
50  now -= tmp;
51  }
52 
53  /* Compute the number of whole days that have already passed */
54  /* during the current year (0 through 365) */
55 
56  days = now / SECPERDY;
57 
58  /* Compute the month (0-11) */
59 
60  for (month=0 ; month<12 ; month++) {
61  tmp = Date.dt_msize[month] * SECPERDY;
62  if ((month == 1) && isleap(year)) {
63  tmp += SECPERDY;
64  }
65  if (tmp > now)
66  break;
67  now -= tmp;
68  }
69 
70  /* Compute the day of month (1-31) */
71 
72  day = (int32)( now/SECPERDY ) + 1;
73  now %= SECPERDY;
74 
75  /* Compute the hour (0-23) */
76 
77  hour = (int32) ( now/SECPERHR );
78  now %= SECPERHR;
79 
80  /* Compute the minutes (0-59) */
81 
82  minute = now / SECPERMN;
83  now %= SECPERMN;
84 
85  /* Compute the seconds (0-59) */
86  second = (int32) now;
87 
88  /* Compute the day of the week (0-6) */
89  /* */
90  /* Note: Jan 1, 1970 was a Thursday (4 on a 0 to 6 range) */
91  /* Add one day of the week each full year (365%7 == 1) plus */
92  /* one extra day for each leap year */
93 
94  /* Compute number of leap years prior to current year */
95 
96  leapyrs = (year-1969)/4;
97 
98  /* Compute shift for all previous years (i.e., day of week of */
99  /* Jan 1 of the current year) */
100 
101  shift = 4 + (year - 1970) + leapyrs;
102 
103  /* Remember day of the week for Jan 1 this year (for DST) */
104 
105  jan1 = shift % 7;
106 
107  /* Add the number of days so far this year */
108 
109  shift += days;
110 
111  /* Convert to integer from 0 through 6 */
112 
113  dayofweek = shift % 7;
114 
115  /* */
116  /* Handle daylight savings time */
117  /* */
118  /* Date.dt_daylight specifies whether to set it on, off, */
119  /* or automatically compute the setting */
120  /* */
121 
122  dst = FALSE;
123  if (Date.dt_daylight == DATE_DST_ON) {
124  dst = TRUE;
125  } else if (Date.dt_daylight == DATE_DST_AUTO) {
126 
127  /* Automatic DST calculation: DST is on between 2:00 AM */
128  /* the second Sunday of March and 2:00 AM the first */
129  /* sunday in November */
130 
131  /* Calculate day of the week for March 1 */
132 
133  mar1 = (jan1 + Date.dt_msize[0] + Date.dt_msize[1] +
134  isleap(year)) % 7;
135 
136  /* Caculate day (1-31) of the second sunday in March */
137 
138  if (mar1 == 0) { /* Mar. 1 is a Sunday */
139  marss = 8;
140  } else { /* Mar. 1 is Monday - Saturday */
141  marss = 15 - mar1;
142  }
143 
144  /* Calculate day of the week for November 1 */
145 
146  nov1 = jan1;
147  for (i=0; i<10; i++) {
148  nov1 += Date.dt_msize[i];
149  }
150  if (isleap(year)) {
151  nov1++;
152  }
153  nov1 = nov1 % 7;
154  if (nov1 == 0) { /* Nov. 1 is a Sunday */
155  novfs = 1;
156  } else { /* Nov. 1 is Monday - Saturday */
157  novfs = 8 - nov1;
158  }
159 
160  /* Set dst based on time of the year */
161 
162  /* DST is off during December, January, and Feburary */
163 
164  if ( (month == 11) || (month == 0) || (month == 1) ) {
165  dst = FALSE;
166 
167  /* DST is on from April through October */
168 
169  } else if ( (month>2) && (month<10) ) {
170  dst = TRUE;
171 
172  /* DST is on in March past 2 AM on the second Sunday */
173 
174  } else if (month == 2) {
175 
176  dst = FALSE;
177  if (day > marss) {
178  dst = TRUE;
179  } else if ( (day == marss) && (hour >= 2) ) {
180  dst = TRUE;
181  }
182 
183  /* DST is on in November until 2 am on first Sunday */
184 
185 
186  } else if (month == 10) {
187 
188  dst = TRUE;
189  if (day > novfs) {
190  dst = FALSE;
191  } else if ( (day == novfs) && (hour >= 1) ) {
192  dst = FALSE;
193  }
194  }
195  }
196 
197  /* If we are doing DST, move ahead one hour and handle the */
198  /* case where we move to the next day or the next month */
199 
200  if (dst) {
201  hour++;
202 
203  /* If day exceeded, move to next day */
204 
205  if (hour > 23) {
206  hour = 0;
207  day++;
208 
209  /* If month exceeded, move to next month */
210 
211  if (day > Date.dt_msize[month]) {
212  day = 1;
213  month++;
214 
215  /* Stop here because DST does not occur */
216  /* during the year boundary */
217  }
218  }
219  }
220 
221  sprintf(str, "%3s %3s %2d %2d:%02d:%02d %s %d",
222  Date.dt_dnam[dayofweek], Date.dt_mnam[month],
223  day, hour, minute, second, dst? dzones[TIMEZONE-5]:
224  zones[TIMEZONE-5], year);
225  return OK;
226 }
struct dateinfo Date
Definition: ascdate.c:6
全てのシステムヘッダファイルをインクルードする。
Definition: date.h:26
int32 dt_msize[12]
Definition: date.h:33
status ascdate(uint32 now, char *str)
Definition: ascdate.c:16
#define OK
処理が成功した場合
Definition: kernel.h:77
#define TIMEZONE
Definition: date.h:53
byte bool8
Boolean値
Definition: kernel.h:36
int32 sprintf(char *, char *,...)
Definition: sprintf.c:12
int32 status
ステータスを意味する返り値の型(OK/SYSERR)
Definition: kernel.h:57
char * dt_mnam[12]
Definition: date.h:34
#define SECPERDY
Definition: date.h:43
char * dt_dnam[7]
Definition: date.h:35
#define FALSE
Boolean False(0)
Definition: kernel.h:63
#define TRUE
Boolean True(1)
Definition: kernel.h:65
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
#define isleap(x)
Definition: date.h:42
#define DATE_DST_AUTO
Definition: date.h:22
#define DATE_DST_ON
Definition: date.h:21
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
int32 dt_daylight
Definition: date.h:31
#define SECPERMN
Definition: date.h:45
#define SECPERHR
Definition: date.h:44