Author Topic: TimeFormat function  (Read 615 times)

tlarson

  • Newbie
  • *
  • Posts: 3
TimeFormat function
« on: January 31, 2018, 11:28:12 am »
I have a function TimeFormat that was created in 2016B and I am now on 2017B and the function is failing with the wrong number of arguments. Is there a way to change the run to execute as a user defined and not system?

#DefineFunction TimeFormat(spn_lang,spn_lang,YmdHms,Format)
YY = ItemExtract(1,YmdHms,":")                             ;;;; Extract Year
MM = ItemExtract(2,YmdHms,":")                             ;;;; Month
DD = ItemExtract(3,YmdHms,":")                             ;;;; and day.
If YY<100 Then YY = YY + 2000                              ;;;; Two digit years assumed to be 2000

Hour24 = ItemExtract(4,YmdHms,":")                         ;;;; Extract Hour
Minute = ItemExtract(5,YmdHms,":")                         ;;;; Minute
Second = ItemExtract(6,YmdHms,":")                         ;;;; and second
If Hour24<12
   Hour12 = Hour24                                         ;;;; Get 12-hour format hour
   AMPM = "am"                                             ;;;; and AM/PM value
Else
   Hour12 = Hour24 - 12
   AMPM = "pm"
EndIf
If Hour12==0 Then Hour12 = 12                              ;;;; Get julian day and day of week
JJJ = TimeDiffDays(YmdHms,StrCat(ItemExtract(1,YmdHms,":"),":01:01:00:00:00")) + 1
Weekday = ((TimeJulianDay(YmdHms) + 5) mod 7) + 1          ;;;; Get month and day name
If spn_lang=="1"
   MMMM = ItemExtract(MM,"de enero de.de febrero de.de marzo de.de abril de.de mayo de.de junio de.de julio de.de agosto de.de septiembre de.de octubre de.de noviembre de.de diciembre de",".")
   DDDD = ItemExtract(Weekday,"Domingo.Lunes.Martes.Miércoles.Jueves.Viernes.Sábado",".")
Else
   MMMM = ItemExtract(MM,"January.February.March.April.May.June.July.August.September.October.November.December",".")
   DDDD = ItemExtract(Weekday,"Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday",".")
EndIf

FX = 1                                                     ;;;; Copy the format to the buffer, looking for substititutions
LenFmt = StrLen(Format)
Output = ""

While @true
   If FX>LenFmt Then Break
   Four = StrSub(Format,FX,4)
   Three = StrSub(Four,1,3)
   Two = StrSub(Three,1,2)
   New = StrSub(Two,1,1)
   Skip = 1
   If StrScan(New,"aAYMDHZhmszJ",1,@fwdscan)
      Select @true
      Case Two=="am"        ;;;    am          am or pm. (lower case.)
         Skip = 2
         New = AMPM
         Break
      Case Two=="AM"        ;;;    AM          AM or PM  (upper case.)
         Skip = 2
         New = StrUpper(AMPM)
         Break
      Case Four=="YYYY"     ;;;    YYYY        Four digit year
         skip = 4
         New = YY
         Break
      Case Two=="YY"        ;;;    YY          Two digit year
         skip = 3
         New = StrFixLeft(YY mod 100,"0",2)
         Break
      Case Four=='MMMM'     ;;;    MMMM        Full name of the month
         skip = 4
         New = MMMM
         Break
      Case Three=="MMM"     ;;;    MMM         Three letter abbreviation of the month
         skip = 3
         New = StrSub(MMMM,1,3)
         Break
      Case Two=="MM"        ;;;    MM          The month as a two digit number
         skip = 3
         New = StrFixLeft(MM,"0",2)
         Break
      Case Four=="DDDD"     ;;;    DDDD        Full name of the day of the week.
         skip = 4
         New = DDDD
         Break
      Case Three=="DDD"     ;;;    DDD         Three letter abbreviation of the day of the week.
         skip = 3
         New = StrSub(DDDD,1,3)
         Break
      Case Two=="DD"        ;;;    DD          Day of the month, as a two digit number.
         skip = 2
         New = StrFixLeft(DD,"0",2)
         Break
      Case Two=="HH"        ;;;    HH          The hour, in 24 hour format, as a two digit number.
         skip = 2
         New = StrFixLeft(Hour24,"0",2)
         Break
      Case Two=='hh'        ;;;    hh          The hour, in 12 hour format, as a two digit number.
         skip = 2
         New = StrFixLeft(Hour12,"0",2)
         Break
      Case Two=='mm'        ;;;    mm          The minute as a two digit number
         skip = 2
         New = StrFixLeft(Minute,"0",2)
         Break
      Case Two=='ss'        ;;;    ss          Seconds as a two digit number.
         skip = 2
         New = StrFixLeft(Second,"0",2)
         Break
      Case Two=="ZM"        ;;;    ZM          Month as a zero-suppressed number.
         skip = 2
         New = MM + 0
         Break
      Case Two=="ZD"        ;;;    ZD          Day of the month as a zero-suppressed number.
         skip = 2
         New = DD + 0
         Break
      Case Two=="ZH"        ;;;    ZH          Hour in 24 hour format, zero-suppressed.
         skip = 2
         New = Hour24 + 0
         Break
      Case Two=="zh"        ;;;    zh          Hour in 12 hour format, zero-suppressed.
         skip = 2
         New = Hour12 + 0
         Break
      Case Two=="zm"        ;;;    zm          Minute, zero-suppressed.
         skip = 2
         New = Minute + 0
         Break
      Case Two=="zs"        ;;;    zs          Seconds, zero-supressed.
         skip = 2
         New = Second + 0
         Break
      Case Three=="JJJ"     ;;;    JJJ         Julian day.  (Number of days since the beginning of the year.)
         skip = 3
         New = StrFixLeft(JJJ,"0",3)
         Break
      End Select
   EndIf

   Output = StrCat(Output,New)
   FX = FX + Skip
