# Date Object and Functions

6 messages
Open this post in threaded view
|

## Date Object and Functions

 ```Hi. I want to calculate days between two dates. There isn't object date in Lua and I decided to write function with string parameters to calculate this. Can you somebody help me? I wrote this: function days( date1, date2 ) d1 = os.date("*t"); d1.hour, d1.min, d1.sec = 0, 0, 0; d1.day, d1.month, d1.year = string.gfind( date1, "(%d+).(%d+).(%d+)"); d2 = os.date("*t"); d2.hour, d2.min, d2.sec = 0, 0, 0; d2.day, d2.month, d2.year = string.gfind( date2, "(%d+).(%d+).(%d+)"); t1 = os.time( d1 ); t2 = os.time( d2 ); t = os.difftime( t1, t2 ); return ( t / (60*60*24) ); end ...but gfind do not work for me:-( I need Lua-function like sscanf in C... Thank You in advance. Stoil __________________________________ Do you Yahoo!? New Yahoo! Photos - easier uploading and sharing. http://photos.yahoo.com/ ```
Open this post in threaded view
|

## Re: Date Object and Functions

 ```>d1.day, d1.month, d1.year = string.gfind( date1, "(%d+).(%d+).(%d+)"); >...but gfind do not work for me:-( You need to ignore the first 2 return values. Try the code below. --lhf function str2time(s) local d={} local a,b a, b, d.day, d.month, d.year = string.find(s, "(%d+).(%d+).(%d+)"); return os.time(d) end function diff(a,b) return os.difftime(str2time(b),str2time(a))/(60*60*24) end ```
Open this post in threaded view
|

## Re: Date Object and Functions

 ```Thank You, Luiz Now everything is OK:-) I read about strfind function in "Programing in Lua",Roberto Ierusalimschy but it is about Lua 4.0... In my opinion, reading "Lua 5.0 Reference Manual", gfind is the nearest by strfind...but I don't know why it does not work as well I suppose:-( --- Luiz Henrique de Figueiredo <[hidden email]> wrote: > >d1.day, d1.month, d1.year = string.gfind( date1, > "(%d+).(%d+).(%d+)"); > >...but gfind do not work for me:-( > > You need to ignore the first 2 return values. Try > the code below. > --lhf > > function str2time(s) > local d={} > local a,b > a, b, d.day, d.month, d.year = string.find(s, > "(%d+).(%d+).(%d+)"); > return os.time(d) > end > > function diff(a,b) > return > os.difftime(str2time(b),str2time(a))/(60*60*24) > end __________________________________ Do you Yahoo!? New Yahoo! Photos - easier uploading and sharing. http://photos.yahoo.com/ ```
Open this post in threaded view
|

## Re: Date Object and Functions

 In reply to this post by Luiz Henrique de Figueiredo ```Luiz Henrique de Figueiredo schrieb: ``````d1.day, d1.month, d1.year = string.gfind( date1, "(%d+).(%d+).(%d+)"); ...but gfind do not work for me:-( `````` You need to ignore the first 2 return values. `````` ```... and use string.find instead of string.gfind! string.gfind is meant to be used in for-loops. Btw., I have written some date calculation functions myself. They are not perfect (and not well-tested either), but you could have a look (or use them if you like): ``` http://www.ratnet.stw.uni-erlangen.de/~siphjand/lua50/date.lua ``````Try the code below. --lhf function str2time(s) local d={} local a,b a, b, d.day, d.month, d.year = string.find(s, "(%d+).(%d+).(%d+)"); return os.time(d) end function diff(a,b) return os.difftime(str2time(b),str2time(a))/(60*60*24) end `````` ```
 ```Thank you very much, Philipp! I think your code will help me to learn more about programming with Lua:-) --- Philipp Janda <[hidden email]> wrote: > Luiz Henrique de Figueiredo schrieb: > >>d1.day, d1.month, d1.year = string.gfind( date1, > "(%d+).(%d+).(%d+)"); > >>...but gfind do not work for me:-( > > > > > > You need to ignore the first 2 return values. > > ... and use string.find instead of string.gfind! > string.gfind is meant > to be used in for-loops. > Btw., I have written some date calculation functions > myself. They are > not perfect (and not well-tested either), but you > could have a look (or > use them if you like): > > http://www.ratnet.stw.uni-erlangen.de/~siphjand/lua50/date.lua > > > > Try the code below. > > --lhf > > > > function str2time(s) > > local d={} > > local a,b > > a, b, d.day, d.month, d.year = string.find(s, > "(%d+).(%d+).(%d+)"); > > return os.time(d) > > end > > > > function diff(a,b) > > return > os.difftime(str2time(b),str2time(a))/(60*60*24) > > end > > > > __________________________________ Do you Yahoo!? New Yahoo! Photos - easier uploading and sharing. http://photos.yahoo.com/ ```
 In reply to this post by Luiz Henrique de Figueiredo ```Hi, All. I have the next problem: I use Lua 5.0 and I want my code to not be dependented by locale "decimal symbol". I decide to use the point as decimal symbol, but if in Windows is chosen comma as decimal symbol, Lua show error... I want to change locale settings in my program at the begining and when Lua finish, to restore settings for Number:decimal symbols... I see that I have to use os.setlocale("numeric","..."), but I dont know exactly how:-( Can you anybody help me? __________________________________ Do you Yahoo!? Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes http://hotjobs.sweepstakes.yahoo.com/signingbonus ```