1

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

SorryEh
  • 900
  • 2
  • 15
  • 47
  • 1
    Look at http://www.boost.org/doc/libs/1_51_0/doc/html/date_time/local_time.html – ForEveR Sep 08 '12 at 16:52
  • 1
    here is some history about time zones and why they were developed. http://geography.about.com/od/physicalgeography/a/timezones.htm and another source is this web site http://www.timeanddate.com/time/time-zones.html and this stackoverflow PHP question http://stackoverflow.com/questions/5584602/determine-timezone-from-latitude-longitude-without-using-web-services-like-geona – Richard Chambers Sep 08 '12 at 17:01

1 Answers1

2

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 man system, e.g. by typing man 3 time, or man 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 since 1970-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 as GMT +1, just add 3600 * <your timezone offset here> to the value returned by time().

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++11 standard, which is mostly supported by the most recent versions of the gcc and Microsoft compilers, also provides a new standard way of getting time: std::chrono. std::chrono supports microsecond accuracy, as opposed to the second accuracy of time().

mic_e
  • 5,594
  • 4
  • 34
  • 49
  • that was an outstanding answer, thanks a lot! this definitely helps me go in the right direction. You all are awesome. – SorryEh Sep 08 '12 at 17:23