'===========================================================================
' Subject: Date-Time Formter                  Date: 06-15-01 (19:34)       
'  Author: Vincent Voois                      Code: XBASIC                 
'  Origin: vvacme@worldonline.nl            Packet: XBASIC.ABC
'===========================================================================
'
'
' ####################
' #####  PROLOG  #####
' ####################
'
PROGRAM	"DateandTime"  ' 1-8 char program/file name without .x or any .extent
VERSION	"1.0000"    ' version number - increment before saving altered program
'
' A simple Date and Time formatter for those who miss TIME$ and DATE$ options.
' This little code contains those sort of functions. The Entry() functions displays
' a couple of example lines and explanations.
'
' Vincent Voois
'
'
' ******  Comment libraries in/out as needed  *****
'
'	IMPORT	"xma"   ' Math library     : SIN/ASIN/SINH/ASINH/LOG/EXP/SQRT...
'	IMPORT	"xcm"   ' Complex library  : complex number library  (trig, etc)
	IMPORT	"xst"   ' Standard library : required by most programs
'	IMPORT	"xgr"   ' GraphicsDesigner : required by GuiDesigner programs
'	IMPORT	"xui"   ' GuiDesigner      : required by GuiDesigner programs
'

DECLARE FUNCTION  Entry ()
DECLARE FUNCTION  TIME$ (mode)
DECLARE FUNCTION  DATE$ (format$)
'
'
' ######################
' #####  Entry ()  #####
' ######################
'
' Programs contain:
'   1. A PROLOG with type/function/constant declarations.
'   2. This Entry() function where execution begins.
'   3. Zero or more additional functions.
'
FUNCTION  Entry ()
		' abb			stands for
		'	dn			Day Name		(Sunday, Monday...)
		'	df			Day Figure	(1st, 2nd, 3rd...)
		' mn			Month Name	(July, August, September,...)
		' yy/yyyy	Year				(70, 80, .. / 1970, 1980,....)
		' TIME$ returns a string depending on the mode:12 is 12 hours mode, anything else is 24 hours mode
		
		XstClearConsole()
		PRINT TIME$(0)
		PRINT TIME$(12)
		PRINT DATE$("dn the df, mn, yyyy")
		PRINT DATE$("yy-mm-dd")
		PRINT DATE$("dn, dd/mm/yyyy")
		PRINT "Day one : "; DATE$("dn, df "); TIME$(12); " CET"

END FUNCTION
'
'
' ######################
' #####  TIME$ ()  #####
' ######################
'
FUNCTION  TIME$ (mode)
	XstGetLocalDateAndTime (@year, @month, @day, @weekDay, @hour, @minute, @second, @nsec)

	SELECT CASE mode

		CASE 12

			IF hour > 12 THEN
				hour = hour - 12
			END IF

		CASE 24

		CASE ELSE
			'Default is 24 hours

	END SELECT

	hour$ = TRIM$(STR$(hour))
	minute$ = TRIM$(STR$(minute))
	second$ = TRIM$(STR$(second))

	IF (SIZE(hour$) < 2) AND (mode <> 12) THEN
		hour$ = "0" + hour$
	END IF

	IF SIZE(minute$)< 2 THEN
		minute$ = "0" + minute$
	END IF

	IF SIZE(second$)< 2 THEN
		second$ =  "0" + second$
	END IF

	Temp$ =  hour$ + ":" + minute$ + ":" + second$

	RETURN Temp$

END FUNCTION
'
'
' ######################
' #####  DATE$ ()  #####
' ######################
'
FUNCTION  DATE$ (format$)

	XstGetLocalDateAndTime (@year, @month, @day, @weekDay, @hour, @minute, @second, @nsec)

	year$ = TRIM$(STR$(year))
	month$ = TRIM$(STR$(month))
	day$ = 	TRIM$(STR$(day))

	GOSUB SetDayName
	GOSUB SetDayFigure
	GOSUB SetMonthName

	IF INSTRI(format$, "dd") THEN
		format$ = XstMergeStrings$(format$, day$, INSTRI(format$, "dd"), 2)
	END IF

	IF INSTRI(format$, "mm") THEN
		format$ = XstMergeStrings$(format$, month$, INSTRI(format$, "mm"), 2)
	END IF

	IF INSTRI(format$, "yyyy") THEN
		format$ = XstMergeStrings$(format$, year$, INSTRI(format$, "yyyy"), 4)
	ELSE

		IF INSTRI(format$, "yy") THEN
			format$ = XstMergeStrings$(format$, RIGHT$(year$,2), INSTRI(format$, "yy"), 2)
		END IF

	END IF

	IF INSTRI(format$, "mn") THEN
		format$ = XstMergeStrings$(format$, monthname$, INSTRI(format$, "mn"), 2)
	END IF

	IF INSTRI(format$, "dn") THEN
		format$ = XstMergeStrings$(format$, dayname$, INSTRI(format$, "dn"), 2)
	END IF

	IF INSTRI(format$, "df") THEN
		format$ = XstMergeStrings$(format$, dayfigure$, INSTRI(format$, "df"), 2)
	END IF


	RETURN format$


SUB SetDayFigure

	SELECT CASE RIGHT$(day$,1)

		CASE "1"
			dayfigure$ = day$+ "st"

		CASE "2"
			dayfigure$ = day$+ "nd"

		CASE "3"
			dayfigure$ = day$+ "rd"

		CASE "0", "4", "5", "6", "7", "8", "9"
			dayfigure$ = day$+ "th"

	END SELECT

END SUB

SUB SetDayName

	SELECT CASE weekDay

		CASE 0
			dayname$ = "Sunday"

		CASE 1
			dayname$ = "Monday"

		CASE 2
			dayname$ = "Tuesday"

		CASE 3
			dayname$ = "Wednesday"

		CASE 4
			dayname$ = "Thursday"

		CASE 5
			dayname$ = "Friday"

		CASE 6
			dayname$ = "Saturday"

	END SELECT

END SUB

SUB SetMonthName

	SELECT CASE month

		CASE 1
			monthname$ = "January"

		CASE 2
			monthname$ = "February"

		CASE 3
			monthname$ = "March"

		CASE 4
			monthname$ = "April"

		CASE 5
			monthname$ = "May"

		CASE 6
			monthname$ = "June"

		CASE 7
			monthname$ = "July"

		CASE 8
			monthname$ = "August"

		CASE 9
			monthname$ = "September"

		CASE 10
			monthname$ = "October"

		CASE 11
			monthname$ = "November"

		CASE 12
			monthname$ = "December"

	END SELECT

END SUB

END FUNCTION
END PROGRAM

