# chrono

***

**1. Measure Execution Time of a Function**

```cpp
#include <chrono>

using namespace std::chrono;

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

**2. Sleep for a Specified Duration**

```cpp
#include <chrono>

using namespace std::chrono;

int main() {
  auto duration = milliseconds(500);
  this_thread::sleep_for(duration);
  std::cout << "Slept for 500 milliseconds\n";
}
```

**3. Get Current System Time**

```cpp
#include <chrono>

using namespace std::chrono;

int main() {
  auto now = system_clock::now();
  auto time_point = time_point_cast<system_clock::time_point>(now);
  std::cout << "Current system time: " << time_point.time_since_epoch().count() << " seconds\n";
}
```

**4. Convert Time Points to Different Time Scales**

```cpp
#include <chrono>

using namespace std::chrono;

int main() {
  auto now = system_clock::now();
  auto time_point_seconds = time_point_cast<seconds>(now);
  auto time_point_milliseconds = time_point_cast<milliseconds>(now);
  std::cout << "Time in seconds: " << time_point_seconds.time_since_epoch().count() << "\n";
  std::cout << "Time in milliseconds: " << time_point_milliseconds.time_since_epoch().count() << "\n";
}
```

**5. Perform Date and Time Arithmetic**

```cpp
#include <chrono>

using namespace std::chrono;

int main() {
  auto now = system_clock::now();
  auto one_day_later = now + days(1);
  auto one_hour_ago = now - hours(1);
  std::cout << "One day later: " << one_day_later << "\n";
  std::cout << "One hour ago: " << one_hour_ago << "\n";
}
```

**6. Compare Time Points**

```cpp
#include <chrono>

using namespace std::chrono;

int main() {
  auto now = system_clock::now();
  auto later = now + hours(2);
  if (later > now) {
    std::cout << "Later is after now\n";
  } else if (later < now) {
    std::cout << "Later is before now\n";
  } else {
    std::cout << "Later is equal to now\n";
  }
}
```

**7. Round Durations to Nearest Units**

```cpp
#include <chrono>
#include <cmath>

using namespace std::chrono;

int main() {
  auto duration = milliseconds(523);
  auto rounded_seconds = duration_cast<seconds>(duration);
  auto rounded_minutes = duration_cast<minutes>(duration);
  std::cout << "Rounded to seconds: " << rounded_seconds.count() << " seconds\n";
  std::cout << "Rounded to minutes: " << rounded_minutes.count() << " minutes\n";
}
```

**8. Create and Format Date and Time Objects**

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

using namespace std;
using namespace std::chrono;

int main() {
  system_clock::time_point time_point = system_clock::now();
  time_t time_t_value = system_clock::to_time_t(time_point);
  tm* tm_value = localtime(&time_t_value);
  cout << put_time(tm_value, "%a %b %d %H:%M:%S %Y") << endl;
}
```

**9. Parse and Convert Date and Time Strings**

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

using namespace std;
using namespace std::chrono;

int main() {
  string date_time_string = "2023-03-08 12:34:56";
  stringstream ss(date_time_string);
  tm tm_value;
  ss >> get_time(&tm_value, "%Y-%m-%d %H:%M:%S");
  system_clock::time_point time_point = system_clock::from_time_t(mktime(&tm_value));
  cout << "Parsed time point: " << time_point << endl;
}
```

**10. Duration Calculations with Floating-Point**

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

using namespace std;
using namespace std::chrono;

int main() {
  duration<double> duration = duration_cast<duration<double>>(milliseconds(1234));
  cout << "Duration in seconds: " << duration.count() << endl;
}
```

**11. Measure CPU Time of a Process**

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

using namespace std;
using namespace std::chrono;

int main() {
  auto start = process_clock::now();
  // Code to be timed
  auto end = process_clock::now();
  auto duration = duration_cast<milliseconds>(end - start);
  cout << "CPU time: " << duration.count() << " milliseconds" << endl;
}
```

**12. Calculate Time Differences Using Ratio Types**

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

using namespace std;
using namespace std::chrono;

int main() {
  duration<int, ratio<3600>> hours(1);
  duration<int, ratio<60>> minutes(30);
  auto total_minutes = hours + minutes;
  cout << "Total minutes: " << total_minutes.count() << endl;
}
```

**13. Use Chrono in Multithreaded Environments**

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

using namespace std;
using namespace std::chrono;

int main() {
  auto start = steady_clock::now();
  thread t([]() {
    // Code to be executed in a separate thread
  });
  t.join();
  auto end = steady_clock::now();
  auto duration = duration_cast<milliseconds>(end - start);
  cout << "Thread execution time: " << duration.count() << " milliseconds\n";
}
```

**14. Animate or Time Events in Graphics**

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

using namespace std;
using namespace std::chrono;

int main() {
  auto previous_time = steady_clock::now();
  while (true) {
    auto current_time = steady_clock::now();
    auto elapsed_time = duration_cast<milliseconds>(current_time - previous_time);
    if (elapsed_time.count() >= 16) {
      // Update animations or graphics here
      previous_time = current_time;
    }
  }
}
```

**15. Implement a Countdown Timer**

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

using namespace std;
using namespace std::chrono;

int main() {
  int countdown_time = 10;
  auto start = high_resolution_clock::now();
  while (countdown_time >= 0) {
    auto current = high_resolution_clock::now();
    auto elapsed_time = duration_cast<seconds>(current - start);
    int time_left = countdown_time - elapsed_time.count();
    cout << "Time left: " << time_left << " seconds" << endl;
    this_thread::sleep_for(seconds(1));
    countdown_time--;
  }
  cout << "Countdown finished!" << endl;
}
```

**16. Measure Latency in a Network Connection**

```cpp
#include <chrono>
#include <iostream>
#include <netdb.h>
#include <sys/socket.h>

using namespace std;
using namespace std::chrono;

int main() {
  // Create a socket
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    cout << "Error creating socket" << endl;
    return 1;
  }

  // Get the IP address of the server
  struct hostent* host = gethostbyname("www.example.com");
  if (host == NULL) {
    cout << "Error getting host address" << endl;
    return 1;
  }

  // Connect to the server
  struct sockaddr_in server_addr;
  server_addr.sin_family

```
