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 doesnt return the correct date/month/year

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



Joined: 04 Mar 2006
Posts: 46

PostPosted: Mon Nov 12, 2007 1:49 am    Post subject: localtime doesnt return the correct date/month/year Reply with quote

This is just something I noticed recently, every time I try to call date/month/year, it will always return January 1 1970 (when unix time started) using the latest toolchain.

Does anyone else experience this problem?
Back to top
View user's profile Send private message
gambiting



Joined: 17 Aug 2006
Posts: 154

PostPosted: Mon Nov 12, 2007 3:32 am    Post subject: Reply with quote

Try time() - it will return you number of seconds since that date.
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Mon Nov 12, 2007 5:37 am    Post subject: Re: localtime doesnt return the correct date/month/year Reply with quote

cools wrote:
This is just something I noticed recently, every time I try to call date/month/year, it will always return January 1 1970 (when unix time started) using the latest toolchain.

What exactly are you trying to call?
If you call localtime, you need to pass it a pointer to a correct time_t.
Back to top
View user's profile Send private message
cools



Joined: 04 Mar 2006
Posts: 46

PostPosted: Mon Nov 12, 2007 8:15 am    Post subject: Reply with quote

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_year+1900, stm->tm_mon+1, stm->tm_mday);
      sceDisplayWaitVblankStart();
      sceDisplayWaitVblankStart();
      if (c++ == 300) break;
   }
   sceKernelExitGame();

   return 0;
}


^oh and thats pretty much shines code, except 1 line...

It will return 1970:01:01
Back to top
View user's profile Send private message
carlosedp



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

PostPosted: Tue Nov 13, 2007 7:27 am    Post subject: Reply with quote

Hi cools,

I got this problem too. Fixed it replacing the following:


time() -> sceKernelLibcTime() like:

Code:

    time_t now;
    sceKernelLibcTime(&now);
    printf("Time is: %s\n", ctime(&now));


gettimeofday() -> sceKernelLibcGettimeofday():

gettimeofday have microseconds resolution but its seconds are from 0:00 so you can grab the seconds since epoch with sceKernelLibcTime() and add to the usec from the structure like this:
Code:

double gettime() {
    time_t now;
    struct timeval t;
    double result;

    sceKernelLibcTime(&now);
    sceKernelLibcGettimeofday(&t, NULL)
    res = (double)now;
    res += t.tv_usec*0.000001;

    return res
}

I have also used the initTimezone function above... i got it from another post here in the forum and fixed the timezone with DST. I call it from my main() just one time....

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;
    int pspDaylight = 0;
    sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_DAYLIGHTSAVINGS, &pspDaylight);
    static char tz[18];
    sprintf(tz, "GMT%s%02i:%02i%s", tzOffset < 0 ? "+" : "-", hours, minutes, pspDaylight ? " DST" : "");
    setenv("TZ", tz, 1);
    tzset();
}



[/code]
Back to top
View user's profile Send private message
carlosedp



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

PostPosted: Wed Nov 14, 2007 3:15 am    Post subject: Reply with quote

One thing I noticed that is very weird is that the file newlib-1.15.0/newlib/libc/sys/psp/libcglue.c already does this mapping from the original C call to the PSP call... like this:

Code:

/* Time routines.  These wrap around the routines provided by the kernel. */
#ifdef F__gettimeofday
int _gettimeofday(struct timeval *tp, struct timezone *tzp)
{
    return __psp_set_errno(sceKernelLibcGettimeofday(tp, tzp));
}

#endif



Or this:

Code:

#if defined(F_time)
time_t time(time_t *t)
{
    return __psp_set_errno(sceKernelLibcTime(t));
}
#endif



Why it is not called when I use the clock or similar functions???? It looks like the library itself would wrap this for me.... It happens also on tzset()... it appears on libcglue.c and should do all that I do in the function I posted above but this doesnt happens.

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



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Wed Nov 14, 2007 5:14 am    Post subject: Reply with quote

Yeah, it should just work. Are you sure you're linking against newlib?
Back to top
View user's profile Send private message
carlosedp



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

PostPosted: Wed Nov 14, 2007 5:30 am    Post subject: Reply with quote

Well,

I think I am.... tell me if is there anything wrong...

Here is my makefile:

Code:

