Page 1 of 1

FormatDateEx()

Posted: Sun Apr 08, 2012 10:49 am
by Little John
Works also with PB 5.20

Hi all,

here is some code which extends the possibilities of PB's FormatDate() function.
For details, please see the comments at the beginning of the function FormatDateEx().
The functions LocalizedDayName() and LocalizedMonthName() for Windows are slightly modified after this message. The functions for Linux and Mac OS are from here. Many thanks!
The code works in ASCII mode and in Unicode mode.

//edit 2016-06-21:
Added code for Linux and Mac OS.

Code: Select all

; tested with PB 5.42

EnableExplicit


CompilerIf #PB_Compiler_OS = #PB_OS_Windows
   ; tested on German Windows 10
   
   Procedure.s LocalizedDayName (DayOfWeek.i, short.i=#False)
      ; in : DayOfWeek: Sunday=0, ..., Saturday=6
      ;                 (compliant with PureBasic's DayOfWeek() function)
      ;      short    : #True/#False
      ; out: short or full localized name of given weekday
      Protected fmt.i, buffer$, bufferSize.i=80
      
      If DayOfWeek = 0
         DayOfWeek = 7
      EndIf
      
      If short
         fmt = #LOCALE_SABBREVDAYNAME1
      Else
         fmt = #LOCALE_SDAYNAME1
      EndIf
      
      buffer$ = Space(bufferSize)
      GetLocaleInfo_(#LOCALE_USER_DEFAULT, fmt + DayOfWeek - 1, @buffer$, bufferSize)
      
      ProcedureReturn buffer$
   EndProcedure
   
   Procedure.s LocalizedMonthName (MonthOfYear.i, short.i=#False)
      ; in : MonthOfYear: January=1, ..., December=12
      ;                   (compliant with PureBasic's Month() function)
      ;      short      : #True/#False
      ; out: short or full localized name of given month
      Protected fmt.i, buffer$, bufferSize.i=80
      
      If short
         fmt = #LOCALE_SABBREVMONTHNAME1
      Else
         fmt = #LOCALE_SMONTHNAME1
      EndIf
      
      buffer$ = Space(bufferSize)
      GetLocaleInfo_(#LOCALE_USER_DEFAULT, fmt + MonthOfYear - 1, @buffer$, bufferSize)
      
      ProcedureReturn buffer$
   EndProcedure
   
CompilerElse
   ; tested on German Linux Mint 17.3
   
   Structure tm
      tm_sec.l
      tm_min.l
      tm_hour.l
      tm_mday.l
      tm_mon.l
      tm_year.l
      tm_wday.l
      tm_yday.l
      tm_isdst.l
   EndStructure
   
   Procedure.s LocalizedDayName (DayOfWeek.i, short.i=#False)
      ; in : DayOfWeek: Sunday=0, ..., Saturday=6
      ;                 (compliant with PureBasic's DayOfWeek() function)
      ;      short    : #True/#False
      ; out: short or full localized name of given weekday
      Protected tm.tm, fmt.i, numBytes.i, buffer$, bufferSize.i=80
      
      If short
         fmt = $6125      ; "%a"
      Else
         fmt = $4125      ; "%A"
      EndIf
      
      buffer$ = Space(bufferSize)
      tm\tm_wday = DayOfWeek
      numBytes = strftime_(@buffer$, bufferSize*SizeOf(Character), @fmt, @tm)
      
      ProcedureReturn PeekS(@buffer$, numBytes, #PB_UTF8|#PB_ByteLength)
   EndProcedure
   
   Procedure.s LocalizedMonthName (MonthOfYear.i, short.i=#False)
      ; in : MonthOfYear: January=1, ..., December=12
      ;                   (compliant with PureBasic's Month() function)
      ;      short      : #True/#False
      ; out: short or full localized name of given month
      Protected tm.tm, fmt.i, numBytes.i, buffer$, bufferSize.i=80
      
      If short
         fmt = $6225      ; "%b"
      Else
         fmt = $4225      ; "%B"
      EndIf
      
      buffer$ = Space(bufferSize)
      tm\tm_mon = MonthOfYear - 1
      numBytes = strftime_(@buffer$, bufferSize*SizeOf(Character), @fmt, @tm)
      
      ProcedureReturn PeekS(@buffer$, numBytes, #PB_UTF8|#PB_ByteLength)
   EndProcedure   
CompilerEndIf


Procedure.s FormatDateEx (mask$, date.i=-1)
   ; in : mask$: can contain the same tokens as used with PB's FormatDate(),
   ;             plus the following additional ones:
   ;             - %ww    -->  full  localized name of given weekday
   ;             - %w     -->  short localized name of given weekday
   ;             - %mmmm  -->  full  localized name of given month
   ;             - %mmm   -->  short localized name of given month
   ;             - %d     -->  day   number without leading "0"
   ;             - %m     -->  month number without leading "0"
   ;      date : date value in PB's format; -1 for current system date and time
   ; out: mask string with all tokens replaced by the respective date values
   
   If date = -1
      date = Date()
   EndIf
   
   mask$ = ReplaceString(mask$, "%ww",   LocalizedDayName(DayOfWeek(date)))
   mask$ = ReplaceString(mask$, "%w",    LocalizedDayName(DayOfWeek(date), #True))
   mask$ = ReplaceString(mask$, "%mmmm", LocalizedMonthName(Month(date)))
   mask$ = ReplaceString(mask$, "%mmm",  LocalizedMonthName(Month(date), #True))
   mask$ = FormatDate(mask$, date)
   mask$ = ReplaceString(mask$, "%d", Str(Day(date)))
   mask$ = ReplaceString(mask$, "%m", Str(Month(date)))
   
   ProcedureReturn mask$
EndProcedure


; -- Demo
Debug FormatDateEx("%ww, %d. %mmmm %yyyy", Date(2016,6,19, 0,0,0))
Debug FormatDateEx("%w, %d. %mmm %yyyy")
Debug FormatDateEx("%d.%m.%yyyy")
Debug FormatDateEx("%dd.%mm.%yyyy")

Re: [Windows] FormatDateEx()

Posted: Mon Apr 09, 2012 2:33 pm
by Kwai chang caine
Cool works great on XP, thanks for sharing 8)

Re: FormatDateEx()

Posted: Tue Jun 21, 2016 6:09 am
by Little John
Added code for Linux (maybe it works on Mac OS as well).

Re: FormatDateEx()

Posted: Tue Jun 21, 2016 7:22 am
by wilbert
Little John wrote:maybe it works on Mac OS as well.
Seems to work fine on Mac as well. :)

Re: FormatDateEx()

Posted: Tue Jun 21, 2016 1:10 pm
by Little John
Hi wilbert,

that's good to know. :-)
I've changed the first post of this thread accordingly.
Many thanks for testing and reporting!

Re: FormatDateEx()

Posted: Fri Jan 20, 2017 12:26 am
by said
Thanks a lot for sharing :D :D
Just what i need :lol: i love this forum