I want to start creating programs on my own, however my c/c++ skills are still very basic.
I’m really interested in creating a program that tells you your time according to the time zone you enter
for example:
Please enter time zone:
eastern time
Your time zone is GMT -5:00
Your current time is 12:19 pm
Would you like to try another?(y/n)
something along those lines. Eventually I want to be able use this knowledge and apply it to creating some basic free throw around apps that you mess around with and look nice. And ultimately I want to be able to apply this (and of course many other important features) to when i start developing video games, or other software,
in this case save files they have time stamps most likely applied via the machine it’s using but how does it access that?.
ive looked into various functions found on cplusplus library and i’ve come across some functions
“localtime”
“gmtime”
and “time” (for getting current time etc)
but I want to know how these functions are made, the process, the formulas they used. How do i even go about thinking to create something like that.
I even looked up how time zones are calculated and i got “Each line of longitude has a 15 degree difference and every 15 degree is 1 hour different” – Wiki Answers.
this is just some pondering I’ve had for a couple days.
So I plan to do this using C++ and primarily windows based machines (not sure if it makes a difference whether it’s OSX or Unix/Linux or Windows)
All help is greatly appreciated,
thanks!
Umeed
You will want to take a look at the official documentation for these functions. In UNIX environments (such as Cygwin on Windows), which I’d heavily recommend for development, you can get the documentation of any C standard function via the
mansystem, e.g. by typingman 3 time, orman gmtime. On Windows, at least as far as I know, there is no such documentation system, you’ll just have to google the documentation to the function.The
time()method is defined in the header file<ctime>(or, for pure C,<time.h>). It will return the number of seconds that have passed since1970-01-01 00:00 UTC (GMT +0). This is also known as the UNIX time. It is a pretty simple task to calculate the actual date (year, month, day, hour, minute, second) from that value (you’ll just have to consider leap years, etc.). If you wish to know the time for a different timezone, such asGMT +1, just add3600 * <your timezone offset here>to the value returned bytime().The method
time()is internally implemented in your C standard library, or directly as a system call in your Operating System’s Kernel, which in turn calculates the time from your motherboard’s Real-Time-Clock, which is powered by the CMOS battery to keep track of time even when your PC is switched off and disconnected from it’s power supply.You’ll also want to make sure that you always use standard C methods, instead of Windows-Only methods, to ensure portability of your code.
edit
The
C++11standard, which is mostly supported by the most recent versions of thegccand Microsoft compilers, also provides a new standard way of getting time:std::chrono.std::chronosupports microsecond accuracy, as opposed to the second accuracy oftime().