TARGET = StacklessPSP
BUILD_PRX = 1
PSP_FW_VERSION = 371
RELEASE_NAME = StacklessPSP
RELEASE_VERSION = 2.5.1
RELEASE_DIRECTORY = ~/$(RELEASE_NAME)-$(RELEASE_VERSION)

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Stackless PyPSP 2.5.1
PSP_EBOOT_ICON = icon_slp251.png
#PSP_EBOOT_UNKPNG = pic0.png
#PSP_EBOOT_PIC1 = pic1.png
#PSP_EBOOT_SND0 = snd0.at3

MODULES_STATIC = \
   Modules/config.o
        ........

OBJECTS_PYTHON = \
    ../Python/ast.o \
        ........
 
OBJECTS_OBJECTS = \
   ../Objects/typeobject.o \
        ........

OBJECTS_PARSER = \
   ../Parser/tokenizer.o \
           ........


OBJS_STACKLESS = ../Stackless/core/cframeobject.o \
   ../Stackless/core/slp_transfer.o \
           ........

OBJS = main.o psperror.o support.o \
   $(MODULES_STATIC) $(OBJECTS_PYTHON) $(OBJECTS_OBJECTS) $(OBJECTS_PARSER) $(OBJS_STACKLESS)

CPPFLAGS=-I../../cpplibs
LDFLAGS=-L../../cpplibs
LIBS=

ifeq ($(WITH_SQLITE),1)
OBJS += $(OBJS_SQLITE)
CPPFLAGS += -I../Modules/_sqlite -DMODULE_NAME=\"sqlite3\" -DWITH_SQLITE
LIBS += -lsqlite3
endif

ifeq ($(WITH_PSP2D),1)
OBJS += $(OBJS_PSP2D)
LDFAGS += -Llibpsp2d
LIBS += -lpsp2d -ljpeg -lpng -lz -lpspgu
CPPFLAGS += -DWITH_PSP2D
endif

ifeq ($(WITH_PSPSND),1)
OBJS += $(OBJS_PSPSND)
LDFLAGS += -Llibpspsnd
LIBS += -lpspsnd -lmikmod -lmmio -lpspaudiolib -lpspaudio
CPPFLAGS += -DWITH_PSPSND
endif

ifeq ($(WITH_PSPNET),1)
OBJS += $(OBJS_PSPNET)
CPPFLAGS += -DWITH_PSPNET
LIBS += -lpspwlan
endif

ifeq ($(WITH_PSPOGG),1)
OBJS += $(OBJS_PSPOGG)
LIBS += -lvorbisidec -lpspaudiolib -lpspaudio
CPPFLAGS += -DWITH_PSPOGG
endif

ifeq ($(WITH_PSPMP3),1)
OBJS += $(OBJS_PSPMP3)
LIBS += -lmad -lpspaudiolib -lpspaudio
CPPFLAGS += -DWITH_PSPMP3
endif

LIBS += -lpsppower -lstdc++ -lbz2 -lssl -lcrypto

CPPFLAGS += -DHAVE_CONFIG_H -DPSP -I../Include -I../Stackless
CPPFLAGS += -I../Modules/expat -DHAVE_MEMMOVE

LIBS += -lc -lm

CFLAGS = -Os -G0 -Wall -Wno-strict-aliasing
CXXFLAGS = $(CFLAGS) -fno-rtti -fno-strict-aliasing
ASFLAGS = $(CFLAGS)

LIBDIR =

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak

psperror.c:
   python generr.py
   
Back to top
View user's profile Send private message
carlosedp



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

PostPosted: Fri Nov 30, 2007 3:27 am    Post subject: Reply with quote

Guys,

No clue on why the time functions doesnt works... am I linking to the correct libraries?

Thanks
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Fri Nov 30, 2007 7:53 am    Post subject: Reply with quote

Looks like it, but as you noticed newlib already has all of the right code in there, so maybe there's something wrong with your build setup? Try a simpler test program maybe.
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Mon Jul 21, 2008 3:15 am    Post subject: Reply with quote

I looked at this today as I need some of the newlib time functions...

It seems all the time functions from libcglue aren't being used, instead the functions from newlib-1.15.0/newlib/libc/time are used instead.

As a hack test I edited the newlib-1.15.0/newlib/libc/time/time.c file and removed the:

