Fai Wong
2013-Sep-20 07:30 UTC
Retrieve duration (in string format) from database and convert to Ruby syntax for calculation
If I store 1.month as a string in the database, how do I convert "1.month" into 1.month? This way I can use the "1.month" value stored in database to perform the following calculation. Time.now + 1.month -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a19f9925-f725-4cd5-9dfd-6c8c4b897bd6%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Raj Kumar
2013-Sep-20 08:47 UTC
Re: Retrieve duration (in string format) from database and convert to Ruby syntax for calculation
I don''t think this is good idea. 1.month.to_i will return in seconds as 2592000. you can store this and while fetching you can use it as it is Time.now + 2592000 On Friday, 20 September 2013 03:30:16 UTC-4, Fai Wong wrote:> > If I store 1.month as a string in the database, how do I convert "1.month" > into 1.month? > > This way I can use the "1.month" value stored in database to perform the > following calculation. > > Time.now + 1.month >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a48f71fa-1f31-4037-94a4-c876bbc2630a%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Raj Kumar
2013-Sep-20 08:52 UTC
Re: Retrieve duration (in string format) from database and convert to Ruby syntax for calculation
btw you can do it using "eval" like eval("1.month") On Friday, 20 September 2013 03:30:16 UTC-4, Fai Wong wrote:> > If I store 1.month as a string in the database, how do I convert "1.month" > into 1.month? > > This way I can use the "1.month" value stored in database to perform the > following calculation. > > Time.now + 1.month >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f1f52892-acd5-47d6-8d50-7686bfb39841%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Eric Hayes
2013-Sep-20 15:24 UTC
Re: Retrieve duration (in string format) from database and convert to Ruby syntax for calculation
Take a look at this gem: https://github.com/peleteiro/ruby-duration —it will convert an arbitrary duration (like 1 month, 2 weeks, etc.) into seconds which can be stored in the DB. It is very similar to what Raj is recommending, just a little more formal. Either way you can avoid eval''ing code, and your calculation can be a lot more flexible. On Friday, September 20, 2013 12:30:16 AM UTC-7, Fai Wong wrote:> > If I store 1.month as a string in the database, how do I convert "1.month" > into 1.month? > > This way I can use the "1.month" value stored in database to perform the > following calculation. > > Time.now + 1.month >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bd968e41-7f42-4ca0-91ba-a608b4a75955%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Norm Scherer
2013-Sep-21 21:20 UTC
Re: Re: Retrieve duration (in string format) from database and convert to Ruby syntax for calculation
But not all months are 2592000 seconds long.....some are shorter and some are longer.... If you have a Date of 1 Jan and add 1.month to it you will get 1 Feb which is 31 days later. If you have 1 Feb and you add 1.month you will get 1 Mar which is 28 or 29 days later. You could just store the value as x so you could do Time.now + x.month Norm On 09/20/2013 01:47 AM, Raj Kumar wrote:> I don''t think this is good idea. 1.month.to_i will return in seconds > as 2592000. you can store this and while fetching you can use it as it is > Time.now + 2592000 > > On Friday, 20 September 2013 03:30:16 UTC-4, Fai Wong wrote: > > If I store 1.month as a string in the database, how do I convert > "1.month" into 1.month? > > This way I can use the "1.month" value stored in database to > perform the following calculation. > > Time.now + 1.month > > ---- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/523E0D91.50302%40earthlink.net. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Sep-22 07:58 UTC
Re: Retrieve duration (in string format) from database and convert to Ruby syntax for calculation
On 20 September 2013 08:30, Fai Wong <wongwf82-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If I store 1.month as a string in the database, how do I convert "1.month" > into 1.month? > > This way I can use the "1.month" value stored in database to perform the > following calculation. > > Time.now + 1.monthIf you really feel you need to do this then you can store "1.month" as a string and use eval 1.9.3p194 :007 > delta = "1.month" => "1.month" 1.9.3p194 :008 > Time.now => 2013-09-22 08:52:13 +0100 1.9.3p194 :009 > Time.now + eval( delta ) => 2013-10-22 08:52:23 +0100 However if you do this be /very/ careful about what can get into the database as arbitrary code can be executed using eval and conceivably your machine could be hacked. I DO NOT recommend that you do this. If your requirement is for a delta with values such as 1 month 3 weeks and so on, then I suggest having two fields, one for the quantity and an enumerated value for the period (month, week and so on). Then work it out in code. Less efficient but /much/ safer. Colin> > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/a19f9925-f725-4cd5-9dfd-6c8c4b897bd6%40googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuA668r_NV%2BEubhH_TRSkshYS4ia_8hPC5ZkATOqeRMRg%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Fai Wong
2013-Sep-24 14:50 UTC
Re: Retrieve duration (in string format) from database and convert to Ruby syntax for calculation
Thanks for your help everyone. I ended up using Chronic Duration gem. https://github.com/hpoydar/chronic_duration On Sunday, September 22, 2013 3:58:58 PM UTC+8, Colin Law wrote:> > On 20 September 2013 08:30, Fai Wong <wong...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> > wrote: > > If I store 1.month as a string in the database, how do I convert > "1.month" > > into 1.month? > > > > This way I can use the "1.month" value stored in database to perform the > > following calculation. > > > > Time.now + 1.month > > If you really feel you need to do this then you can store "1.month" as > a string and use eval > > 1.9.3p194 :007 > delta = "1.month" > => "1.month" > 1.9.3p194 :008 > Time.now > => 2013-09-22 08:52:13 +0100 > 1.9.3p194 :009 > Time.now + eval( delta ) > => 2013-10-22 08:52:23 +0100 > > However if you do this be /very/ careful about what can get into the > database as arbitrary code can be executed using eval and conceivably > your machine could be hacked. I DO NOT recommend that you do this. > If your requirement is for a delta with values such as 1 month 3 weeks > and so on, then I suggest having two fields, one for the quantity and > an enumerated value for the period (month, week and so on). Then work > it out in code. Less efficient but /much/ safer. > > Colin > > > > > -- > > You received this message because you are subscribed to the Google > Groups > > "Ruby on Rails: Talk" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. > > To post to this group, send email to rubyonra...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:>. > > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/rubyonrails-talk/a19f9925-f725-4cd5-9dfd-6c8c4b897bd6%40googlegroups.com. > > > For more options, visit https://groups.google.com/groups/opt_out. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9273b7f1-c2bc-4776-9ab1-25f77341af30%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.