static void insert_entry(const char *host, struct in_addr *addrs, int count) { stx_cache_entry_t *entry; stx_dns_data_t *data; char *key; size_t n; if (_stx_dns_ttl > 0) { key = strdup(host); data = (stx_dns_data_t *)malloc(sizeof(stx_dns_data_t)); n = count * sizeof(*addrs); if (data) { data->addrs = (struct in_addr *)malloc(n); if (data->addrs) memcpy(data->addrs, addrs, n); data->num_addrs = count; data->cur = 0; data->expires = st_time() + _stx_dns_ttl; } entry = stx_cache_entry_create(key, data, strlen(host) + 1 + sizeof(stx_dns_data_t) + n + stx_cache_entry_sizeof()); if (key && data && data->addrs && entry && stx_cache_entry_insert(_stx_dns_cache, entry) == 0) { stx_cache_entry_release(_stx_dns_cache, entry); return; } if (entry) stx_cache_entry_delete(_stx_dns_cache, entry); else cleanup_entry(key, data); } }
bool AIPlayer::_isSessionExpired() const { if ( _sessionExpiry == 0 ) return false; // No expiry at all! const time_t sessionLength = st_time() - _sessionStart; return ( sessionLength > _sessionExpiry ); }
AIPlayer::AIPlayer( const std::string& id, const std::string& password, const std::string& role /* = "Red" */ ) : Player( id, password ) , _aiLib( NULL ) , _myRole( role ) , _moveNumber( 0 ) , _sessionStart( 0 ) , _sessionExpiry( 2 * 3600 ) // NOTE: 2-hour session. { _sessionStart = st_time(); }
/** * 输出一个当前时间的字符串表示 */ char *err_tstamp(void) { static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; static char str[32]; static time_t lastt = 0; struct tm *tmp; time_t currt = st_time(); if (currt == lastt) return str; tmp = localtime(&currt); sprintf(str, "[%02d/%s/%d:%02d:%02d:%02d] ", tmp->tm_mday, months[tmp->tm_mon], 1900 + tmp->tm_year, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); lastt = currt; return str; }
static int lookup_entry(const char *host, struct in_addr *addrs, int *num_addrs, int rotate) { stx_cache_entry_t *entry; stx_dns_data_t *data; int n; entry = stx_cache_entry_lookup(_stx_dns_cache, host); if (entry) { data = (stx_dns_data_t *)stx_cache_entry_getdata(entry); if (st_time() <= data->expires) { if (*num_addrs == 1) { if (rotate) { *addrs = data->addrs[data->cur++]; if (data->cur >= data->num_addrs) data->cur = 0; } else { *addrs = data->addrs[0]; } } else { n = STX_MIN(*num_addrs, data->num_addrs); memcpy(addrs, data->addrs, n * sizeof(*addrs)); *num_addrs = n; } stx_cache_entry_release(_stx_dns_cache, entry); return 1; } /* * Cache entry expired: decrement its refcount and purge it from cache. */ stx_cache_entry_release(_stx_dns_cache, entry); stx_cache_entry_delete(_stx_dns_cache, entry); } return 0; }
size_t WINAPI StrFTime(string &strDest, const wchar_t *Format,const tm *t) { if (CurLang==-1 && Lang.IsLanguageLoaded()) PrepareStrFTime(); // меняем язык. CurLang=0; size_t Len; for (Len=1; *Format; Format++) { if (*Format!=L'%') { Len++; const wchar_t Text[]={*Format,0}; strDest+=Text; } #if 1 else { string strBuf; switch (*++Format) { case L'L': CurLang=!CurLang; continue; // Краткое имя дня недели (Sun,Mon,Tue,Wed,Thu,Fri,Sat) // abbreviated weekday name case L'a': strBuf=AWeekday[CurLang][!WeekFirst?((t->tm_wday+6)%7):(!t->tm_wday?6:t->tm_wday-1)]; break; // Полное имя дня недели // full weekday name case L'A': strBuf=Weekday[CurLang][!WeekFirst?((t->tm_wday+6)%7):(!t->tm_wday?6:t->tm_wday-1)]; break; // Краткое имя месяца (Jan,Feb,...) // abbreviated month name case L'h': case L'b': strBuf=AMonth[CurLang][t->tm_mon]; break; // Полное имя месяца // full month name case L'B': strBuf=Month[CurLang][t->tm_mon]; break; //Дата и время в формате WDay Mnt Day HH:MM:SS yyyy //appropriate date and time representation case L'c': atime(strBuf,t); break; // Столетие как десятичное число (00 - 99). Например, 1992 => 19 case L'C': strBuf.Format(L"%02d",(t->tm_year+1900)/100); break; // day of month, blank padded case L'e': // Две цифры дня месяца (01 - 31) // day of the month, 01 - 31 case L'd': strBuf.Format(*Format==L'e'?L"%2d":L"%02d",t->tm_mday); break; // hour, 24-hour clock, blank pad case L'k': // Две цифры часа (00 - 23) // hour, 24-hour clock, 00 - 23 case L'H': strBuf.Format(*Format==L'k'?L"%2d":L"%02d",t->tm_hour); break; // hour, 12-hour clock, 1 - 12, blank pad case L'l': // Две цифры часа (01 - 12) // hour, 12-hour clock, 01 - 12 case L'I': { int I=t->tm_hour%12; if (!I) I=12; strBuf.Format(*Format==L'l'?L"%2d":L"%02d",I); break; } // Три цифры дня в году (001 - 366) // day of the year, 001 - 366 case L'j': strBuf.Format(L"%03d",t->tm_yday+1); break; // Две цифры месяца, как десятичное число (1 - 12) // month, 01 - 12 case L'm': { // %mh - Hex month digit // %m0 - ведущий 0 const wchar_t *fmt=Format[1]==L'h'?L"%X":Format[1]==L'0'?L"%02d":L"%d"; if (fmt[1]!=L'd') Format++; strBuf.Format(fmt,t->tm_mon+1); break; } // Две цифры минут (00 - 59) // minute, 00 - 59 case L'M': strBuf.Format(L"%02d",t->tm_min); break; // AM или PM // am or pm based on 12-hour clock case L'p': strBuf=(t->tm_hour/12)?L"PM":L"AM"; break; // Две цифры секунд (00 - 59) // second, 00 - 59 case L'S': strBuf.Format(L"%02d",t->tm_sec); break; // День недели где 0 - Воскресенье (Sunday) (0 - 6) // weekday, Sunday == 0, 0 - 6 case L'w': strBuf.Format(L"%d",t->tm_wday); break; // Две цифры номера недели, где Воскресенье (Sunday) // является первым днем недели (00 - 53) // week of year, Sunday is first day of week case L'U': // Две цифры номера недели, где Понедельник (Monday) // является первым днем недели (00 - 53) // week of year, Monday is first day of week case L'W': { int I=t->tm_wday-(t->tm_yday%7); //I = (chr == 'W'?(!WeekFirst?((t->tm_wday+6)%7):(t->tm_wday? t->tm_wday-1:6)):(t->tm_wday)) - (t->tm_yday % 7); if (I<0) I+=7; strBuf.Format(L"%02d",(t->tm_yday+I-(*Format==L'W'))/7); break; } // date as dd-bbb-YYYY case L'v': // Дата в формате mm.dd.yyyy // appropriate date representation case L'D': case L'x': st_time(strBuf,t,*Format); break; // Время в формате HH:MM:SS // appropriate time representation case L'T': case L'X': { int TimeSeparator=GetTimeSeparator(); strBuf.Format(L"%02d%c%02d%c%02d",t->tm_hour,TimeSeparator,t->tm_min,TimeSeparator,t->tm_sec); break; } // Две цифры года без столетия (00 to 99) // year without a century, 00 - 99 case L'y': strBuf.Format(L"%02d",t->tm_year%100); break; // Год со столетием (19yy-20yy) // year with century case L'Y': strBuf.Format(L"%d",1900+t->tm_year); break; // Имя часового пояса или пусто, если часовой пояс не задан case L'Z': strBuf.Format(L"%+03d%02d",-(_timezone/3600),-(_timezone/60)%60); //Ptr = _tzname[ t->tm_isdst ]; break; // same as \n case L'n': strBuf=L"\n"; break; // same as \t case L't': strBuf=L"\t"; break; case L'%': strBuf=L"%"; break; // time as %I:%M:%S %p case L'r': StrFTime(strBuf,L"%I:%M:%S %p",t); break; // time as %H:%M case L'R': StrFTime(strBuf,L"%H:%M",t); break; // week of year according ISO 8601 case L'V': strBuf.Format(L"%02d",iso8601wknum(t)); break; } strDest+=strBuf; Len+=strBuf.GetLength(); } #endif } if (*Format) return 0; return Len-1; }