Code:
time_t
_DEFUN (time, (t),
   time_t * t)
{


function entirely.

Re-compiled newlib and the time() function in libcglue worked perfectly.

I know virtually nothing on newlib or how to fix this issue. Maybe someone else could take a look at it?
Back to top
View user's profile Send private message
darkness



Joined: 15 Jun 2008
Posts: 121

PostPosted: Mon Jul 21, 2008 3:29 am    Post subject: Reply with quote

Why don't use sceRtcGetCurrentClockLocalTime funct?
I think is better!
Code:

pspTime time;
sceRtcGetCurrentClockLocalTime(&time);
printf("%2.2d/%2.2d   %2.2d:%2.2d", time.day, time.month, time.hour, time.minutes);
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Mon Jul 21, 2008 3:35 am    Post subject: Reply with quote

Insert_witty_name wrote:
I looked at this today as I need some of the newlib time functions...

It seems all the time functions from libcglue aren't being used, instead the functions from newlib-1.15.0/newlib/libc/time are used instead.

As a hack test I edited the newlib-1.15.0/newlib/libc/time/time.c file and removed the:

Code:
time_t
_DEFUN (time, (t),
   time_t * t)
{


function entirely.

Re-compiled newlib and the time() function in libcglue worked perfectly.

I know virtually nothing on newlib or how to fix this issue. Maybe someone else could take a look at it?


I ran into this with B2 - I wound up just inserting InitTimeZone() into my main and using sceKernelLibcTime(&rawtime) to get the time. Hopefully at some point, this will make it into newlib.
Back to top
View user's profile Send private message AIM Address
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Mon Jul 21, 2008 4:21 am    Post subject: Reply with quote

It's easy to use sceKernelLibcTime() in place of time().

The point is that it doesn't work in newlib correctly so needs fixing :)
Back to top
View user's profile Send private message
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Mon Jul 21, 2008 5:20 am    Post subject: Reply with quote

If I may say so.... libc and it's implementations (namely newlib) are all so clobbered with function redirections and functionname dissembling that it's just a freaking PITA to work with it or do any changes/fixes....
who the hell decided that the code must be so unreadable that only the ones that wrote it can actually understand it? Shoot that idiot!
</rant>
_________________
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
Back to top
View user's profile Send private message Visit poster's website
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Mon Jul 21, 2008 5:39 am    Post subject: Reply with quote

Raphael wrote:
If I may say so.... libc and it's implementations (namely newlib) are all so clobbered with function redirections and functionname dissembling that it's just a freaking PITA to work with it or do any changes/fixes....
who the hell decided that the code must be so unreadable that only the ones that wrote it can actually understand it? Shoot that idiot!
</rant>


QFT. :D

I've settled for minor changes to code I KNOW gets included - like the change for a negative heap size.
Back to top
View user's profile Send private message AIM Address
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Mon Jul 21, 2008 6:47 am    Post subject: Reply with quote

Ok I've committed a change to the newlib patch.

The problem was that gettimeofday() internally called sceKernelLibcGettimeofday() which was assumed to be the sce version of gettimeofday().

sceKernelLibcGettimeofday() actually returns sec and usec since 00:00 on that day only, not since the 1970 epoch.

I have changed gettimeofday() to use the values from sceKernelLibcTime(), and gettimeofday(), time() and tz_set() now seem to be working correctly.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Mon Jul 21, 2008 7:31 am    Post subject: Reply with quote

Insert_witty_name wrote:
Ok I've committed a change to the newlib patch.

The problem was that gettimeofday() internally called sceKernelLibcGettimeofday() which was assumed to be the sce version of gettimeofday().

sceKernelLibcGettimeofday() actually returns sec and usec since 00:00 on that day only, not since the 1970 epoch.

I have changed gettimeofday() to use the values from sceKernelLibcTime(), and gettimeofday(), time() and tz_set() now seem to be working correctly.


Cool. I'll have to update and change B2 again. :)
Back to top
View user's profile Send private message AIM Address
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Mon Jul 21, 2008 8:47 am    Post subject: Reply with quote

@IWN, when did you update it?
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Mon Jul 21, 2008 8:48 am    Post subject: Reply with quote

Exactly two hours before you posted.
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Mon Jul 21, 2008 8:58 am    Post subject: Reply with quote

OK, I need to update my ubuntu then. thanks :)
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Tue Jul 22, 2008 4:09 pm    Post subject: Reply with quote

I made a fix to IWN's fix. gettimeofday() still needs to return microseconds, so now gettimeofday() will use sceKernelLibcGettimeofday() to get usecs and sceKernelLibcTime() to get epoch seconds, with some extra protection to avoid glitches if the usecs wrap around between calls. It should be correct now.
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