Go Back   Club CDFreaks - Knowledge is Power > International Chat: Software related > General Software


Commercial message



General Software Discuss, C++ Time Functions at International Chat: Software related forum; I've been having trouble getting my text based program to display the time correctly. I want it to display in the format Code:


Reply
 
Thread Tools
Old 07-09-2005   #1 (permalink)
CDFreaks Resident
 
kwkard's Avatar
 
Join Date: Apr 2002
Location: int main() {
Posts: 1,720
C++ Time Functions

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

I want it to display in the format
Code:
Www Mmm dd hh:mm:ss yyyy
I can do so using this code here:
Code:
  
  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
Code:
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:
Code:
  
  #define MST (-7)
  time_t rawtime;
  tm * ptm;

  time ( &rawtime );

  ptm = gmtime ( &rawtime );

  printf ("MST Is: %2d %02d %02d %2d:%02d:%02d %0002d\n", ptm->tm_wday, ptm->tm_mon, tm_hour+MST, ptm->tm_min, ptm->tm_sec, ptm->tm_year+1900);
Returns:
Code:
 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.
__________________
I smart, learn fast.
The Coolest Page Ever

My drives:
Benq 1640 BSLB
Benq 1625 BBHA (Rebadge HP 640c)(Lightscribe)
LG GSA-4163B A104
NEC ND-3520A 1.04
NEC ND-3500AG LD 2.FD
Liteon SOHW-812s@832s VS0E
Liteon SOHW-1633S BS41
Liteon SOHC 5232K NK0H
Yamaha CRW-F1 1.0g

And yes these are all in one PC. Hehe.
kwkard is offline   Reply With Quote
Old 07-09-2005   #2 (permalink)
CD Freaks Expert
 
RichMan's Avatar
 
Join Date: Jan 2004
Posts: 592
Re: C++ Time Functions

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.
RichMan is offline   Reply With Quote
Old 07-09-2005   #3 (permalink)
CDFreaks Resident
 
kwkard's Avatar
 
Join Date: Apr 2002
Location: int main() {
Posts: 1,720
Re: C++ Time Functions

Actually, it's being compiled for my psp 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.
__________________
I smart, learn fast.
The Coolest Page Ever

My drives:
Benq 1640 BSLB
Benq 1625 BBHA (Rebadge HP 640c)(Lightscribe)
LG GSA-4163B A104
NEC ND-3520A 1.04
NEC ND-3500AG LD 2.FD
Liteon SOHW-812s@832s VS0E
Liteon SOHW-1633S BS41
Liteon SOHC 5232K NK0H
Yamaha CRW-F1 1.0g

And yes these are all in one PC. Hehe.
kwkard is offline   Reply With Quote
Old 07-09-2005   #4 (permalink)
New on Forum
 
Join Date: Sep 2005
Posts: 18
Re: C++ Time Functions

Quote:
Originally Posted by MSDN
If the TZ variable is not set, localtime attempts to use the time zone information specified in the Date/Time application in Control Panel. If this information cannot be obtained, PST8PDT, which signifies the Pacific time zone, is used by default.
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:
Code:
#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\n");
		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\n", date, time);

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

	return 0;
}
Hope this helps.
levicki is offline   Reply With Quote
Old 07-09-2005   #5 (permalink)
CDFreaks Resident
 
kwkard's Avatar
 
Join Date: Apr 2002
Location: int main() {
Posts: 1,720
Re: C++ Time Functions

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()?
__________________
I smart, learn fast.
The Coolest Page Ever

My drives:
Benq 1640 BSLB
Benq 1625 BBHA (Rebadge HP 640c)(Lightscribe)
LG GSA-4163B A104
NEC ND-3520A 1.04
NEC ND-3500AG LD 2.FD
Liteon SOHW-812s@832s VS0E
Liteon SOHW-1633S BS41
Liteon SOHC 5232K NK0H
Yamaha CRW-F1 1.0g

And yes these are all in one PC. Hehe.
kwkard is offline   Reply With Quote
Old 07-09-2005   #6 (permalink)
New on Forum
 
Join Date: Sep 2005
Posts: 18
Re: C++ Time Functions

PSP stands for Playstation Portable right? I am not familiar with that, what toolchain do you use for programming?
levicki is offline   Reply With Quote
Old 07-09-2005   #7 (permalink)
CDFreaks Resident
 
kwkard's Avatar
 
Join Date: Apr 2002
Location: int main() {
Posts: 1,720
Re: C++ Time Functions

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.
__________________
I smart, learn fast.
The Coolest Page Ever

My drives:
Benq 1640 BSLB
Benq 1625 BBHA (Rebadge HP 640c)(Lightscribe)
LG GSA-4163B A104
NEC ND-3520A 1.04
NEC ND-3500AG LD 2.FD
Liteon SOHW-812s@832s VS0E
Liteon SOHW-1633S BS41
Liteon SOHC 5232K NK0H
Yamaha CRW-F1 1.0g

And yes these are all in one PC. Hehe.
kwkard is offline   Reply With Quote
Old 07-09-2005   #8 (permalink)
New on Forum
 
Join Date: Sep 2005
Posts: 18
Re: C++ Time Functions

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.
levicki is offline   Reply With Quote
Old 07-09-2005   #9 (permalink)
CDFreaks Resident
 
kwkard's Avatar
 
Join Date: Apr 2002
Location: int main() {
Posts: 1,720
Re: C++ Time Functions

Ok thank you very much for your help.
__________________
I smart, learn fast.
The Coolest Page Ever

My drives:
Benq 1640 BSLB
Benq 1625 BBHA (Rebadge HP 640c)(Lightscribe)
LG GSA-4163B A104
NEC ND-3520A 1.04
NEC ND-3500AG LD 2.FD
Liteon SOHW-812s@832s VS0E
Liteon SOHW-1633S BS41
Liteon SOHC 5232K NK0H
Yamaha CRW-F1 1.0g

And yes these are all in one PC. Hehe.
kwkard is offline   Reply With Quote
 
Reply


If you can't find where you are looking for, then become a member and get an answer fast! We have thousands of people online every moment of the day to help you! Click here



Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Integration functions bigdude AnyDVD 1 07-11-2007 14:23
No Remote Functions with VCD's ??? dexterdoo General Hardware Forum 1 22-03-2007 11:20
CloneDVD2 no longer functions winpitt CloneDVD 2 13-12-2006 07:12
Supported Functions - not DVD-R Willjt Firmware 1 01-10-2005 08:59


All times are GMT +2. The time now is 07:31.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0