Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Table of days
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Wed 08 Aug 2007 05:19 PM (UTC) |
Message
| I'm trying to track the sales of a particular item sold over the auction system every day. I've been storing the data in a table where each key is os.date( "%Y%m%d" ). The only problem I'm facing now is that if I'm going to check up on say the last 7 days, or the last 30 days or anything like that, I have no real easy way to do this if it crosses months. Does anyone have any easier suggestions than for me to make a function to spit out a table with a range of dates? |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #1 on Wed 08 Aug 2007 06:35 PM (UTC) |
Message
| If you don't want to change your table format, I'm afraid you pretty much have to get into the string handling of subtracting one from the month, maybe the year if necessary, and bringing the day to 31, 30, 29 or 28 as necessary.
If you are willing to do so, it might be easier to index your table differently, so that the first index is the year, the second the month, and the third the day. Then at least you have the data values to work with, and don't need to worry about finding the latest day to check in string form: you just go to the biggest index. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Meerclar
USA (733 posts) Bio
|
Date
| Reply #2 on Wed 08 Aug 2007 06:37 PM (UTC) |
Message
| Feed it into a spreadsheet via delimited text if Lua can't do spreadsheet directly. |
Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #3 on Wed 08 Aug 2007 08:28 PM (UTC) |
Message
|
Quote: Feed it into a spreadsheet via delimited text if Lua can't do spreadsheet directly.
This isn't really a spreadsheet issue. Loading up a whole new program to do something this simple is just a waste of resources.
Quote: If you are willing to do so, it might be easier to index your table differently, so that the first index is the year, the second the month, and the third the day.
That's pretty much how I have it set up now. %Y is the current year in 4 digit format, %m is the month, and %d is the day. The first option you listed is what I'm doing already, I just thought there might be a function I couldn't find to adjust the date on a per day basis. My main problem with this is if I have the program shut down for a few days (shocking, but it happens on occasion), or if there are no sales for a day. In that situation, a date is skipped.
I suppose I could just have year and yday as the index values, and just have some check for a leap year. |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #4 on Wed 08 Aug 2007 08:57 PM (UTC) |
Message
|
Quote: My main problem with this is if I have the program shut down for a few days (shocking, but it happens on occasion), or if there are no sales for a day. In that situation, a date is skipped.
Yeah, I was wondering what you would do about holes. That's why I prefer the three dimensional solution instead of using strings as indices; it makes it a little easier to go back 30 days and then just count forward through the indices you actually have.
Quote: I just thought there might be a function I couldn't find to adjust the date on a per day basis
I'm sure such functions exist ad nauseam, you just have to find the date libraries and then hope they're in Lua or write them into Lua yourself. Lua itself doesn't have this facility AFAIK. And also AFAIK, date libraries (while not the hardest code to write in the world) aren't trivial either (lots of issues with leap years and whatnot), so it might be easier to look for one somebody else wrote (even like a calendaring library) than to try to roll your own from scratch. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #5 on Wed 08 Aug 2007 09:22 PM (UTC) |
Message
|
Quote: it makes it a little easier to go back 30 days and then just count forward through the indices you actually have.
I'm still unclear as to how this would be easier. You would still wind up with the same problem of counting backwards through a month. It's simple in Lua to access an index with sales["2007"..month..day] and just tick back through them. If you're tracking back 30 days, you will more than likely be crossing a month turnover, and since you would have to check for that anyway, it's the same effort as if you were counting back each day. If you have an array with indexes as sales[2007][month][day] then all you're doing is adding two more for loops when you want to display all the data.
Again, I'm just going to skip it and switch to the day of the year instead of the month and day. Seems to be a bit easier. |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #6 on Wed 08 Aug 2007 09:31 PM (UTC) |
Message
| Why not key by os.time rather than a date string made by os.date? That way the keys can easily be compared (since os.time is a number of seconds, you just add 60 * 60 * 24 to add a day).
Then, for printing, you just call os.date passing down the key, which will format it in readable form from the key.
And, if you *do* want a date like 1st August, you simply key into your table with:
k = os.time { year = 2007, month = 8, day = 1 }
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #7 on Wed 08 Aug 2007 09:37 PM (UTC) |
Message
| heh, nice... os.time { year = 2007, month = 6, day = 30 } is the same as os.time { year = 2007, month = 8, day = -31 }
I couldn't figure out how to do that with the date function, I always got an error with it.
|
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #8 on Wed 08 Aug 2007 10:09 PM (UTC) |
Message
| And I just figured out my issue with the date function. I didn't have a correct time in there... I was using os.date() instead of os.date("*t") This function works quite well.
function averagelast( days )
require "serialize"
assert (loadstring (GetVariable ("sales") or "")) ()
local count,total = 0,0
local t = os.date( "*t" )
for i = 0,( days-1 ) do
local tempday = os.date("%Y%m%d",os.time( { year = t.year, month = t.month, day = (t.day-i) } ) )
if sales[tempday] then
total = total + (sales[tempday].total or 0)
count = count + (sales[tempday].count or 0)
end
end
return total/count
end
|
It is much easier to fight for one's ideals than to live up to them. | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
24,207 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top