Chris, the Young One (hedgee) wrote,
Chris, the Young One
hedgee

  • Mood:

Celebrating the billennium

The time has come. Finally, today, time_t will reach one billion. And there will be celebration indeed, among Unix fans everywhere!

Here's a countdown program in C++ under Unix, just to be 1337. :-) Not tested yet, sadly, as I don't have a standard-conforming C++ implementation on my machine yet. (Compiling gcc 3.0.1 now, but I don't think it'll complete in time.)


#include <ctime>
#include <iostream>
#include <locale>
#include <unistd.h>

/*
This bit is based on Appendix D of ``The C++ Programming Language'',
special edition, by Bjarne Stroustrup.
*/
template<typename Ch, typename Tr>
std::basic_ostream<Ch, Tr>&
operator<<(std::basic_ostream<Ch, Tr>& s, std::tm const& t)
{
        std::basic_ostream<Ch, Tr>::sentry guard(s);
        if (!guard)
                return s;
        try {
                if (std::use_facet<std::time_put<char> >(s.getloc()).put(s,
                    s, s.fill(), &t, 'X').failed())
                        s.setstate(std::ios_base::failbit);
        } catch (...) {
                try {
                        s.setstate(std::ios_base::badbit);
                } catch (...) {}
                if (s.exceptions() & std::ios_base::badbit)
                        throw;
        }
        return s;
}

int
main(int, char**)
{
        while (!sleep(1)) {
                std::time_t count = 1000000000 - std::time(0);
                std::cout << '\r' << *std::gmtime(&count) << std::flush;
        }
        return 0;
}
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 1 comment