Annotation of mstools/samples/sdktools/netwatch/datetime.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  *  datetime.c
                      3:  *  
                      4:  *  Purpose:
                      5:  *      mostly stolen from winfile
                      6:  *      converted to unicode and added PutCounterTime
                      7:  *  
                      8:  *  Owner:
                      9:  *      MikeSart
                     10:  */
                     11: #define UNICODE 1
                     12: 
                     13: #include <windows.h>
                     14: #include <windowsx.h>
                     15: #include <time.h>
                     16: #include "netwatch.h"
                     17: 
                     18: TCHAR   szShortDate[11]     = TEXT("MM/DD/YY");
                     19: TCHAR   szTime[4]           = TEXT(":");
                     20: TCHAR   sz1159[9]           = TEXT("AM");
                     21: TCHAR   sz2359[9]           = TEXT("PM");
                     22: TCHAR   szInternational[]   = TEXT("Intl");
                     23: TCHAR   szComma[4]          = TEXT(",");
                     24: TCHAR   szDecimal[4]        = TEXT(".");
                     25: //INT     iTime               = 1;           // Default to 24-hour time
                     26: INT     iTime               = 0;           // Default to 12-hour time
                     27: INT     iTLZero             = FALSE;       // Default to non-leading zeros
                     28: //INT     iTLZero             = TRUE;        // Default to leading zeros
                     29: 
                     30: #define COUNTOF(_sz)    (sizeof(_sz) / sizeof(TCHAR))
                     31: 
                     32: //--------------------------------------------------------------------------
                     33: //
                     34: //  GetPict() -
                     35: //
                     36: //--------------------------------------------------------------------------
                     37: 
                     38: //  This gets the number of consecutive chrs of the same kind.  This is used
                     39: //  to parse the time picture.  Returns 0 on error.
                     40: UINT
                     41: GetPict(TCHAR ch, TCHAR *szStr)
                     42: {
                     43:     register UINT  count;
                     44: 
                     45:     count = 0;
                     46:     while(ch == *szStr++)
                     47:         count++;
                     48: 
                     49:     return count;
                     50: }
                     51: 
                     52: /*--------------------------------------------------------------------------*/
                     53: /*                                                                          */
                     54: /*  GetInternational() -                                                    */
                     55: /*                                                                          */
                     56: /*--------------------------------------------------------------------------*/
                     57: VOID
                     58: GetInternational(VOID)
                     59: {
                     60:     GetProfileString(szInternational, TEXT("sShortDate"), szShortDate, szShortDate, COUNTOF(szShortDate));
                     61:     if(szShortDate[0])
                     62:         CharUpper(szShortDate);
                     63:     GetProfileString(szInternational, TEXT("sTime"), szTime, szTime, COUNTOF(szTime));
                     64:     GetProfileString(szInternational, TEXT("s1159"), sz1159, sz1159, COUNTOF(sz1159));
                     65:     GetProfileString(szInternational, TEXT("s2359"), sz2359, sz2359, COUNTOF(sz2359));
                     66:     GetProfileString(szInternational, TEXT("sThousand"), szComma, szComma, COUNTOF(szComma));
                     67:     GetProfileString(szInternational, TEXT("sDecimal"), szDecimal, szDecimal, COUNTOF(szDecimal));
                     68: 
                     69:     iTime   = GetProfileInt(szInternational, TEXT("iTime"), iTime);
                     70:     iTLZero = GetProfileInt(szInternational, TEXT("iTLZero"), iTLZero);
                     71: }
                     72: 
                     73: /////////////////////////////////////////////////////////////////////
                     74: //
                     75: // Name:     CreateDate
                     76: //
                     77: // Synopsis: Converts a local time to a localized string
                     78: //
                     79: // IN        lpst       LPSYSTEMTIME  local system time
                     80: // INOUT     lpszOutStr --            string of date
                     81: //
                     82: // Return:   INT   length of date
                     83: //
                     84: // Assumes:  lpszOutStr is large enough for the date string.
                     85: //           Separator is one character long
                     86: //
                     87: // Effects:  lpszOutStr modified.
                     88: //
                     89: //
                     90: // Notes:    albertt (255 1.26)
                     91: //
                     92: /////////////////////////////////////////////////////////////////////
                     93: INT
                     94: CreateDate(LPSYSTEMTIME lpst, TCHAR *szOutStr)
                     95: {
                     96:     INT              i;
                     97:     UINT             cchPictPart;
                     98:     UINT             uDigits, uVal, uValT;
                     99:     register TCHAR   *pszPict;
                    100:     register TCHAR   *pszInStr;
                    101:     TCHAR            *pChar;
                    102:     BOOL             bClipping = FALSE;
                    103: 
                    104:     pszPict = szShortDate;
                    105:     pszInStr = szOutStr;
                    106: 
                    107:     for(i = 0; i < 3; i++) 
                    108:     {
                    109:         cchPictPart = GetPict(*pszPict, pszPict);
                    110:         switch(*pszPict) 
                    111:         {
                    112:         case 'M':
                    113:             uVal = lpst->wMonth;
                    114:             goto CDDoIt;
                    115: 
                    116:         case 'D':
                    117:             uVal = lpst->wDay;
                    118:             goto CDDoIt;
                    119: 
                    120:         case 'Y':
                    121:             uVal = lpst->wYear;
                    122:             bClipping = TRUE;
                    123: 
                    124:             // goto CDDoIt;
                    125: CDDoIt:
                    126: 
                    127:             //
                    128:             // Move pszPict to the separator
                    129:             //
                    130:             pszPict += cchPictPart;
                    131: 
                    132:             //
                    133:             // First calculate how many digits there are
                    134:             //
                    135:             for(uDigits=0, uValT=uVal; uValT;uDigits++) 
                    136:             {
                    137:                 uValT /= 10;
                    138:             }
                    139: 
                    140:             //
                    141:             // Store any necessary leading zeros
                    142:             //
                    143:             while(uDigits < cchPictPart) 
                    144:             {
                    145:                 *pszInStr++ = '0';
                    146:                 cchPictPart--;
                    147:             }
                    148: 
                    149:             //
                    150:             // Allow clipping (1993 -> 93) IF bClipping on
                    151:             // (Only for Year)
                    152:             //
                    153:             if(bClipping && uDigits > cchPictPart) 
                    154:             {
                    155:                 uDigits = cchPictPart;
                    156:             }
                    157:             bClipping = FALSE;
                    158: 
                    159:             //
                    160:             // Write out the number now one digit at a time
                    161:             // From end to beginning..
                    162:             //
                    163:             // Set pszInStr to the end of the date string
                    164:             //
                    165:             pszInStr += uDigits;
                    166: 
                    167:             //
                    168:             // Now walk backwards:
                    169:             // We must pre-decrement pChar since it now points to
                    170:             // the next character AFTER our string.
                    171:             //
                    172:             for(pChar = pszInStr; uDigits; uVal /= 10, uDigits--) 
                    173:             {
                    174:                 *--pChar = (TCHAR) ((uVal % 10) + '0');
                    175:             }
                    176: 
                    177:             //
                    178:             // Add the separator
                    179:             //
                    180:             if(*pszPict)
                    181:                 *pszInStr++ = *pszPict;
                    182: 
                    183:             break;
                    184:         }
                    185:         pszPict++;
                    186:     }
                    187: 
                    188:     *pszInStr = 0;
                    189: 
                    190:     return lstrlen(szOutStr);
                    191: }
                    192: 
                    193: /////////////////////////////////////////////////////////////////////
                    194: //
                    195: // Name:     CreateTime
                    196: //
                    197: // Synopsis: Creates a localized time string for local time
                    198: //
                    199: // IN    lpst       LPSYSTEMTIME  local time
                    200: // INOUT lpszOutStr --            String
                    201: //
                    202: // Return:   INT  length of string
                    203: //
                    204: //
                    205: // Assumes:   lpszOutStr is big enough for all times
                    206: //
                    207: // Effects:   lpszOutStr modified.
                    208: //            Separator is 1 character
                    209: //
                    210: //
                    211: // Notes:
                    212: //
                    213: /////////////////////////////////////////////////////////////////////
                    214: INT
                    215: CreateTime(LPSYSTEMTIME lpst, TCHAR *szOutStr, BOOL fCounter)
                    216: {
                    217:     INT             i;
                    218:     BOOL            bAM;
                    219:     UINT            uHourMinSec;
                    220:     register UINT   uDigit;
                    221:     register TCHAR  *pszInStr;
                    222:     UINT            uValArray[3];
                    223: 
                    224:     uDigit = uValArray[0] = lpst->wHour;
                    225:     uValArray[1] = lpst->wMinute;
                    226:     uValArray[2] = lpst->wSecond;
                    227: 
                    228:     pszInStr = szOutStr;
                    229: 
                    230:     bAM = (uDigit < 12);
                    231: 
                    232:     if(!fCounter && !iTime) 
                    233:     {
                    234:         if(uDigit >= 12)
                    235:             uDigit -= 12;
                    236: 
                    237:         if(!uDigit)
                    238:             uDigit = 12;
                    239:     }
                    240: 
                    241:     uValArray[0] = uDigit;
                    242:     for(i = 0; i < 3; i++) 
                    243:     {
                    244:         uHourMinSec = uValArray[i];
                    245: 
                    246:         if((i == 0) && fCounter)
                    247:         {
                    248:             wsprintf(pszInStr, szFmtNum, uValArray[0]);
                    249:             pszInStr += lstrlen(pszInStr);
                    250:         }
                    251:         else
                    252:         {
                    253:             // This assumes that the values are of two digits only.
                    254:             uDigit = uHourMinSec / 10;
                    255: 
                    256:             if (i > 0)
                    257:                 *pszInStr++ = (TCHAR)(uDigit + '0');
                    258:             else if (uDigit || iTLZero)
                    259:                 *pszInStr++ = (TCHAR)(uDigit + '0');
                    260: 
                    261:             *pszInStr++ = (TCHAR)((uHourMinSec % 10) + '0');
                    262:         }
                    263: 
                    264:         // Assumes time sep. is 1 char long
                    265:         if(i < 2)
                    266:             *pszInStr++ = *szTime;
                    267:     }
                    268: 
                    269:     if(fCounter)
                    270:     {
                    271:         *pszInStr = 0;
                    272:     }
                    273:     else
                    274:     {
                    275:         if(bAM)
                    276:             lstrcpy(pszInStr, sz1159);
                    277:         else
                    278:             lstrcpy(pszInStr, sz2359);
                    279:     }
                    280: 
                    281:     return lstrlen(szOutStr);
                    282: }
                    283: 
                    284: 
                    285: /*--------------------------------------------------------------------------*/
                    286: /*                                                                          */
                    287: /*  PutDate() -                                                             */
                    288: /*                                                                          */
                    289: /*--------------------------------------------------------------------------*/
                    290: TCHAR *
                    291: PutDate(time_t tm_t, TCHAR *szStr)
                    292: {
                    293:     SYSTEMTIME  st;
                    294:     struct tm   *ptm;
                    295: 
                    296:     ptm = localtime(&tm_t);
                    297: 
                    298:     st.wYear        = ptm->tm_year + 1900;
                    299:     st.wMonth       = ptm->tm_mon + 1;
                    300:     st.wDay         = ptm->tm_mday;
                    301: 
                    302:     CreateDate(&st, szStr);
                    303:     return szStr;
                    304: }
                    305: 
                    306: 
                    307: /*--------------------------------------------------------------------------*/
                    308: /*                                                                          */
                    309: /*  PutTime() -                                                             */
                    310: /*                                                                          */
                    311: /*--------------------------------------------------------------------------*/
                    312: TCHAR *
                    313: PutTime(time_t tm_t, TCHAR *szStr)
                    314: {
                    315:     SYSTEMTIME  st;
                    316:     struct tm   *ptm;
                    317: 
                    318:     ptm = localtime(&tm_t);
                    319: 
                    320:     st.wHour            = ptm->tm_hour;
                    321:     st.wMinute          = ptm->tm_min;
                    322:     st.wSecond          = ptm->tm_sec;
                    323:     st.wMilliseconds    = 0;
                    324: 
                    325:     CreateTime(&st, szStr, FALSE);
                    326:     return szStr;
                    327: }
                    328: 
                    329: TCHAR *
                    330: PutCounterTime(DWORD dwTime, TCHAR *szStr)
                    331: {
                    332:     SYSTEMTIME  st;
                    333: 
                    334:     st.wHour            = (WORD)(dwTime / 3600);
                    335:     dwTime              -= (st.wHour * 3600);
                    336:     st.wMinute          = (WORD)(dwTime / 60);
                    337:     dwTime              -= (st.wMinute * 60);
                    338:     st.wSecond          = (WORD)dwTime;
                    339:     st.wMilliseconds    = 0;
                    340: 
                    341:     CreateTime(&st, szStr, TRUE);
                    342:     return szStr;
                    343: }

unix.superglobalmegacorp.com

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