CurrentDate()

BlitzMax Forums/BlitzMax Module Tweaks/CurrentDate()

degac(Posted 2009) [#1]
I was looking into bmax source and I discovered the CurrentDate() implementation
Function CurrentDate$()
	Local	time[256],buff:Byte[256]
	time_(time)
	strftime_(buff,256,"%d %B %Y",localtime_( time ))
	Return String.FromCString(buff)
End Function

I'm not a C expert but I just played around with the function and changed with the current one to offer more 'flexibility' in the date output format (maybe someone has already discovered and suggested this change...)

Function CurrentDate$(_what$="%d %B %Y")
	Local	time[256],buff:Byte[256]
	time_(time)
	strftime_(buff,256,_what,localtime_( time ))
	Return String.FromCString(buff)
End Function

The difference?
Well, you can have different date output - I think it is useful

Try yourself
Print CurrentDate()' --> 02 May 2009
Print CurrentDate("%a %d %m %y")' --> Sat 02 05 09
Print CurrentDate("%x")' --> 05/02/09
Print CurrentDate("%A %d %b %Y")' --> Saturday 02 May 2009
Print CurrentDate("%A,%d-%b-%Y")' --> Saturday,02-May-2009
Print CurrentDate("%j %U")' --> 122nd day of the year 17th week


Of course you can use %H:%M:%S to get the time (plus %p to print out PM/AM)

PS: it should work on every platforms - as I have understand the function CurrentDate() is based on strtime and it seems to be a 'standard' on every OS.

edit: just tested on my Ubuntu machine - same results as Windows with the example I tested.


Htbaa(Posted 2009) [#2]
That would be useful indeed. I vote for yes to change this.


N(Posted 2009) [#3]
Seconding this, since it's a nice change that doesn't break old code.


plash(Posted 2009) [#4]
Thirding this. I already did this change locally because I needed more options.

EDIT: I noticed that CurrentTime() does the same as CurrentDate() (with different % values, obviously). Should CurrentTime and CurrentDate be replaced by a single function to just wrap the strftime_ function?


xlsior(Posted 2009) [#5]
Fourthing this.


GfK(Posted 2009) [#6]
Yeah, seems useful.


Otus(Posted 2009) [#7]
Looks very useful indeed. As long as everything is backwards compatible this should be included in a future release.


plash(Posted 2009) [#8]
Rem
	bbdoc: Get the FileTime of the given file in the given format.
	returns: The formatted filetime.
End Rem
Function FileTimeWithFormat:String(path:String, format:String)
	Local time:Int Ptr, buff:Byte[256], ftime:Int
	
	ftime = FileTime(path)
	time = Varptr(ftime)
	strftime_(buff, 256, format, localtime_(time))
	
	Return String.FromCString(buff)
	
End Function


:)


SebHoll(Posted 2009) [#9]
Bump. What does Mr. Sibly think?


plash(Posted 2009) [#10]
The lack of notice for this thread (and others like it) makes kittens cry :(


ima747(Posted 2010) [#11]
would love to see this go official as well, here's a superstrict variant for implementation along side the current one if you don't want to change your build (I always forget to make changes on all my systems so I like to have them built into my code until they become official)

Function CurrentDateTime:String(_what:String="%d %B %Y")
	Local	time:Byte[256],buff:Byte[256]
	time_(time)
	strftime_(buff,256,_what,localtime_( time ))
	Return String.FromCString(buff)
End Function



matibee(Posted 2010) [#12]
I've tried ripping out all the pertinent bits to come up with small module but it doesn't work. The strftimeA_ call gives an access violation but without a low level debugger I can't see why. Any clues?

The time() function works a treat and is all I wanted anyway, I don't suppose there was an existing way of getting that value was there?

SuperStrict
Import "date.c"

Module matibee.date

Extern 
	Function timeA_:Int ( ttime:Byte Ptr )
	Function localtimeA_:Byte Ptr ( ttime:Byte Ptr )
	Function strftimeA_:Int ( buf:Byte Ptr, size:Int, fmt:Byte Ptr, ttime:Byte Ptr  )
End Extern 

Function Date$(_what$="%d %B %Y")
	Local time:Byte[256],buff:Byte[256]
	timeA_(time)
	strftimeA_(buff,256,_what,localtimeA_( time ))
	Return String.FromCString(buff)
End Function

Function time:Int()
	Return timeA_( Null )
End Function 


#include <../../brl.Mod/blitz.Mod/blitz.h>
#include <time.h>

int strftimeA_( char *buf,int size,BBString *fmt,void *ttime ){
	return strftime( buf,size,bbTmpCString(fmt),ttime );
}

int timeA_( void *ttime ){
	return time( (time_t*)ttime );
}

void *localtimeA_( void *ttime ){
	return localtime( (time_t*)ttime );
}