Thread Tools Display Modes
03/29/15, 06:07 AM   #1
lightz39
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 21
How to format GetTimeString()?

Hey everyone. I was wondering how I could go about formatting a time string? The default format for the string is "00:00:00" where I want something like "0:00 P.M.". I've looked around a bit but haven't found anything really.

Any help is appreciated.
  Reply With Quote
03/29/15, 06:43 AM   #2
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Use ZO_FormatClockTime() instead. It will show 12h format for English client users, 24h format for all other users (German, French). Or you can switch format manually by calling ZO_SetClockFormat(clockFormat) where clockFormat could be TIME_FORMAT_PRECISION_TWELVE_HOUR or TIME_FORMAT_PRECISION_TWENTY_FOUR_HOUR.
  Reply With Quote
03/29/15, 07:08 AM   #3
Ayantir
 
Ayantir's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 1,019
If you don't want to use core function, you can use an old trick like this :
But I clearly think I will rewrite this function with the two ones Garkin just quoted.


Lua Code:
  1. -- Return a formatted time
  2. -- timeStr = GetTimeString()
  3. -- formatStr: HH: hours (24), hh: hours (12), H: hour (24, no leading 0), h: hour (12, no leading 0), A: AM/PM, a: am/pm, m: minutes, s: seconds
  4. local function createTimestamp(timeStr, formatStr)
  5.     formatStr = formatStr or pChat.opts.timestampFormat
  6.    
  7.     -- split up default timestamp
  8.     local hours, minutes, seconds = timeStr:match("([^%:]+):([^%:]+):([^%:]+)")
  9.     local hoursNoLead = tonumber(hours) -- hours without leading zero
  10.     local hours12NoLead = (hoursNoLead - 1)%12 + 1
  11.     local hours12
  12.     if (hours12NoLead < 10) then
  13.         hours12 = "0" .. hours12NoLead
  14.     else
  15.         hours12 = hours12NoLead
  16.     end
  17.     local pUp = "AM"
  18.     local pLow = "am"
  19.     if (hoursNoLead >= 12) then
  20.         pUp = "PM"
  21.         pLow = "pm"
  22.     end
  23.    
  24.     -- create new one
  25.     local timestamp = formatStr
  26.     timestamp = timestamp:gsub("HH", hours)
  27.     timestamp = timestamp:gsub("H", hoursNoLead)
  28.     timestamp = timestamp:gsub("hh", hours12)
  29.     timestamp = timestamp:gsub("h", hours12NoLead)
  30.     timestamp = timestamp:gsub("m", minutes)
  31.     timestamp = timestamp:gsub("s", seconds)
  32.     timestamp = timestamp:gsub("A", pUp)
  33.     timestamp = timestamp:gsub("a", pLow)
  34.    
  35.     return timestamp
  36.    
  37. end
  Reply With Quote
03/29/15, 07:31 AM   #4
lightz39
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 21
Thanks both of you. Garkin for the quick solution and Ayantir for the detailed one.
  Reply With Quote
01/06/16, 12:24 PM   #5
axsk
Join Date: Jan 2016
Posts: 1
Can i somehow also get milliseconds in the timestamps?
  Reply With Quote
01/06/16, 01:10 PM   #6
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,589
Originally Posted by axsk View Post
Can i somehow also get milliseconds in the timestamps?
Not really, but you might be able to fake them by using GetGameTimeMilliseconds. You just need a way to sync that with the GetTimeStamp return value, otherwise you might get a timestamp that does not grow continuously.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » How to format GetTimeString()?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off