[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Table of days

Table of days

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Shaun Biggs   USA  (644 posts)  [Biography] 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.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] 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
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] 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.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] 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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] 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
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] 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.
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] 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.
[Go to top] 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.


21,172 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]