📄 prtime.c
字号:
} if (zone != TT_UNKNOWN && zone_offset == -1) { switch (zone) { case TT_PST: zone_offset = -8 * 60; break; case TT_PDT: zone_offset = -7 * 60; break; case TT_MST: zone_offset = -7 * 60; break; case TT_MDT: zone_offset = -6 * 60; break; case TT_CST: zone_offset = -6 * 60; break; case TT_CDT: zone_offset = -5 * 60; break; case TT_EST: zone_offset = -5 * 60; break; case TT_EDT: zone_offset = -4 * 60; break; case TT_AST: zone_offset = -4 * 60; break; case TT_NST: zone_offset = -3 * 60 - 30; break; case TT_GMT: zone_offset = 0 * 60; break; case TT_BST: zone_offset = 1 * 60; break; case TT_MET: zone_offset = 1 * 60; break; case TT_EET: zone_offset = 2 * 60; break; case TT_JST: zone_offset = 9 * 60; break; default: PR_ASSERT (0); break; } } /* If we didn't find a year, month, or day-of-the-month, we can't possibly parse this, and in fact, mktime() will do something random (I'm seeing it return "Tue Feb 5 06:28:16 2036", which is no doubt a numerologically significant date... */ if (month == TT_UNKNOWN || date == -1 || year == -1) return PR_FAILURE; memset(&tm, 0, sizeof(tm)); if (sec != -1) tm.tm_sec = sec; if (min != -1) tm.tm_min = min; if (hour != -1) tm.tm_hour = hour; if (date != -1) tm.tm_mday = date; if (month != TT_UNKNOWN) tm.tm_month = (((int)month) - ((int)TT_JAN)); if (year != -1) tm.tm_year = year; if (dotw != TT_UNKNOWN) tm.tm_wday = (((int)dotw) - ((int)TT_SUN)); if (zone == TT_UNKNOWN && default_to_gmt) { /* No zone was specified, so pretend the zone was GMT. */ zone = TT_GMT; zone_offset = 0; } if (zone_offset == -1) { /* no zone was specified, and we're to assume that everything is local. */ struct tm localTime; time_t secs; PR_ASSERT(tm.tm_month > -1 && tm.tm_mday > 0 && tm.tm_hour > -1 && tm.tm_min > -1 && tm.tm_sec > -1); /* * To obtain time_t from a tm structure representing the local * time, we call mktime(). However, we need to see if we are * on 1-Jan-1970 or before. If we are, we can't call mktime() * because mktime() will crash on win16. In that case, we * calculate zone_offset based on the zone offset at * 00:00:00, 2 Jan 1970 GMT, and subtract zone_offset from the * date we are parsing to transform the date to GMT. We also * do so if mktime() returns (time_t) -1 (time out of range). */ /* month, day, hours, mins and secs are always non-negative so we dont need to worry about them. */ if(tm.tm_year >= 1970) { PRInt64 usec_per_sec; localTime.tm_sec = tm.tm_sec; localTime.tm_min = tm.tm_min; localTime.tm_hour = tm.tm_hour; localTime.tm_mday = tm.tm_mday; localTime.tm_mon = tm.tm_month; localTime.tm_year = tm.tm_year - 1900; /* Set this to -1 to tell mktime "I don't care". If you set it to 0 or 1, you are making assertions about whether the date you are handing it is in daylight savings mode or not; and if you're wrong, it will "fix" it for you. */ localTime.tm_isdst = -1; secs = mktime(&localTime); if (secs != (time_t) -1) {#if defined(XP_MAC) && (__MSL__ < 0x6000) /* * The mktime() routine in MetroWerks MSL C * Runtime library returns seconds since midnight, * 1 Jan. 1900, not 1970 - in versions of MSL (Metrowerks Standard * Library) prior to version 6. Only for older versions of * MSL do we adjust the value of secs to the NSPR epoch */ secs -= ((365 * 70UL) + 17) * 24 * 60 * 60;#endif LL_I2L(*result, secs); LL_I2L(usec_per_sec, PR_USEC_PER_SEC); LL_MUL(*result, *result, usec_per_sec); return PR_SUCCESS; } } /* So mktime() can't handle this case. We assume the zone_offset for the date we are parsing is the same as the zone offset on 00:00:00 2 Jan 1970 GMT. */ secs = 86400; (void) MT_safe_localtime(&secs, &localTime); zone_offset = localTime.tm_min + 60 * localTime.tm_hour + 1440 * (localTime.tm_mday - 2); } /* Adjust the hours and minutes before handing them to PR_ImplodeTime(). Note that it's ok for them to be <0 or >24/60 We adjust the time to GMT before going into PR_ImplodeTime(). The zone_offset represents the difference between the time zone parsed and GMT */ tm.tm_hour -= (zone_offset / 60); tm.tm_min -= (zone_offset % 60); *result = PR_ImplodeTime(&tm); return PR_SUCCESS;}/* ******************************************************************* ******************************************************************* ** ** OLD COMPATIBILITY FUNCTIONS ** ******************************************************************* ******************************************************************* *//* *----------------------------------------------------------------------- * * PR_FormatTime -- * * Format a time value into a buffer. Same semantics as strftime(). * *----------------------------------------------------------------------- */PR_IMPLEMENT(PRUint32)PR_FormatTime(char *buf, int buflen, const char *fmt, const PRExplodedTime *tm){ struct tm a; a.tm_sec = tm->tm_sec; a.tm_min = tm->tm_min; a.tm_hour = tm->tm_hour; a.tm_mday = tm->tm_mday; a.tm_mon = tm->tm_month; a.tm_wday = tm->tm_wday; a.tm_year = tm->tm_year - 1900; a.tm_yday = tm->tm_yday; a.tm_isdst = tm->tm_params.tp_dst_offset ? 1 : 0;/* * On some platforms, for example SunOS 4, struct tm has two additional * fields: tm_zone and tm_gmtoff. The following code attempts to obtain * values for these two fields. */#if defined(SUNOS4) || (__GLIBC__ >= 2) || defined(XP_BEOS) if (mktime(&a) == -1) { PR_snprintf(buf, buflen, "can't get timezone"); return 0; }#endif return strftime(buf, buflen, fmt, &a);}/* * The following string arrays and macros are used by PR_FormatTimeUSEnglish(). */static const char* abbrevDays[] ={ "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};static const char* days[] ={ "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};static const char* abbrevMonths[] ={ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};static const char* months[] ={ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};/* * Add a single character to the given buffer, incrementing the buffer pointer * and decrementing the buffer size. Return 0 on error. */#define ADDCHAR( buf, bufSize, ch ) \do \{ \ if( bufSize < 1 ) \ { \ *(--buf) = '\0'; \ return 0; \ } \ *buf++ = ch; \ bufSize--; \} \while(0)/* * Add a string to the given buffer, incrementing the buffer pointer * and decrementing the buffer size appropriately. Return 0 on error. */#define ADDSTR( buf, bufSize, str ) \do \{ \ PRUint32 strSize = strlen( str ); \ if( strSize > bufSize ) \ { \ if( bufSize==0 ) \ *(--buf) = '\0'; \ else \ *buf = '\0'; \ return 0; \ } \ memcpy(buf, str, strSize); \ buf += strSize; \ bufSize -= strSize; \} \while(0)/* Needed by PR_FormatTimeUSEnglish() */static unsigned int pr_WeekOfYear(const PRExplodedTime* time, unsigned int firstDayOfWeek);/*********************************************************************************** * * Description: * This is a dumbed down version of strftime that will format the date in US * English regardless of the setting of the global locale. This functionality is * needed to write things like MIME headers which must always be in US English. * **********************************************************************************/ PR_IMPLEMENT(PRUint32)PR_FormatTimeUSEnglish( char* buf, PRUint32 bufSize, const char* format, const PRExplodedTime* time ){ char* bufPtr = buf; const char* fmtPtr; char tmpBuf[ 40 ]; const int tmpBufSize = sizeof( tmpBuf ); for( fmtPtr=format; *fmtPtr != '\0'; fmtPtr++ ) { if( *fmtPtr != '%' ) { ADDCHAR( bufPtr, bufSize, *fmtPtr ); } else { switch( *(++fmtPtr) ) { case '%': /* escaped '%' character */ ADDCHAR( bufPtr, bufSize, '%' ); break; case 'a': /* abbreviated weekday name */ ADDSTR( bufPtr, bufSize, abbrevDays[ time->tm_wday ] ); break; case 'A': /* full weekday name */ ADDSTR( bufPtr, bufSize, days[ time->tm_wday ] ); break; case 'b': /* abbreviated month name */ ADDSTR( bufPtr, bufSize, abbrevMonths[ time->tm_month ] ); break; case 'B': /* full month name */ ADDSTR(bufPtr, bufSize, months[ time->tm_month ] ); break; case 'c': /* Date and time. */ PR_FormatTimeUSEnglish( tmpBuf, tmpBufSize, "%a %b %d %H:%M:%S %Y", time ); ADDSTR( bufPtr, bufSize, tmpBuf ); break; case 'd': /* day of month ( 01 - 31 ) */ PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_mday ); ADDSTR( bufPtr, bufSize, tmpBuf ); break; case 'H': /* hour ( 00 - 23 ) */ PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_hour ); ADDSTR( bufPtr, bufSize, tmpBuf ); break; case 'I': /* hour ( 01 - 12 ) */ PR_snprintf(tmpBuf,tmpBufSize,"%.2ld", (time->tm_hour%12) ? time->tm_hour%12 : (PRInt32) 12 ); ADDSTR( bufPtr, bufSize, tmpBuf ); break; case 'j': /* day number of year ( 001 - 366 ) */ PR_snprintf(tmpBuf,tmpBufSize,"%.3d",time->tm_yday + 1); ADDSTR( bufPtr, bufSize, tmpBuf ); break; case 'm': /* month number ( 01 - 12 ) */ PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_month+1); ADDSTR( bufPtr, bufSize, tmpBuf ); break; case 'M': /* minute ( 00 - 59 ) */ PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_min ); ADDSTR( bufPtr, bufSize, tmpBuf ); break; case 'p': /* locale's equivalent of either AM or PM */ ADDSTR( bufPtr, bufSize, (time->tm_hour<12)?"AM":"PM" ); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -