Update of the Date library

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Update of the Date library

Post by Mesa »

An Update of the Date library with calculation on dates.


See my post here : (all OS)
http://www.purebasic.fr/english/viewtop ... 12&t=49665

in french
http://www.purebasic.fr/french/viewtopi ... =6&t=12710

Thanx.

Mesa.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Update of the Date library

Post by IdeasVacuum »

+1 :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Update of the Date library

Post by Seymour Clufley »

I think we definitely need a date library that can handle a much greater range than 1970-2038.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
ozzie
Enthusiast
Enthusiast
Posts: 443
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: Update of the Date library

Post by ozzie »

+1. Just got caught out with Date(), trying to use a date in 1944 and finding Date() returned Jan 1 1970 :(
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Update of the Date library

Post by jesperbrannmark »

Oh yes. That would be *awsome*. Please also if this is done include the dategadget update and calendargadget (in Win it cant do before 1970 unless using bad hacks. in mac it does before, but could probably also need some pampering)
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Update of the Date library

Post by c4s »

ozzie wrote:+1. Just got caught out with Date(), trying to use a date in 1944 and finding Date() returned Jan 1 1970 :(
Me too! It's really annoying if you have to work with e.g. birthdays...

I don't think you have to change the existing date library though. Just add a new "DateQ" lib with exactly the same functionality (maybe with some kind of an offset?!) and there wouldn't even be any issue with backward compatibility etc!
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Update of the Date library

Post by Michael Vogel »

I would be happy, if the available range for valid dates would be increased to support 1970 to 2106 or so. This would be possible, if the used long numbers would be used like unsigned values.

Code: Select all

;a.l

a=Date(2038,1,19,3,14,7)
Debug a
Debug FormatDate("%yyyy,%mm,%dd,%hh,%ii,%ss",a)

Debug "· · · · · · · · · · · ·"
a+1
Debug a
Debug FormatDate("%yyyy,%mm,%dd,%hh,%ii,%ss",a)
Ulix
User
User
Posts: 48
Joined: Wed Jan 23, 2008 12:45 pm
Location: France, Montpellier

Re: Update of the Date library

Post by Ulix »

google translation!

+1, +10, +100 ....

Already ask! (and I still cry over and over again) :cry:

Do we not say: hope is alive! :mrgreen:

Ulix
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Update of the Date library

Post by Michael Vogel »

Michael Vogel wrote:

Code: Select all

a=Date(2038,1,19,3,14,7)
Debug a
Debug FormatDate("%yyyy,%mm,%dd,%hh,%ii,%ss",a)
Interestingly, the code above presents a correct result for the (first) date when using PB5.0, but not with 5.11 :shock: So I am not sure, if the following routines will work for all PB versions (I didn't find any problems for now):

Code: Select all

; Define

	; Date Functions - Version 1.o by Michael Vogel
	; ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	; should work for the range 1970/1/1-2099/12/31

	#DaySeconds=86400

	Procedure.l _Seconds(Date.l)

		Date%#DaySeconds
		If Date<0
			ProcedureReturn (109696+Date)%#DaySeconds;	adapt seconds
		Else
			ProcedureReturn Date
		EndIf

	EndProcedure
	Procedure.l _Days(Date.l)

		If Date<0
			Date=(109697+Date)/#DaySeconds;					adapt seconds
			ProcedureReturn Date+49708;						change direction
		Else
			Date/#DaySeconds
			ProcedureReturn Date
		EndIf

	EndProcedure
	Procedure Day_(Date.l)

		If Date<0
			Date-#DaySeconds*24837;	68 years earlier
		EndIf
		ProcedureReturn Day(Date)

	EndProcedure
	Procedure Month_(Date.l)

		If Date<0
			Date-#DaySeconds*24837;	68 years earlier
		EndIf
		ProcedureReturn Month(Date)

	EndProcedure
	Procedure Year_(Date.l)

		If Date<0
			ProcedureReturn Year(Date-#DaySeconds*24837)+68;	68 years earlier
		Else
			ProcedureReturn Year(Date)
		EndIf

	EndProcedure
	Procedure Hour_(Date.l)
		ProcedureReturn Hour(_Seconds(Date))
	EndProcedure
	Procedure Minute_(Date.l)
		ProcedureReturn Minute(_Seconds(Date))
	EndProcedure
	Procedure Second_(Date.l)
		ProcedureReturn Second(_Seconds(Date))
	EndProcedure

	Procedure Date_(Year,Month,Day,Hour,Minute,Second)

		Protected n

		n=Date(Year,Month,Day,Hour,Minute,Second)
		If n<0 And Year>2037 And Year<2100
			n=Date(Year-68,Month,Day,Hour,Minute,Second)-2149050496
		EndIf

		ProcedureReturn n

	EndProcedure
	Procedure.s FormatDate_(Mask$,Date.l)

		Mask$=ReplaceString(Mask$,"%yyyy",RSet(Str(Year_(Date)),4,"0"))
		Mask$=ReplaceString(Mask$,"%mm",RSet(Str(Month_(Date)),2,"0"))
		Mask$=ReplaceString(Mask$,"%dd",RSet(Str(Day_(Date)),2,"0"))
		Mask$=ReplaceString(Mask$,"%hh",RSet(Str(Hour_(Date)),2,"0"))
		Mask$=ReplaceString(Mask$,"%ii",RSet(Str(Minute_(Date)),2,"0"))
		Mask$=ReplaceString(Mask$,"%ss",RSet(Str(Second_(Date)),2,"0"))

		ProcedureReturn Mask$

	EndProcedure

; EndDefine

Procedure DebugDate(a.l)
	
	If 0
		Debug FormatDate_("%yyyy-%mm-%dd  %hh:%ii:%ss",a)+" ~ "+Str(a)+"/x"+Hex(a,#PB_Long)+" ~  day: "+Str(_Days(a))+" ~ sec: "+Str(_Seconds(a))
	Else
		Debug FormatDate_("%yyyy-%mm-%dd  %hh:%ii:%ss",a)
	EndIf

EndProcedure


a.l

a=Date(2038,1,18,23,59,59);			Classic 'Date' function (PB Version 5.11)
DebugDate(a)
a+1
DebugDate(a)

a+86399
DebugDate(a)
a+1
DebugDate(a)

a+86399
DebugDate(a)
a+1
DebugDate(a)

For y=2038 To 2040
	a=Date_(y,2,28,1,2,3);			New 'Date' function...
	Debug FormatDate_("*%yyyy*",a)
	For i=1 To 3
		DebugDate(a)
		a+#DaySeconds
	Next i
Next y
PS: still would like to see something like that within PB :wink:
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Update of the Date library

Post by BorisTheOld »

Many applications require the ability to work with long time spans. We get around PB's restrictive Date library by using the FreeBasic date functions wrapped in a small dynamic library -- "FBdate.so" for Linux, and "FBdate.dll" for Windows.

This is a quick and easy technique that can be used for getting around the problem of PB not having features that exist in other Basic dialects. We do this for file sharing, dates, and various other language features.

We will eliminate these libraries as PB is upgraded to include the missing features.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Post Reply