bool fs::file::stat(stat_t& info) const { g_tls_error = fse::ok; #ifdef _WIN32 FILE_BASIC_INFO basic_info; if (!GetFileInformationByHandleEx((HANDLE)m_fd, FileBasicInfo, &basic_info, sizeof(FILE_BASIC_INFO))) { return false; } info.is_directory = (basic_info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; info.is_writable = (basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) == 0; info.size = this->size(); info.atime = to_time_t(basic_info.LastAccessTime); info.mtime = to_time_t(basic_info.ChangeTime); info.ctime = to_time_t(basic_info.CreationTime); #else struct stat file_info; if (fstat(m_fd, &file_info) < 0) { return false; } info.is_directory = S_ISDIR(file_info.st_mode); info.is_writable = file_info.st_mode & 0200; // HACK: approximation info.size = file_info.st_size; info.atime = file_info.st_atime; info.mtime = file_info.st_mtime; info.ctime = file_info.st_ctime; #endif return true; }
bool fs::stat(const std::string& path, stat_t& info) { g_tls_error = fse::ok; #ifdef _WIN32 WIN32_FILE_ATTRIBUTE_DATA attrs; if (!GetFileAttributesExW(to_wchar(path).get(), GetFileExInfoStandard, &attrs)) { return false; } info.is_directory = (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; info.is_writable = (attrs.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0; info.size = (u64)attrs.nFileSizeLow | ((u64)attrs.nFileSizeHigh << 32); info.atime = to_time_t(attrs.ftLastAccessTime); info.mtime = to_time_t(attrs.ftLastWriteTime); info.ctime = to_time_t(attrs.ftCreationTime); #else struct stat file_info; if (stat(path.c_str(), &file_info) < 0) { return false; } info.is_directory = S_ISDIR(file_info.st_mode); info.is_writable = file_info.st_mode & 0200; // HACK: approximation info.size = file_info.st_size; info.atime = file_info.st_atime; info.mtime = file_info.st_mtime; info.ctime = file_info.st_ctime; #endif return true; }
bool fs::dir::read(std::string& name, stat_t& info) { g_tls_error = fse::ok; if (!m_path) { return false; } #ifdef _WIN32 WIN32_FIND_DATAW found; if (m_dd == -1) { m_dd = (std::intptr_t)FindFirstFileW(to_wchar(m_path.get() + "/*"s).get(), &found); if (m_dd == -1) { return false; } } else if (!FindNextFileW((HANDLE)m_dd, &found)) { return false; } to_utf8(name, found.cFileName); info.is_directory = (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; info.is_writable = (found.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0; info.size = ((u64)found.nFileSizeHigh << 32) | (u64)found.nFileSizeLow; info.atime = to_time_t(found.ftLastAccessTime); info.mtime = to_time_t(found.ftLastWriteTime); info.ctime = to_time_t(found.ftCreationTime); #else const auto found = ::readdir((DIR*)m_dd); struct stat file_info; if (!found || ::fstatat(::dirfd((DIR*)m_dd), found->d_name, &file_info, 0) < 0) { return false; } name = found->d_name; info.is_directory = S_ISDIR(file_info.st_mode); info.is_writable = file_info.st_mode & 0200; // HACK: approximation info.size = file_info.st_size; info.atime = file_info.st_atime; info.mtime = file_info.st_mtime; info.ctime = file_info.st_ctime; #endif return true; }
int between_ymd(char *from_ymd, char *to_ymd) { time_t t_from; time_t t_to; to_time_t(&t_from, from_ymd); to_time_t(&t_to, to_ymd); int i_diff = difftime(t_to, t_from)/60/60/24; return i_diff; }
time_t to_time_t(const FILETIME& ft) { ULARGE_INTEGER v; v.LowPart = ft.dwLowDateTime; v.HighPart = ft.dwHighDateTime; return to_time_t(v); }
time_t to_time_t(const LARGE_INTEGER& ft) { ULARGE_INTEGER v; v.LowPart = ft.LowPart; v.HighPart = ft.HighPart; return to_time_t(v); }
bool fs::dir::get_first(std::string& name, stat_t& info) { if (m_dd != null) // close previous handle { #ifdef _WIN32 FindClose((HANDLE)m_dd); #else ::closedir((DIR*)m_dd); #endif } m_dd = null; if (!m_path) { return false; } #ifdef _WIN32 WIN32_FIND_DATAW found; m_dd = (intptr_t)FindFirstFileW(m_path.get(), &found); if (m_dd == null) { return false; } to_utf8(name, found.cFileName); info.is_directory = (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; info.is_writable = (found.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0; info.size = ((u64)found.nFileSizeHigh << 32) | (u64)found.nFileSizeLow; info.atime = to_time_t(found.ftLastAccessTime); info.mtime = to_time_t(found.ftLastWriteTime); info.ctime = to_time_t(found.ftCreationTime); return true; #else m_dd = (intptr_t)::opendir(m_path.get()); return get_next(name, info); #endif }
void bucket::add_contact( const key_t& k, const endpoint_proto& ep) { // avoid adding self to contact list if (k == local_key_) return; now_ = update_time(); unsigned int common = common_bits<KEY_SIZE>( string_to_key<KEY_SIZE>(local_key_), string_to_key<KEY_SIZE>(k)); contact_proto c; c.set_key(k); c.mutable_ep()->set_address(ep.address()); c.mutable_ep()->set_port(ep.port()); assert(c.ep().address() != std::string("")); assert(c.ep().port() != std::string("")); c.set_time(to_time_t(now_)); std::pair<unsigned int, contact_proto> p(common, c); // is key here iterator ite = find_key(k); // insert if key not present if (ite == this->end()) { size_t item_count = this->count(common); // too many item in the bucket if (item_count >= BUCKET_SIZE) { ite = this->find(common); std::map<uint64_t, iterator> map_time_ite; for (unsigned int i = 0; i < item_count; ++i) map_time_ite[ite->second.time()] = ite++; this->erase(map_time_ite.begin()->second); } this->insert(p); changed_ = true; } else { // if not the same endpoint change it if (ite->second.ep() != ep) { ite->second.mutable_ep()->set_address(ep.address()); ite->second.mutable_ep()->set_port(ep.port()); } ite->second.set_time(to_time_t(now_)); } }
void RegestryHistory::getOneHistory(const QString& name, QVector<qreal>& timeLabels, QVector<qreal>& values) { assert(m_all_history.find(name) != m_all_history.end()); QMutexLocker locker(&m_accessMutex); time_t startTime = to_time_t(m_startTime); const ValueHistory& a_history = m_all_history[name]; for (const auto& i : a_history) { double i_time = startTime + i.m_timeFromStart.total_seconds(); timeLabels.push_back(i_time); values.push_back(i.m_value); } if (values.size()) { timeLabels.push_back(to_time_t(boost::posix_time::second_clock::local_time())); values.push_back(*(values.end() - 1)); } }
std::tm VirtualClock::pointToTm(time_point point) { std::time_t rawtime = to_time_t(point); std::tm out; #ifdef _WIN32 // On Win32 this is returns a thread-local and there's no _r variant. std::tm* tmPtr = gmtime(&rawtime); out = *tmPtr; #else // On Unix the _r variant uses a local output, so is thread safe. gmtime_r(&rawtime, &out); #endif return out; }
TEST(UtilDateTime, toIsoAndBack) { tm t = {0}; t.tm_hour = 8; t.tm_min = 6; t.tm_sec = 56; t.tm_year = 113; t.tm_mon = 8; t.tm_mday = 10; auto timet = mktime(&t); auto strTestTimeStamp(gcTime::to_iso_string(timet)); auto time = gcTime::from_iso_string(strTestTimeStamp); ASSERT_EQ(timet, time.to_time_t()); }
time_t time(time_t *tp) { time_t tt = to_time_t(transform(now())); if (tp) *tp = tt; return tt; }
void constants::reset() { now = to_time_t(pt::second_clock::universal_time()); }