C++ Time Functions

vbimport

#1

I’ve been having trouble getting my text based program to display the time correctly.

I want it to display in the format

Www Mmm dd hh:mm:ss yyyy

I can do so using this code here:

  
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current date and time are: %s", asctime (timeinfo) );

Here is an example of that output

Current date and time are: Sat May 20 15:21:51 2000

That’s exaclty how I want it to display, but the problem is the hour is wrong. I am in Mountain Time or -7:00GMT. So the clock is displayed 7 hours fast.
Now I can get it to work using gmtime instead of localtime, but then my day of the week, and month are now numeric. And if the tm_hour+(MST) = a negative number, it doesn’t convert it (for example -1 does not become 23). For example:

  
  #define MST (-7)
  time_t rawtime;
  tm * ptm;

  time ( &rawtime );

  ptm = gmtime ( &rawtime );

  printf ("MST Is: %2d %02d %02d %2d:%02d:%02d %0002d
", ptm->tm_wday, ptm->tm_mon, tm_hour+MST, ptm->tm_min, ptm->tm_sec, ptm->tm_year+1900);

Returns:

 MST Is: 3 09 07 -01:15:13 2005

If anyone could shed some light on my problem I would greatly appreciate it. I am also just starting out in C++ and have been googling but couldn’t find the answer. Some code borrowed from here and here.


#2

If you are running Windows, is your system tray clock set up to use a internet time server? This might cause the internal time to be not what you expect. Windows adjust this for you based on your time zone selection.

Just a thought.


#3

Actually, it’s being compiled for my psp :iagree: The psp shows the correct time (timezone adjusted) on the main menu or ingame by pressing the home button.
I just can’t figure out a way to print Wed September and be adjusted for my time zone, it seems I can have 1 or the other.


#4

There is a function _tzset() not part of ANSI (M$ extension) that should do the job for you and that is setting some global variables in C runtime according to the TZ environment variable which you should set before running the program.

However I consider that solution rather clunky and awkward so if you are targeting Windows OS only you may consider using native API:



#include <stdio.h>
#include <windows.h>

int main(int argc, char *argv[])
{
	SYSTEMTIME 	st;
	LCID		lc = LOCALE_SYSTEM_DEFAULT;	// or LOCALE_USER_DEFAULT
							// or MAKELCID(MAKELANGID(p, s), SORT_DEFAULT)
							// where p is primary language identifier
							// and s is sublanguage identifier (check MSDN)
	LPTSTR		date, time;
	int		date_len, time_len;

	date_len = GetDateFormat(lc, DATE_LONGDATE, NULL, NULL, NULL, 0);
	time_len = GetTimeFormat(lc, LOCALE_NOUSEROVERRIDE, NULL, NULL, NULL, 0);

	date = (LPTSTR)malloc(date_len);
	time = (LPTSTR)malloc(time_len);

	if ((date == NULL) || (time == NULL)) {
		printf("Error allocating string buffers
");
		goto bail_out;
	}

	GetDateFormat(lc, DATE_LONGDATE, NULL, NULL, date, date_len);
	GetTimeFormat(lc, LOCALE_NOUSEROVERRIDE, NULL, NULL, time, time_len);

	printf("Current date and time is : %s %s
", date, time);

bail_out:
	free(date);	// if date or time is NULL free() just returns
	free(time);

	return 0;
}

Hope this helps.


#5

Welcome, and thanks for your input! However, the problem is my target is not Windows, but my psp. Is there a way to do this without including windows.h?? Or rather, is there a header file I can download and include to be able to use _tzset()?


#6

PSP stands for Playstation Portable right? I am not familiar with that, what toolchain do you use for programming?


#7

Yes, psp=Play Station Portable.
I’ve got my environment set up with cygwin. I then got the psp toolchain from here(scroll to bottom) and installed it. The toolchain and pspsdk are written from the guys at ps2dev.org.

It seems to use most of the normal functions of C++ but there are some difference when calling functions (such as using the buttons because of the lack of keyboard etc.) and they usually go something like sceKernelSleepThread() or pspDebugScreenClear() etc.

Maybe my question would be better suited towards them.


#8

I guess you are right, better ask them or even better, check which CRT lib is being used and then look for the right approach.


#9

Ok thank you very much for your help.