# time

***

**1. Current Time and Date**

```cpp
#include <iostream>
#include <ctime>

int main() {
  time_t t = time(nullptr);
  struct tm *now = localtime(&t);
  std::cout << "Current time and date: " << now->tm_mday << '/'
            << (now->tm_mon + 1) << '/' << (now->tm_year + 1900) << ' '
            << now->tm_hour << ':' << now->tm_min << ':' << now->tm_sec << std::endl;
  return 0;
}
```

**2. Sleep for a Specific Duration**

```cpp
#include <iostream>
#include <thread>

int main() {
  std::cout << "Sleeping for 5 seconds..." << std::endl;
  std::this_thread::sleep_for(std::chrono::seconds(5));
  std::cout << "Done sleeping." << std::endl;
  return 0;
}
```

**3. Measure Execution Time**

```cpp
#include <iostream>
#include <chrono>

int main() {
  auto start = std::chrono::high_resolution_clock::now();
  // Code to be timed
  auto end = std::chrono::high_resolution_clock::now();
  auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
  std::cout << "Execution time: " << duration << " milliseconds." << std::endl;
  return 0;
}
```

**4. Limit Execution Time**

```cpp
#include <iostream>
#include <thread>
#include <chrono>

int main() {
  auto start = std::chrono::high_resolution_clock::now();
  while (true) {
    // Code to be executed
    if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - start).count() >= 60) {
      std::cout << "Time limit reached." << std::endl;
      break;
    }
  }
  return 0;
}
```

**5. Retry with Exponential Backoff**

```cpp
#include <iostream>
#include <chrono>

int main() {
  int retries = 5;
  int wait_time = 1;  // Initial wait time in seconds
  for (int i = 0; i < retries; i++) {
    // Code to be retried
    if (result == success) {
      std::cout << "Success!" << std::endl;
      break;
    } else {
      std::cout << "Failed. Retrying in " << wait_time << " second(s)..." << std::endl;
      std::this_thread::sleep_for(std::chrono::seconds(wait_time));
      wait_time *= 2;  // Double the wait time for each retry
    }
  }
  return 0;
}
```

**6. Timed Mutex Lock**

```cpp
#include <iostream>
#include <mutex>
#include <thread>
#include <chrono>

std::mutex m;

void try_lock_thread() {
  if (m.try_lock_for(std::chrono::seconds(10))) {
    // Critical section
    std::cout << "Thread acquired the lock." << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(5));
    m.unlock();
    std::cout << "Thread released the lock." << std::endl;
  } else {
    std::cout << "Thread failed to acquire the lock." << std::endl;
  }
}

int main() {
  std::thread t(try_lock_thread);
  t.join();
  return 0;
}
```

**7. Scheduled Tasks**

```cpp
#include <iostream>
#include <thread>
#include <chrono>

void scheduled_task() {
  std::cout << "Scheduled task running." << std::endl;
}

int main() {
  std::thread t(scheduled_task);
  t.join();
  return 0;
}
```

**8. Timed Event Loop**

```cpp
#include <iostream>
#include <thread>
#include <chrono>

void event_handler() {
  std::cout << "Event handler called." << std::endl;
}

int main() {
  while (true) {
    // Check for events
    event_handler();
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
  }
  return 0;
}
```

**9. Rate Limiter**

```cpp
#include <iostream>
#include <chrono>
#include <atomic>

std::atomic<int> requests_count{0};
std::chrono::steady_clock::time_point last_request_time;

bool rate_limit_check() {
  auto now = std::chrono::steady_clock::now();
  if (now - last_request_time >= std::chrono::seconds(1)) {
    last_request_time = now;
    requests_count.exchange(0);
    return true;
  } else {
    requests_count.fetch_add(1);
    return requests_count <= 10;  // Max 10 requests per second
  }
}

int main() {
  while (true) {
    if (rate_limit_check()) {
      // Process request
    } else {
      std::cout << "Too many requests. Try again later." << std::endl;
    }
  }
  return 0;
}
```

**10. Deadline Timer**

```cpp
#include <iostream>
#include <chrono>

void deadline_handler() {
  std::cout << "Deadline reached." << std::endl;
}

int main() {
  auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5);
  while (std::chrono::steady_clock::now() < deadline) {
    // Do something
  }
  deadline_handler();
  return 0;
}
```

**11. Performance Counter**

```cpp
#include <iostream>
#include <chrono>

int main() {
  auto start = std::chrono::high_resolution_clock::now();
  // Code to be measured
  auto end = std::chrono::high_resolution_clock::now();
  auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
  std::cout << "Performance: " << duration << " milliseconds." << std::endl;
  return 0;
}
```

**12. Time Zone Conversion**

```cpp
#include <iostream>
#include <ctime>

int main() {
  time_t t = time(nullptr);
  std::tm *now = localtime(&t);
  std::cout << "Local time: " << now->tm_mday << '/'
            << (now->tm_mon + 1) << '/' << (now->tm_year + 1900) << ' '
            << now->tm_hour << ':' << now->tm_min << ':' << now->tm_sec << std::endl;
  std::tm *utc = gmtime(&t);
  std::cout << "UTC time: " << utc->tm_mday << '/'
            << (utc->tm_mon + 1) << '/' << (utc->tm_year + 1900) << ' '
            << utc->tm_hour << ':' << utc->tm_min << ':' << utc->tm_sec << std::endl;
  return 0;
}
```

**13. Time Leap Detection**

```cpp
#include <iostream>
#include <chrono>

int main() {
  auto last_time = std::chrono::steady_clock::now();
  while (true) {
    auto now = std::chrono::steady_clock::now();
    auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_time).count();
    if (diff > 1000) {  // Time leap detected if more than 1 second has passed
      std::cout << "Time leap detected." << std::endl;
    }
    last_time = now;
  }
  return 0;
}
```

**14. Precise Time Measurement**

```cpp
#include <iostream>
#include <chrono>

int main() {
  auto start = std::chrono::high_resolution_clock::now();
  // Code to be timed
  auto end = std::chrono::high_resolution_clock::now();
  auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
  std::cout << "Duration: " << duration << " nanoseconds." << std::endl;
  return 0;
}
```

**15. Wait for a Specific Time**

```cpp
#include <iostream>
#include <chrono>

int main() {
  auto wait_time = std::chrono::seconds(10);
  auto start = std::chrono::steady_clock::now();
  while (std::chrono::steady_clock::now() - start < wait_time) {
    // Do something
  }
  std::cout << "Wait time expired." << std::endl;
  return 0;
}
```

**16. Date and Time Formatting**

```cpp
#include <iostream>
#include <iomanip>
#include <chrono>

int main() {
  auto now = std::chrono::system_clock::now();
  std::time_t time_t = std::chrono::system_clock::to_time_t(now);
  std::cout << "Formatted date and time: " << std::put_time(std::localtime(&time_t), "%c") << std::endl;
  return 0;
}

```
