Logo Search packages:      
Sourcecode: pwlib version File versions  Download package

PString PTime::AsString ( const char *  formatPtr,
int  zone = Local 
) const

Parameters:
formatPtr  Arbitrary format C string pointer for time.
zone  Time zone for the time.

Definition at line 544 of file ptime.cxx.

References GetDayName(), GetMonthName(), GetTimeAM(), GetTimePM(), GetTimeZone(), and Local.

{
  PAssert(format != NULL, PInvalidParameter);

  BOOL is12hour = strchr(format, 'a') != NULL;

  PStringStream str;
  str.fill('0');

  // the localtime call automatically adjusts for daylight savings time
  // so take this into account when converting non-local times
  if (zone == Local)
    zone = GetTimeZone();  // includes daylight savings time
  time_t realTime = theTime + zone*60;     // to correct timezone
  struct tm ts;
  struct tm * t = os_gmtime(&realTime, &ts);

  PINDEX repeatCount;

  while (*format != '\0') {
    repeatCount = 1;
    switch (*format) {
      case 'a' :
        while (*++format == 'a')
          ;
        if (t->tm_hour < 12)
          str << GetTimeAM();
        else
          str << GetTimePM();
        break;

      case 'h' :
        while (*++format == 'h')
          repeatCount++;
        str << setw(repeatCount) << (is12hour ? (t->tm_hour+11)%12+1 : t->tm_hour);
        break;

      case 'm' :
        while (*++format == 'm')
          repeatCount++;
        str << setw(repeatCount) << t->tm_min;
        break;

      case 's' :
        while (*++format == 's')
          repeatCount++;
        str << setw(repeatCount) << t->tm_sec;
        break;

      case 'w' :
        while (*++format == 'w')
          repeatCount++;
        if (repeatCount != 3 || *format != 'e')
          str << GetDayName((Weekdays)t->tm_wday, repeatCount <= 3 ? Abbreviated : FullName);
        else {
          static const char * const EnglishDayName[] = {
            "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
          };
          str << EnglishDayName[t->tm_wday];
          format++;
        }
        break;

      case 'M' :
        while (*++format == 'M')
          repeatCount++;
        if (repeatCount < 3)
          str << setw(repeatCount) << (t->tm_mon+1);
        else if (repeatCount > 3 || *format != 'E')
          str << GetMonthName((Months)(t->tm_mon+1),
                                    repeatCount == 3 ? Abbreviated : FullName);
        else {
          static const char * const EnglishMonthName[] = {
            "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
          };
          str << EnglishMonthName[t->tm_mon];
          format++;
        }
        break;

      case 'd' :
        while (*++format == 'd')
          repeatCount++;
        str << setw(repeatCount) << t->tm_mday;
        break;

      case 'y' :
        while (*++format == 'y')
          repeatCount++;
        if (repeatCount < 3)
          str << setw(2) << (t->tm_year%100);
        else
          str << setw(4) << (t->tm_year+1900);
        break;

      case 'z' :
      case 'Z' :
        if (zone == 0) {
          if (*format == 'Z')
            str << 'Z';
          else
            str << "GMT";
        }
        else {
          str << (zone < 0 ? '-' : '+');
          zone = PABS(zone);
          str << setw(2) << (zone/60) << setw(2) << (zone%60);
        }
        while (toupper(*++format) == 'z')
          ;
        break;

      case 'u' :
        while (*++format == 'u')
          repeatCount++;
        switch (repeatCount) {
          case 1 :
            str << (microseconds/100000);
            break;
          case 2 :
            str << setw(2) << (microseconds/10000);
            break;
          case 3 :
            str << setw(3) << (microseconds/1000);
            break;
          default :
            str << setw(6) << microseconds;
            break;
        }
        break;

      case '\\' :
        format++;
        // Escaped character, put straight through to output string

      default :
        str << *format++;
    }
  }

  return str;
}


Generated by  Doxygen 1.6.0   Back to index