Thread Tools Display Modes
04/25/24, 01:08 PM   #1
VessVelendas
Join Date: Apr 2024
Posts: 5
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?
  Reply With Quote
04/26/24, 04:37 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,993
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.

Last edited by Baertram : 04/26/24 at 04:48 AM.
  Reply With Quote
04/26/24, 09:05 AM   #3
VessVelendas
Join Date: Apr 2024
Posts: 5
Originally Posted by Baertram View Post
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.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » How to use GetDateElementsFromTimestamp?


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