FormatDateEx()

Share your advanced PureBasic knowledge/code with the community.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

FormatDateEx()

Post 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")
Last edited by Little John on Tue Jun 21, 2016 1:06 pm, edited 4 times in total.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: [Windows] FormatDateEx()

Post by Kwai chang caine »

Cool works great on XP, thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: FormatDateEx()

Post by Little John »

Added code for Linux (maybe it works on Mac OS as well).
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: FormatDateEx()

Post by wilbert »

Little John wrote:maybe it works on Mac OS as well.
Seems to work fine on Mac as well. :)
Windows (x64)
Raspberry Pi OS (Arm64)
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: FormatDateEx()

Post 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!
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: FormatDateEx()

Post by said »

Thanks a lot for sharing :D :D
Just what i need :lol: i love this forum
Post Reply