EndWhile
Return Output
#EndFunction

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 0779 - LCTimeFormat
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#DefineFunction LCTimeFormat(spn_lang,spn_lang,YmdHms,Format)
YY = ItemExtract(1,YmdHms,":")                             ;;;; Extract Year
MM = ItemExtract(2,YmdHms,":")                             ;;;; Month
DD = ItemExtract(3,YmdHms,":")                             ;;;; and day.
If YY<100 Then YY = YY + 2000                              ;;;; Two digit years assumed to be 2000

Hour24 = ItemExtract(4,YmdHms,":")                         ;;;; Extract Hour
Minute = ItemExtract(5,YmdHms,":")                         ;;;; Minute
Second = ItemExtract(6,YmdHms,":")                         ;;;; and second
If Hour24<12
   Hour12 = Hour24                                         ;;;; Get 12-hour format hour
   AMPM = "am"                                             ;;;; and AM/PM value
Else
   Hour12 = Hour24 - 12
   AMPM = "pm"
EndIf
If Hour12==0 Then Hour12 = 12                              ;;;; Get julian day and day of week
JJJ = TimeDiffDays(YmdHms,StrCat(ItemExtract(1,YmdHms,":"),":01:01:00:00:00")) + 1
Weekday = ((TimeJulianDay(YmdHms) + 5) mod 7) + 1          ;;;; Get month and day name
If spn_lang=="1"
   MMMM = ItemExtract(MM,"de enero de.de febrero de.de marzo de.de abril de.de mayo de.de junio de.de julio de.de agosto de.de septiembre de.de octubre de.de noviembre de.de diciembre de",".")
   DDDD = ItemExtract(Weekday,"domingo.lunes.martes.miércoles.jueves.viernes.sábado",".")
Else
   MMMM = ItemExtract(MM,"January.February.March.April.May.June.July.August.September.October.November.December",".")
   DDDD = ItemExtract(Weekday,"Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday",".")
EndIf

FX = 1                                                     ;;;; Copy the format to the buffer, looking for substititutions
LenFmt = StrLen(Format)
Output = ""

