Link to home
Start Free TrialLog in
Avatar of doc_jay
doc_jay

asked on

LUA calander function to calculate date range from 'X' number of days old.

Hi,

   I need some help with LUA please.  I hope that this is a good place to post this.

I am running the following lua script to generate a .txt file.
 
a=DicomObject:new()
    a.QueryRetrieveLevel='STUDY'
    a.StudyInstanceUID=''
    --a.PatientName='*test*'
    --a.AccessionNumber='123456'
    a.StudyDate='19000101-20070714'
    a.PatientBirthDate = '19000101-19920714'
    a.ModalitiesInStudy = ''
    b=dicomquery('ARCH', 'STUDY', a)
    f=io.open('suid.txt', 'wt')
    for i=0, #b-1 do
     if string.find(b[i].ModalitiesInStudy, 'MG')==nil then
       f:write(b[i].StudyInstanceUID .. '\n')
     end
    end

Open in new window


What I need help with is simplifying this so that I won't have to edit it every time I need to run it.  And that would include the date range 'a.StudyDate='19000101-20070714''.  I need to have a date range here as this script uses it.  Is there a function that lua can do to calculate the date range for me if I say, show me everything that is '2190' days old and older? I have been using the beginning date of '19000101' because I knew that I would not have anything that old.  Also, the date format above needs to stay as 'yearmonthday'.

thank you!!
Avatar of aikimark
aikimark
Flag of United States of America image

Here are some relevant date libraries that might facilitate your task.
https://github.com/LuaDist/luadate
http://lua-users.org/lists/lua-l/2005-08/msg00694.html


According to the lua date calculation examples in the following link, you might just be able to subtract a number of days from a date.  Once that is done, you would need to format the dates back into the string ranges you are using.
http://scilua.org/time.html
Avatar of doc_jay
doc_jay

ASKER

Aikimark,

   thanks for the reply.  I d/l the date library from the github link.  I really don't know how to write lua as the code I have I have pieced together from others helping me out.  I'll look at the examples from your last link and see if I can have any luck.

Thanks again
Generally, I advise people to normalize their data.  In the case of your date ranges, I would have two data elements (begin date, end date) rather than the delimited string you are currently using.
Example:
a.StudyDateStart='19000101'
a.StudyDateEnd='20070714'
a.PatientBirthDateStart = '19000101'
a.PatientBirthDateEnd = '19920714'

Open in new window

Like you, I am still learning lua.
Avatar of doc_jay

ASKER

Okay - so I have this code now that works:

       
        days = 2192
        startdate = os.date(19000101)
        enddate = os.date('%Y%m%d', os.time()-days*24*3600)
        print (startdate .. '-' .. enddate)
        days = 7670
        startDOB = os.date(19000101)
        endDOB = os.date('%Y%m%d', os.time()-days*24*3600)
        print (startDOB .. '-' .. endDOB)

Open in new window


and it is displayed how I need it.  This displays the output at this time as:

19000101-20070717
19000101-19920717

Open in new window


...I just need now for each line to be placed inside single quotes like:

'19000101-20070717'
'19000101-19920717'

Open in new window


or else my script won't complete correctly.
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial