localtime doesnt return the correct date/month/year
localtime doesnt return the correct date/month/year
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?
Does anyone else experience this problem?
Re: localtime doesnt return the correct date/month/year
What exactly are you trying to call?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.
If you call localtime, you need to pass it a pointer to a correct time_t.
Code: Select all
#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;
}
It will return 1970:01:01
Hi cools,
I got this problem too. Fixed it replacing the following:
time() -> sceKernelLibcTime() like:
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:
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]
I got this problem too. Fixed it replacing the following:
time() -> sceKernelLibcTime() like:
Code: Select all
time_t now;
sceKernelLibcTime(&now);
printf("Time is: %s\n", ctime(&now));
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: Select all
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
}
Code: Select all
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();
}
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:
Or this:
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
Code: Select all
/* 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
Code: Select all
#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
Well,
I think I am.... tell me if is there anything wrong...
Here is my makefile:
I think I am.... tell me if is there anything wrong...
Here is my makefile:
Code: Select all
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
-
- Posts: 376
- Joined: Wed May 10, 2006 11:31 pm
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:
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?
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: Select all
time_t
_DEFUN (time, (t),
time_t * t)
{
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?
Why don't use sceRtcGetCurrentClockLocalTime funct?
I think is better!
I think is better!
Code: Select all
pspTime time;
sceRtcGetCurrentClockLocalTime(&time);
printf("%2.2d/%2.2d %2.2d:%2.2d", time.day, time.month, time.hour, time.minutes);
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.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:
function entirely.Code: Select all
time_t _DEFUN (time, (t), time_t * t) {
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?
-
- Posts: 376
- Joined: Wed May 10, 2006 11:31 pm
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>
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
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki
Alexander Berl
QFT. :DRaphael 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>
I've settled for minor changes to code I KNOW gets included - like the change for a negative heap size.
-
- Posts: 376
- Joined: Wed May 10, 2006 11:31 pm
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.
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. :)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.
-
- Posts: 409
- Joined: Tue Oct 09, 2007 4:22 am
-
- Posts: 376
- Joined: Wed May 10, 2006 11:31 pm
-
- Posts: 409
- Joined: Tue Oct 09, 2007 4:22 am
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.