Annotation of mstools/samples/sdktools/windiff/gdate.c, revision 1.1.1.1

1.1       root        1: 
                      2: /******************************************************************************\
                      3: *       This is a part of the Microsoft Source Code Samples. 
                      4: *       Copyright (C) 1993 Microsoft Corporation.
                      5: *       All rights reserved. 
                      6: *       This source code is only intended as a supplement to 
                      7: *       Microsoft Development Tools and/or WinHelp documentation.
                      8: *       See these sources for detailed information regarding the 
                      9: *       Microsoft samples programs.
                     10: \******************************************************************************/
                     11: 
                     12: /****************************** Module Header *******************************
                     13: * Module Name: GDATE.C
                     14: *
                     15: * Contains date conversion functions.
                     16: *
                     17: * Functions:
                     18: *
                     19: * gdi_isleap()
                     20: * gdate_daytodmy()
                     21: * gdate_dmytoday()
                     22: * gdate_monthdays()
                     23: * gdate_weeklyday()
                     24: *
                     25: * Comments:
                     26: *
                     27: ****************************************************************************/
                     28: 
                     29: #include <windows.h>
                     30: #include <string.h>
                     31: 
                     32: #include "gutils.h"
                     33: 
                     34: 
                     35: BOOL gdi_isleap(LONG year);
                     36: 
                     37: /*---static data--------------------------------------------*/
                     38: 
                     39: int monthdays[] = {
                     40:         31,
                     41:         28,
                     42:         31,
                     43:         30,
                     44:         31,
                     45:         30,
                     46:         31,
                     47:         31,
                     48:         30,
                     49:         31,
                     50:         30,
                     51:         31
                     52: };
                     53: 
                     54: 
                     55: /***************************************************************************
                     56:  * Function: gdate_daytomy
                     57:  *
                     58:  * Purpose:
                     59:  *
                     60:  * converts day to d/m/y
                     61:  */
                     62: void APIENTRY
                     63: gdate_daytodmy(LONG days, int FAR* yrp, int FAR* monthp, int FAR* dayp)
                     64: {
                     65:         int years;
                     66:         int nleaps;
                     67:         int month;
                     68:         int mdays;
                     69: 
                     70:         /* get number of completed years and calc leap days */
                     71:         years = (int) (days / 365);
                     72:         days = days % 365;
                     73:         nleaps = (years / 4) - (years / 100) + (years / 400);
                     74:         while (nleaps > days) {
                     75:                 days += 365;
                     76:                 years--;
                     77:                 nleaps = (years / 4) - (years / 100) + (years / 400);
                     78:         }
                     79:         days -= nleaps;
                     80: 
                     81:         /* add one year for current (non-complete) year */
                     82:         years++;
                     83: 
                     84: 
                     85:         /* current month */
                     86:         for (month = 0; month < 12; month++) {
                     87:                 mdays = monthdays[month];
                     88:                 if (gdi_isleap(years) && (month == 1)) {
                     89:                         mdays++;
                     90:                 }
                     91:                 if (days == mdays) {
                     92:                         days = 0;
                     93:                         month++;
                     94:                         break;
                     95:                 } else if (days < mdays) {
                     96:                         break;
                     97:                 } else {
                     98:                         days -= mdays;
                     99:                 }
                    100:         }
                    101:         /* conv month from 0-11 to 1-12 */
                    102:         if (monthp != NULL) {
                    103:                 *monthp = month+1;
                    104:         }
                    105:         if (dayp != NULL) {
                    106:                 *dayp = (int) days + 1;
                    107:         }
                    108:         if (yrp != NULL) {
                    109:                 *yrp = years;
                    110:         }
                    111: }
                    112: 
                    113: 
                    114: /***************************************************************************
                    115:  * Function: gdate_dmytoday
                    116:  *
                    117:  * Purpose:
                    118:  *
                    119:  * converts d/m/y to a day
                    120:  */ 
                    121: LONG APIENTRY
                    122: gdate_dmytoday(int yr, int month, int day)
                    123: {
                    124:         int nleaps;
                    125:         int i;
                    126:         long ndays;
                    127: 
                    128:         /* exclude the current year */
                    129:         yr--;
                    130:         nleaps = (yr / 4) - (yr / 100) + (yr / 400);
                    131: 
                    132:         /* in any given year, day 0 is jan1 */
                    133:         month--;
                    134:         day--;
                    135:         ndays = 0;
                    136:         for (i = 0; i < month ; i++) {
                    137:                 ndays += monthdays[i];
                    138:                 if (gdi_isleap(yr+1) && (i == 1)) {
                    139:                         ndays++;
                    140:                 }
                    141:         }
                    142:         ndays = ndays + day + nleaps + (yr * 365L);
                    143:         return(ndays);
                    144: }
                    145: 
                    146: /***************************************************************************
                    147:  * Function: gdate_monthdays
                    148:  *
                    149:  * Purpose:
                    150:  *
                    151:  * Gets number of days in month
                    152:  */
                    153: int APIENTRY
                    154: gdate_monthdays(int month, int year)
                    155: {
                    156:         int ndays;
                    157: 
                    158:         ndays = monthdays[month - 1];
                    159:         if (gdi_isleap(year) && (month == 2)) {
                    160:                 ndays++;
                    161:         }
                    162:         return(ndays);
                    163: }
                    164: 
                    165: /***************************************************************************
                    166:  * Function: gdate_weekday
                    167:  *
                    168:  * Purpose:
                    169:  * 
                    170:  * Gets the day of the week
                    171:  */
                    172: int APIENTRY
                    173: gdate_weekday(long daynr)
                    174: {
                    175:         return((int) ((daynr + 1) % 7));
                    176: }
                    177: 
                    178: 
                    179: /***************************************************************************
                    180:  * Function: gdi_isleap
                    181:  *
                    182:  * Purpose:
                    183:  * 
                    184:  * Determines whether the year is a leap year
                    185:  */
                    186: BOOL
                    187: gdi_isleap(LONG year)
                    188: {
                    189:         if ( ((year % 4) == 0) &&
                    190:                 (((year % 100) != 0) ||
                    191:                 ((year % 400) == 0))) {
                    192:                         return TRUE;
                    193:         } else {
                    194:                 return FALSE;
                    195:         }
                    196: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.