ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   How to use GetDateElementsFromTimestamp? (https://www.esoui.com/forums/showthread.php?t=10940)

VessVelendas 04/25/24 01:08 PM

How to use GetDateElementsFromTimestamp?
 
I need to log the date the addon was first initialized, and check if it's the same day/week to reset a variable. But I don't know how to use the in-game function or get the variables from it. Thus far I've only used functions I've created myself. So, how do I do that?

Baertram 04/26/24 04:37 AM

Code:

--- @param timestamp integer53
--- @return year integer, month integer, day integer
function GetDateElementsFromTimestamp(timestamp) end


Rough example
Lua Code:
  1. local oldYear, oldMonth, oldDay = GetDateElementsFromTimestamp(timeStampFromSavedVariables)
  2. local year, month, day = GetDateElementsFromTimestamp(GetTimeStamp())
  3. if year == oldYear and month == oldMonth then
  4.   if day ~= oldDay then
  5.      --changed
  6.   end
  7. else
  8.   if year ~= oldYear or month ~= oldMonth or day ~= oldDay then
  9.     --changed
  10.   end
  11. end


Add event_add_on_loaded you need to save the current timestamp to your SavedVariables then.
Remember that event is called each time for all addons so only update if YOUR addon is found, and also remember that event is called on each reloadui too!
So only update once per same year, month and day e.g. or you will overwrite it on each reloadui/login with current date.

Your check "old vs. current" needs to be done before updating the SavedVariables too or you will always comapre current with current.

VessVelendas 04/26/24 09:05 AM

Quote:

Originally Posted by Baertram (Post 49943)
Code:

--- @param timestamp integer53
--- @return year integer, month integer, day integer
function GetDateElementsFromTimestamp(timestamp) end


Rough example
Lua Code:
  1. local oldYear, oldMonth, oldDay = GetDateElementsFromTimestamp(timeStampFromSavedVariables)
  2. local year, month, day = GetDateElementsFromTimestamp(GetTimeStamp())
  3. if year == oldYear and month == oldMonth then
  4.   if day ~= oldDay then
  5.      --changed
  6.   end
  7. else
  8.   if year ~= oldYear or month ~= oldMonth or day ~= oldDay then
  9.     --changed
  10.   end
  11. end


Add event_add_on_loaded you need to save the current timestamp to your SavedVariables then.
Remember that event is called each time for all addons so only update if YOUR addon is found, and also remember that event is called on each reloadui too!
So only update once per same year, month and day e.g. or you will overwrite it on each reloadui/login with current date.

Your check "old vs. current" needs to be done before updating the SavedVariables too or you will always comapre current with current.

Thank you for all the help you've been giving me.


All times are GMT -6. The time now is 04:15 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI