Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to create a timezone converter form Local to other timezones in C++

2 Answers

0 votes
#include <iostream>
#include <ctime>

int main()
{
    time_t sys_time = time(nullptr);
    struct tm *local = localtime(&sys_time);
    
    std::cout << "Local time:\t" << asctime(local);
    
    std::cout << "Choose time zone:\n"
              << "1. Beijing (+8:00 UTC)\n"
              << "2. Central America (-6:00 UTC)\n"
              << "3. Greenland (-3:00 UTC)\n"
              << "4. Paris (+1:00 UTC)\n"
              << "5. Alaska (-9:00 UTC)\n";


    int time_zone = 0;
    std::cin >> time_zone;

    if (time_zone < 1 || time_zone > 5) {
        std::cerr << "Invalid time zone selection.\n";
        return 1;
    }

    // Time zone offsets in hours relative to UTC
    int offsets[] = { 8, -6, -3, 1, -9 };
    const char* zone_names[] = {
        "Alaska", "Central America", "Greenland", "Paris", "Beijing"
    };

    int offset_hours = offsets[time_zone - 1];
    time_t adjusted_time = sys_time + offset_hours * 3600;

    struct tm *adjusted = gmtime(&adjusted_time);
    std::cout << "Time in " << zone_names[time_zone - 1] << " is:\t"
              << asctime(adjusted);
}


/*
run:

Local time:	Sat Nov  1 06:50:52 2025
Choose time zone:
1. Beijing (+8:00 UTC)
2. Central America (-6:00 UTC)
3. Greenland (-3:00 UTC)
4. Paris (+1:00 UTC)
5. Alaska (-9:00 UTC)
4
Time in Paris is:	Sat Nov  1 07:50:52 2025

*/

 



answered Nov 1, 2025 by avibootz
0 votes
#include <iostream>
#include <chrono>
#include <format>
#include <string>
#include <map>
    
using namespace std::chrono; // current_zone / zoned_time / system_clock
    
int main() {

    // Map user choices to IANA time zone names
    std::map<int, std::string> timezones = {
        {1, "America/Anchorage"},     // Alaska
        {2, "America/Guatemala"},     // Central America
        {3, "America/Godthab"},       // Greenland
        {4, "Europe/Paris"},          // Paris
        {5, "Asia/Shanghai"}          // Beijing
    };

    std::cout << "Choose a timezone to convert your local time:\n"
              << "1. Alaska (-9:00 UTC)\n"
              << "2. Central America (-6:00 UTC)\n"
              << "3. Greenland (-3:00 UTC)\n"
              << "4. Paris (+1:00 UTC)\n"
              << "5. Beijing (+8:00 UTC)\n"
              << "Enter your choice (1-5): ";

    int choice;
    std::cin >> choice;

    if (timezones.find(choice) == timezones.end()) {
        std::cout << "Invalid choice.\n";
        return 1;
    }

    // Get current local time
    auto local_zone = current_zone();
    auto local_time = zoned_time{local_zone, system_clock::now()};
    std::cout << "Local time: " << format("{:%F %T %Z}", local_time) << '\n';

    // Convert to selected time zone
    auto target_zone = locate_zone(timezones[choice]);
    auto target_time = zoned_time{target_zone, local_time.get_sys_time()};
    std::cout << "Converted time (" << timezones[choice] << "): "
              << format("{:%F %T %Z}", target_time) << '\n';
}



/*
run:

Choose a timezone to convert your local time:
1. Alaska (-9:00 UTC)
2. Central America (-6:00 UTC)
3. Greenland (-3:00 UTC)
4. Paris (+1:00 UTC)
5. Beijing (+8:00 UTC)
Enter your choice (1-5): 3
Local time: 2025-11-01 08:51:09.259509992 UTC
Converted time (America/Godthab): 2025-11-01 06:51:09.259509992 -02

*/

 



answered Nov 1, 2025 by avibootz

Related questions

...