Changeset 14 for trunk/src/helpers/datetime.c
- Timestamp:
- Dec 9, 2000, 8:19:42 PM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/datetime.c
r8 r14 25 25 /* 26 26 * This file Copyright (C) 1997-2000 Ulrich Mller. 27 * This file is part of the XWorkplacesource package.28 * XWorkplaceis free software; you can redistribute it and/or modify27 * This file is part of the "XWorkplace helpers" source package. 28 * This is free software; you can redistribute it and/or modify 29 29 * it under the terms of the GNU General Public License as published 30 30 * by the Free Software Foundation, in version 2 as it comes in the … … 63 63 const char *pcszFormatTimestamp = "%4u%02u%02u%02u%02u%02u%"; 64 64 65 ULONG G_ulDateScalarFirstCalled = 0; 66 65 67 /* 66 68 *@@ dtGetULongTime: … … 71 73 * time in milliseconds. 72 74 * 73 * Warning: this does not handle day information. When called twice 74 * in between day changes, this returns incorrect information. 75 * Even though this does handle date information (i.e. 76 * will still return an increasing number when the 77 * clock switches from 23:59:59:9999 to 0:00:00:0000), 78 * this will not work forver after the first call. 79 * Here's the calculation: 80 * 81 + 1000 ms per second 82 + * 60 secs per minute 83 + * 60 minutes per hour 84 + * 24 hours per day 85 + = 86'400'000 after 23:59:59:9999 86 * 87 * A ULONG can hold a max value of 4'294'967'295. 88 * So this overflows after 49.71... days. 89 * 90 *@@changed V0.9.7 (2000-12-05) [umoeller]: now handling date also 75 91 */ 76 92 … … 78 94 { 79 95 DATETIME dt; 96 ULONG ulTime, 97 ulDateScalarPassed = 1; 80 98 DosGetDateTime(&dt); 81 return (10*(dt.hundredths + 100*(dt.seconds + 60*(dt.minutes + 60*(dt.hours))))); 99 ulTime = (10*(dt.hundredths + 100*(dt.seconds + 60*(dt.minutes + 60*(dt.hours))))); 100 101 if (G_ulDateScalarFirstCalled == 0) 102 { 103 // first call: 104 G_ulDateScalarFirstCalled = dtDate2Scalar(dt.year, 105 dt.month, 106 dt.day); 107 } 108 else 109 { 110 // not first call: 111 ULONG ulDateScalarNow = dtDate2Scalar(dt.year, 112 dt.month, 113 dt.day); 114 // calculate days passed since first call; 115 // this should be 1 if the date hasn't changed 116 ulDateScalarPassed = (G_ulDateScalarFirstCalled - ulDateScalarNow) + 1; 117 } 118 119 return (ulTime * ulDateScalarPassed); 82 120 } 83 121 … … 147 185 148 186 /* 187 ** 188 ** 189 ** day: day of month 190 ** mon: month (1-12) 191 ** yr: year 192 ** 193 ** 194 */ 195 196 /* 197 *@@ dtDayOfWeek: 198 * returns an integer that represents the day of 199 * the week for the date passed as parameters. 200 * 201 * Returns 0-6 where 0 is sunday. 202 * 203 *@@added V0.9.7 (2000-12-05) [umoeller] 204 */ 205 206 ULONG dtDayOfWeek(ULONG day, 207 ULONG mon, // 1-12 208 ULONG yr) 209 { 210 int dow; 211 212 if (mon <= 2) 213 { 214 mon += 12; 215 yr -= 1; 216 } 217 dow = ( day 218 + mon * 2 219 + ((mon + 1) * 6) / 10 220 + yr 221 + yr / 4 222 - yr / 100 223 + yr / 400 224 + 2); 225 dow = dow % 7; 226 return ((dow ? dow : 7) - 1); 227 } 228 229 /* 149 230 *@@ dtIsLeapYear: 150 231 * returns TRUE if yr is a leap year. … … 164 245 /* 165 246 *@@ dtMonths2Days: 166 * returns the no. of days for month. 247 * returns the no. of days for the beginning 248 * of "month" (starting from 1). 249 * 250 * For example, if you pass 1 (for january), 251 * you get 0 because there's no days at jan 1st 252 * yet. 253 * 254 * If you pass 2 (for february), you get 31. 255 * 256 * If you pass 3 (for march), you get 61. 257 * 258 * This is useful for computing a day index 259 * for a given month/day pair. Pass the month 260 * in here and add (day-1); for march 3rd, 261 * you then get 63. 167 262 * 168 263 * (c) Ray Gardner. … … 171 266 unsigned dtMonths2Days(unsigned month) 172 267 { 173 return (month * 3057 - 3007) / 100;268 return (month * 3057 - 3007) / 100; 174 269 } 175 270 … … 228 323 *pyr = n; 229 324 n = (unsigned)(scalar - dtYears2Days(n-1)); 230 if ( n > 59 ) { /* adjust if past February */ 325 if ( n > 59 ) /* adjust if past February */ 326 { 231 327 n += 2; 232 328 if ( dtIsLeapYear(*pyr) ) … … 237 333 } 238 334 239 335 /* 336 *@@ dtIsValidDate: 337 * returns TRUE if the given date is valid. 338 * 339 *@@added V0.9.7 (2000-12-05) [umoeller] 340 */ 341 342 BOOL dtIsValidDate(LONG day, // in: day (1-31) 343 LONG month, // in: month (1-12) 344 ULONG year) // in: year (e.g. 1999) 345 { 346 BOOL brc = FALSE; 347 if (day > 0) 348 { 349 switch( month ) 350 { 351 case 1 : 352 case 3 : 353 case 5 : 354 case 7 : 355 case 8 : 356 case 10 : 357 case 12 : 358 if (day <= 31) 359 brc = TRUE; 360 break; 361 362 case 4 : 363 case 6 : 364 case 9 : 365 case 11 : 366 if (day <= 30) 367 brc = TRUE; 368 break; 369 370 case 2 : 371 if (day < 29) 372 brc = TRUE; 373 else 374 if (day == 29) 375 if (dtIsLeapYear(year)) 376 return 1 ; 377 } 378 } 379 return (brc); 380 } 381
Note:
See TracChangeset
for help on using the changeset viewer.