While @true
   If FX>LenFmt Then Break
   Four = StrSub(Format,FX,4)
   Three = StrSub(Four,1,3)
   Two = StrSub(Three,1,2)
   New = StrSub(Two,1,1)
   Skip = 1
   If StrScan(New,"aAYMDHZhmszJ",1,@fwdscan)
      Select @true
      Case Two=="am"        ;;;    am          am or pm. (lower case.)
         Skip = 2
         New = AMPM
         Break
      Case Two=="AM"        ;;;    AM          AM or PM  (upper case.)
         Skip = 2
         New = StrUpper(AMPM)
         Break
      Case Four=="YYYY"     ;;;    YYYY        Four digit year
         skip = 4
         New = YY
         Break
      Case Two=="YY"        ;;;    YY          Two digit year
         skip = 3
         New = StrFixLeft(YY mod 100,"0",2)
         Break
      Case Four=='MMMM'     ;;;    MMMM        Full name of the month
         skip = 4
         New = MMMM
         Break
      Case Three=="MMM"     ;;;    MMM         Three letter abbreviation of the month
         skip = 3
         New = StrSub(MMMM,1,3)
         Break
      Case Two=="MM"        ;;;    MM          The month as a two digit number
         skip = 3
         New = StrFixLeft(MM,"0",2)
         Break
      Case Four=="DDDD"     ;;;    DDDD        Full name of the day of the week.
         skip = 4
         New = DDDD
         Break
      Case Three=="DDD"     ;;;    DDD         Three letter abbreviation of the day of the week.
         skip = 3
         New = StrSub(DDDD,1,3)
         Break
      Case Two=="DD"        ;;;    DD          Day of the month, as a two digit number.
         skip = 2
         New = StrFixLeft(DD,"0",2)
         Break
      Case Two=="HH"        ;;;    HH          The hour, in 24 hour format, as a two digit number.
         skip = 2
         New = StrFixLeft(Hour24,"0",2)
         Break
      Case Two=='hh'        ;;;    hh          The hour, in 12 hour format, as a two digit number.
         skip = 2
         New = StrFixLeft(Hour12,"0",2)
         Break
      Case Two=='mm'        ;;;    mm          The minute as a two digit number
         skip = 2
         New = StrFixLeft(Minute,"0",2)
         Break
      Case Two=='ss'        ;;;    ss          Seconds as a two digit number.
         skip = 2
         New = StrFixLeft(Second,"0",2)
         Break
      Case Two=="ZM"        ;;;    ZM          Month as a zero-suppressed number.
         skip = 2
         New = MM + 0
         Break
      Case Two=="ZD"        ;;;    ZD          Day of the month as a zero-suppressed number.
         skip = 2
         New = DD + 0
         Break
      Case Two=="ZH"        ;;;    ZH          Hour in 24 hour format, zero-suppressed.
         skip = 2
         New = Hour24 + 0
         Break
      Case Two=="zh"        ;;;    zh          Hour in 12 hour format, zero-suppressed.
         skip = 2
         New = Hour12 + 0
         Break
      Case Two=="zm"        ;;;    zm          Minute, zero-suppressed.
         skip = 2
         New = Minute + 0
         Break
      Case Two=="zs"        ;;;    zs          Seconds, zero-supressed.
         skip = 2
         New = Second + 0
         Break
      Case Three=="JJJ"     ;;;    JJJ         Julian day.  (Number of days since the beginning of the year.)
         skip = 3
         New = StrFixLeft(JJJ,"0",3)
         Break
      End Select
   EndIf

   Output = StrCat(Output,New)
   FX = FX + Skip
EndWhile
Return Output
#EndFunction

td

  • Tech Support
  • *****
  • Posts: 2437
    • WinBatch
Re: TimeFormat function
« Reply #1 on: January 31, 2018, 11:34:21 am »
You made an unfortunate choice of UDF name.  Since TimeFormat is now a WIL function name, you will either have to use the WIL function or change the name of your UDF.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


tlarson

  • Newbie
  • *
  • Posts: 3
Re: TimeFormat function
« Reply #2 on: January 31, 2018, 11:45:46 am »
That was my assumption. Unfortunately I didn't do the original code. Thank you for your help!

td

  • Tech Support
  • *****
  • Posts: 2437
    • WinBatch
Re: TimeFormat function
« Reply #3 on: January 31, 2018, 01:31:54 pm »
In all the years that WinBatch has had UDF/S's, this is the first time I recall someone having a name collision problem with a new WIL function.   That said, if you ever happen to create a UDF/S of your own, it is good practice to avoid naming UDF/S's with names that might follow the WIL function naming convention.  Which is referred to internally as WinBatch backward-speak but the in-house wag.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


JTaylor

  • Pundit
  • *****
  • Posts: 858
    • Data & Stuff Inc.
Re: TimeFormat function
« Reply #4 on: January 31, 2018, 02:17:12 pm »
For what it is worth...I always use an "_" in my names to avoid such issues.

Jim

tlarson

  • Newbie
  • *
  • Posts: 3
Re: TimeFormat function
« Reply #5 on: January 31, 2018, 02:47:39 pm »
That's a good point Thanks!

ChuckC

  • Full Member
  • ***
  • Posts: 145
Re: TimeFormat function
« Reply #6 on: February 01, 2018, 07:20:06 am »
Is there any chance that name spaces as used in C#/C++ could be implemented in a future version of the WIL interpreter?  Having all of the built-in functions & constants in a "wil" namespace that is "used" by default would provide backwards compatibility, while all script-defined functions, subroutines and constants would reside in a user-defined name space [with a default name space name that also "used" by default].  Perhaps have any/all "#include" commands be capable of placing all included code within the user-defined default name space or a specified name space if the included content doesn't already have a name space of its own declared in its WBT/WBC file.

td

  • Tech Support
  • *****
  • Posts: 2437
    • WinBatch
Re: TimeFormat function
« Reply #7 on: February 01, 2018, 02:26:11 pm »
Interesting idea.  Thanks for the suggestion.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates