forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

localtime doesn't use the configured timezone

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Oct 02, 2005 10:08 am    Post subject: localtime doesn't use the configured timezone Reply with quote

The code below demonstrates the problem, if you have confgured a different timezone for your PSP than UTC. "localtime" returns UTC instead of the local time.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <time.h>

/* Define the module info section */
PSP_MODULE_INFO("TEST", 0, 1, 1);

/* Define the main thread's attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

int main(void)
{
   pspDebugScreenInit();

   int c = 0;
   while (1) {
      time_t t = time(NULL);
      struct tm *stm = localtime(&t);
      pspDebugScreenClear();
      pspDebugScreenPrintf("%02i:%02i:%02i", stm->tm_hour, stm->tm_min, stm->tm_sec);
      sceDisplayWaitVblankStart();
      sceDisplayWaitVblankStart();
      if (c++ == 300) break;
   }
   sceKernelExitGame();

   return 0;
}
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Sun Oct 02, 2005 12:32 pm    Post subject: Re: localtime doesn't use the configured timezone Reply with quote

Shine wrote:
The code below demonstrates the problem, if you have confgured a different timezone for your PSP than UTC.


See if something like
Code:
setenv("TZ","EST5EDT");

before localtime helps.
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Oct 02, 2005 8:07 pm    Post subject: Re: localtime doesn't use the configured timezone Reply with quote

jimparis wrote:
See if something like
Code:
setenv("TZ","EST5EDT");

before localtime helps.


This doesn't help, because I'm not living in the EST5 timezone :-)

But in general setting the TZ variable helps. With this function the timezone will be set to the PSP timezone:

Code:

void initTimezone()
{
   int tzOffset = 0;
   sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_TIMEZONE, &tzOffset);
   int tzOffsetAbs = tzOffset < 0 ? -tzOffset : tzOffset;
   int hours = tzOffsetAbs / 60;
   int minutes = tzOffsetAbs - hours * 60;
   static char tz[10];
   sprintf(tz, "GMT%s%02i:%02i", tzOffset < 0 ? "+" : "-", hours, minutes);
   setenv("TZ", tz, 1);
   tzset();
}


I don't know, where is the right position in newlib or PSPSDK, but I think it should be set by default in some startup code.
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Tue Oct 04, 2005 5:38 pm    Post subject: Re: localtime doesn't use the configured timezone Reply with quote

Shine wrote:
This doesn't help, because I'm not living in the EST5 timezone :-)
It was a proof of concept :P Thanks for the init code.

I just committed changes so that the timezone is initialized on startup. As a side-effect, newlib now requires linking with -lpsputility. This is handled automatically if you use build.mak or SDL.
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Sun Oct 30, 2005 5:19 am    Post subject: Reply with quote

I'm going to remove the dependencies in newlib libc on tzset(), setenv(), and libpsputility.a because they include a ton of bloated code in typical newlib fashion. I'm also not too fond of depending on -lpsputility for just one function, and then needing to specify -lpsputility for autoconf programs, etc.

To do this, I'm going to replace tzset() with a version that calls jimparis' original tz init code the first time it's called. This will mean that any programs that need the timezone set must explicitly call tzset(). Note that newlib setenv() will call tzset() when you pass "TZ", so it's best to just call tzset() directly.
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Sun Oct 30, 2005 5:21 am    Post subject: Reply with quote

Also, programs using tzset() will still be required to link against -lpsputility, but the rest of the world will not.
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Sun Oct 30, 2005 6:02 am    Post subject: Reply with quote

Done. Also, I was wrong about setenv(), it only calls tzset() if TZ was previously set. Anyway the code should be correct. It will also make sure TZ isn't set before initializing it from the PSP settings, so that programs can override TZ if necessary.

As an aside, removing this code and replacing printf() and its cousins with a smaller version (an earlier change), now makes newlib libc only slightly larger than PSPSDK's libc for most programs. The remaining differences in size can probably be attributed to the thread-safe code in newlib (which we don't use currently). Just wanted to point out that it's now feasible for *everyone* to use newlib :).
Back to top
View user's profile Send private message
carlosedp



Joined: 19 Jun 2007
Posts: 21
Location: Sao Paulo / Brazil

PostPosted: Fri Oct 19, 2007 7:16 am    Post subject: initTimezone() function improved with DST support. Reply with quote

I have improved the initTimezone function above with support for DST.

I have tested on FW 3.71.


Code:

void initTimezone() {
   int tzOffset = 0;
   int dst = 0;
   sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_TIMEZONE, &tzOffset);
   sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_DAYLIGHTSAVINGS, &dst);
   int tzOffsetAbs = tzOffset < 0 ? -tzOffset : tzOffset;
   int hours = tzOffsetAbs / 60;
   int minutes = tzOffsetAbs - hours * 60;
   char *tz;
   if (dst == 1) {
      tz = malloc(14*sizeof(char));
      sprintf(tz, "GMT%s%02i:%02i DST", tzOffset < 0 ? "+" : "-", hours, minutes);
   } else {
      tz = malloc(10*sizeof(char));
      sprintf(tz, "GMT%s%02i:%02i", tzOffset < 0 ? "+" : "-", hours, minutes);
   }
   
   setenv("TZ", tz, 1);
   tzset();
}


Carlos
The Mindcaster
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Sat Oct 20, 2007 6:09 am    Post subject: Reply with quote

Thanks, if you can provide this as a patch against the pspdev repository I'll add it in.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group