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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to abbreviate namespaces in C++

1 Answer

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

// Abbreviation: aliasing a long namespace name.
namespace ch = std::chrono;

int main() {
    // Get current time
    auto now = ch::system_clock::now();

    // Convert to time_t for readable formatting
    std::time_t now_c = ch::system_clock::to_time_t(now);

    std::cout << "Clock ticked!\n";

    // Print human-readable time
    std::cout << "Current time: " << std::ctime(&now_c);

    // Show epoch duration in seconds
    auto since_epoch = now.time_since_epoch();
    auto seconds = ch::duration_cast<ch::seconds>(since_epoch).count();
    std::cout << "Seconds since epoch: " << seconds << "\n";

    // Demonstrate a duration and sleep
    ch::milliseconds pause(1500);
    std::cout << "Sleeping for " << pause.count() << " ms...\n";
    std::this_thread::sleep_for(pause);

    // Show time again after sleep
    auto later = ch::system_clock::now();
    auto diff = later - now;
    auto diff_ms = ch::duration_cast<ch::milliseconds>(diff).count();
    std::cout << "Elapsed after sleep: " << diff_ms << " ms\n";
}



/*
run:

Clock ticked!
Current time: Thu Jul  9 15:02:28 2026
Seconds since epoch: 1783609348
Sleeping for 1500 ms...
Elapsed after sleep: 1501 ms

*/

 



answered Jul 9 by avibootz
...