Guest User

strftime

a guest
May 9th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.93 KB | None | 0 0
  1. From strftime.c
  2. /***
  3. *_expandtime() - Expand the conversion specifier
  4. *
  5. *Purpose:
  6. *       Expand the given strftime conversion specifier using the time struct
  7. *       and store it in the supplied buffer.
  8. *
  9. *       The expansion is locale-dependent.
  10. *
  11. *       *** For internal use with strftime() only ***
  12. *
  13. *Entry:
  14. *       char specifier = strftime conversion specifier to expand
  15. *       const struct tm *tmptr = pointer to time/date structure
  16. *       char **string = address of pointer to output string
  17. *       size_t *count = address of char count (space in output area)
  18. *       struct __lc_time_data *lc_time = pointer to locale-specific info
  19. *
  20. *Exit:
  21. *       BOOL true for success, false for failure
  22. *
  23. *Exceptions:
  24. *
  25. *******************************************************************************/
  26.  
  27. static BOOL __cdecl _expandtime (
  28.         _locale_t plocinfo,
  29.         char specifier,
  30.         const struct tm *timeptr,
  31.         char **string,
  32.         size_t *left,
  33.         struct __lc_time_data *lc_time,
  34.         unsigned alternate_form
  35.         )
  36. {
  37.         unsigned temp;                  /* temps */
  38.         int wdaytemp;
  39.  
  40.         /* Use a copy of the appropriate __lc_time_data pointer.  This
  41.         should prevent the necessity of locking/unlocking in mthread
  42.         code (if we can guarantee that the various __lc_time data
  43.         structures are always in the same segment). contents of time
  44.         strings structure can now change, so thus we do use locking */
  45.  
  46.         switch(specifier) {             /* switch on specifier */
  47.  
  48.         case('a'):              /* abbreviated weekday name */
  49.         {
  50.             _VALIDATE_RETURN( ( ( timeptr->tm_wday >=0 ) && ( timeptr->tm_wday <= 6 ) ), EINVAL, FALSE)
  51.             _store_str((char *)(lc_time->wday_abbr[timeptr->tm_wday]),
  52.                      string, left);
  53.             break;
  54.         }
  55.  
  56.  
  57.         case('A'):              /* full weekday name */
  58.         {
  59.             _VALIDATE_RETURN( ( ( timeptr->tm_wday >=0 ) && ( timeptr->tm_wday <= 6 ) ), EINVAL, FALSE)
  60.             _store_str((char *)(lc_time->wday[timeptr->tm_wday]),
  61.                      string, left);
  62.             break;
  63.         }
  64.  
  65.         case('b'):              /* abbreviated month name */
  66.         {
  67.             _VALIDATE_RETURN( ( ( timeptr->tm_mon >=0 ) && ( timeptr->tm_mon <= 11 ) ), EINVAL, FALSE)
  68.             _store_str((char *)(lc_time->month_abbr[timeptr->tm_mon]),
  69.                      string, left);
  70.             break;
  71.         }
  72.  
  73.         case('B'):              /* full month name */
  74.         {
  75.             _VALIDATE_RETURN( ( ( timeptr->tm_mon >=0 ) && ( timeptr->tm_mon <= 11 ) ), EINVAL, FALSE)
  76.             _store_str((char *)(lc_time->month[timeptr->tm_mon]),
  77.                      string, left);
  78.             break;
  79.         }
  80.  
  81.         case('c'):              /* date and time display */
  82.             if (alternate_form)
  83.             {
  84.                 if(!_store_winword( plocinfo,
  85.                                 WW_LDATEFMT,
  86.                                 timeptr,
  87.                                 string,
  88.                                 left,
  89.                                 lc_time))
  90.                 {
  91.                     return FALSE;
  92.                 }
  93.  
  94.                 if (*left == 0)
  95.                     return FALSE;
  96.                 *(*string)++=' ';
  97.                 (*left)--;
  98.                 if(!_store_winword( plocinfo,
  99.                                 WW_TIMEFMT,
  100.                                 timeptr,
  101.                                 string,
  102.                                 left,
  103.                                 lc_time))
  104.                 {
  105.                     return FALSE;
  106.                 }
  107.  
  108.             }
  109.             else {
  110.                 if(!_store_winword( plocinfo,
  111.                                 WW_SDATEFMT,
  112.                                 timeptr,
  113.                                 string,
  114.                                 left,
  115.                                 lc_time))
  116.                 {
  117.                     return FALSE;
  118.                 }
  119.                 if (*left == 0)
  120.                     return FALSE;
  121.                 *(*string)++=' ';
  122.                 (*left)--;
  123.                 if(!_store_winword( plocinfo,
  124.                                 WW_TIMEFMT,
  125.                                 timeptr,
  126.                                 string,
  127.                                 left,
  128.                                 lc_time))
  129.                 {
  130.                     return FALSE;
  131.                 }
  132.             }
  133.             break;
  134.  
  135.         case('d'):              /* mday in decimal (01-31) */
  136.             /* pass alternate_form as the no leading zeros flag */
  137.         {
  138.             _VALIDATE_RETURN( ( ( timeptr->tm_mday >=1 ) && ( timeptr->tm_mday <= 31 ) ), EINVAL, FALSE)
  139.             _store_num(timeptr->tm_mday, 2, string, left,
  140.                        alternate_form);
  141.             break;
  142.         }
  143.  
  144.         case('H'):              /* 24-hour decimal (00-23) */
  145.             /* pass alternate_form as the no leading zeros flag */
  146.         {
  147.             _VALIDATE_RETURN( ( ( timeptr->tm_hour >=0 ) && ( timeptr->tm_hour <= 23 ) ), EINVAL, FALSE)
  148.             _store_num(timeptr->tm_hour, 2, string, left,
  149.                        alternate_form);
  150.             break;
  151.         }
  152.  
  153.         case('I'):              /* 12-hour decimal (01-12) */
  154.         {
  155.             _VALIDATE_RETURN( ( ( timeptr->tm_hour >=0 ) && ( timeptr->tm_hour <= 23 ) ), EINVAL, FALSE)
  156.             if (!(temp = timeptr->tm_hour%12))
  157.                 temp=12;
  158.             /* pass alternate_form as the no leading zeros flag */
  159.             _store_num(temp, 2, string, left, alternate_form);
  160.             break;
  161.         }
  162.  
  163.         case('j'):              /* yday in decimal (001-366) */
  164.             /* pass alternate_form as the no leading zeros flag */
  165.         {
  166.             _VALIDATE_RETURN( ( ( timeptr->tm_yday >=0 ) && ( timeptr->tm_yday <= 365 ) ), EINVAL, FALSE)
  167.             _store_num(timeptr->tm_yday+1, 3, string, left,
  168.                        alternate_form);
  169.             break;
  170.         }
  171.  
  172.         case('m'):              /* month in decimal (01-12) */
  173.             /* pass alternate_form as the no leading zeros flag */
  174.         {
  175.             _VALIDATE_RETURN( ( ( timeptr->tm_mon >=0 ) && ( timeptr->tm_mon <= 11 ) ), EINVAL, FALSE)
  176.             _store_num(timeptr->tm_mon+1, 2, string, left,
  177.                        alternate_form);
  178.             break;
  179.         }
  180.  
  181.         case('M'):              /* minute in decimal (00-59) */
  182.             /* pass alternate_form as the no leading zeros flag */
  183.         {
  184.             _VALIDATE_RETURN( ( ( timeptr->tm_min >=0 ) && ( timeptr->tm_min <= 59 ) ), EINVAL, FALSE)
  185.             _store_num(timeptr->tm_min, 2, string, left,
  186.                        alternate_form);
  187.             break;
  188.         }
  189.  
  190.         case('p'):              /* AM/PM designation */
  191.         {
  192.             _VALIDATE_RETURN( ( ( timeptr->tm_hour >=0 ) && ( timeptr->tm_hour <= 23 ) ), EINVAL, FALSE)
  193.             if (timeptr->tm_hour <= 11)
  194.                 _store_str((char *)(lc_time->ampm[0]), string, left);
  195.             else
  196.                 _store_str((char *)(lc_time->ampm[1]), string, left);
  197.             break;
  198.         }
  199.  
  200.         case('S'):              /* secs in decimal (00-59) */
  201.             /* pass alternate_form as the no leading zeros flag */
  202.         {
  203.             _VALIDATE_RETURN( ( ( timeptr->tm_sec >=0 ) && ( timeptr->tm_sec <= 59 ) ), EINVAL, FALSE)
  204.             _store_num(timeptr->tm_sec, 2, string, left,
  205.                        alternate_form);
  206.             break;
  207.         }
  208.  
  209.         case('U'):              /* sunday week number (00-53) */
  210.             _VALIDATE_RETURN( ( ( timeptr->tm_wday >=0 ) && ( timeptr->tm_wday <= 6 ) ), EINVAL, FALSE)
  211.             wdaytemp = timeptr->tm_wday;
  212.             goto weeknum;   /* join common code */
  213.  
  214.         case('w'):              /* week day in decimal (0-6) */
  215.             /* pass alternate_form as the no leading zeros flag */
  216.         {
  217.             _VALIDATE_RETURN( ( ( timeptr->tm_wday >=0 ) && ( timeptr->tm_wday <= 6 ) ), EINVAL, FALSE)
  218.             _store_num(timeptr->tm_wday, 1, string, left,
  219.                        alternate_form);
  220.             break;
  221.         }
  222.  
  223.         case('W'):              /* monday week number (00-53) */
  224.             _VALIDATE_RETURN( ( ( timeptr->tm_wday >=0 ) && ( timeptr->tm_wday <= 6 ) ), EINVAL, FALSE)
  225.             if (timeptr->tm_wday == 0)  /* monday based */
  226.                 wdaytemp = 6;
  227.             else
  228.                 wdaytemp = timeptr->tm_wday-1;
  229.         weeknum:
  230.             _VALIDATE_RETURN( ( ( timeptr->tm_yday >=0 ) && ( timeptr->tm_yday <= 365 ) ), EINVAL, FALSE)
  231.             if (timeptr->tm_yday < wdaytemp)
  232.                 temp = 0;
  233.             else {
  234.                 temp = timeptr->tm_yday/7;
  235.                 if ((timeptr->tm_yday%7) >= wdaytemp)
  236.                     temp++;
  237.             }
  238.             /* pass alternate_form as the no leading zeros flag */
  239.             _store_num(temp, 2, string, left, alternate_form);
  240.             break;
  241.  
  242.         case('x'):              /* date display */
  243.             if (alternate_form)
  244.             {
  245.                 if(!_store_winword( plocinfo,
  246.                                 WW_LDATEFMT,
  247.                                 timeptr,
  248.                                 string,
  249.                                 left,
  250.                                 lc_time))
  251.                 {
  252.                     return FALSE;
  253.                 }
  254.             }
  255.             else
  256.             {
  257.                 if(!_store_winword( plocinfo,
  258.                                 WW_SDATEFMT,
  259.                                 timeptr,
  260.                                 string,
  261.                                 left,
  262.                                 lc_time))
  263.                 {
  264.                     return FALSE;
  265.                 }
  266.             }
  267.             break;
  268.  
  269.         case('X'):              /* time display */
  270.             if(!_store_winword( plocinfo,
  271.                             WW_TIMEFMT,
  272.                             timeptr,
  273.                             string,
  274.                             left,
  275.                             lc_time))
  276.                 {
  277.                     return FALSE;
  278.                 }
  279.             break;
  280.  
  281.         case('y'):              /* year w/o century (00-99) */
  282.         {
  283.             _VALIDATE_RETURN( ( timeptr->tm_year >=0 ), EINVAL, FALSE)
  284.             temp = timeptr->tm_year%100;
  285.             /* pass alternate_form as the no leading zeros flag */
  286.             _store_num(temp, 2, string, left, alternate_form);
  287.             break;
  288.         }
  289.  
  290.         case('Y'):              /* year w/ century */
  291.         {
  292.             _VALIDATE_RETURN( ( timeptr->tm_year >= -1900 ) && ( timeptr->tm_year <= 8099 ), EINVAL, FALSE)
  293.             temp = (((timeptr->tm_year/100)+19)*100) +
  294.                    (timeptr->tm_year%100);
  295.             /* pass alternate_form as the no leading zeros flag */
  296.             _store_num(temp, 4, string, left, alternate_form);
  297.             break;
  298.         }
  299.  
  300.         case('Z'):              /* time zone name, if any */
  301.         case('z'):              /* time zone name, if any */
  302.             __tzset();      /* Set time zone info */
  303. _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
  304.             _store_str(_tzname[((timeptr->tm_isdst)?1:0)],
  305.                      string, left);
  306. _END_SECURE_CRT_DEPRECATION_DISABLE
  307.             break;
  308.  
  309.         case('%'):              /* percent sign */
  310.             *(*string)++ = '%';
  311.             (*left)--;
  312.             break;
  313.  
  314.         case('\004'):           /* Workaround issue in older RogueWave libraries */
  315.         case('\015'):
  316.             break;
  317.  
  318.         default:                /* unknown format directive */
  319.             /* ignore the directive and continue */
  320.             /* [ANSI: Behavior is undefined.]    */
  321.             _ASSERTE( ( "Invalid format directive" , 0 ) );
  322.             return FALSE;
  323.             break;
  324.  
  325.         }       /* end % switch */
  326.  
  327.         return TRUE;
  328. }
Advertisement
Add Comment
Please, Sign In to add comment