# Is there any timing class in the standard lib?

**URL:** <https://www.ruby-forum.com/t/is-there-any-timing-class-in-the-standard-lib/84707>\
**Category:** Ruby\
**Created:** [February 7, 2007, 4:20am UTC](https://www.ruby-forum.com/t/is-there-any-timing-class-in-the-standard-lib/84707 "2007-02-07T04:20:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 7, 2007, 4:20am UTC](https://www.ruby-forum.com/t/is-there-any-timing-class-in-the-standard-lib/84707/1 "2007-02-07T04:20:11Z")

</div>

Hi guy,  
I 'm doing a deamon that call a method every hour. Is there any class in  
the standard library that I can use for counting the time?  
Thai

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 7, 2007, 4:34am UTC](https://www.ruby-forum.com/t/is-there-any-timing-class-in-the-standard-lib/84707/2 "2007-02-07T04:34:44Z")

</div>

> I 'm doing a deamon that call a method every hour. Is there  
> any class in  
> the standard library that I can use for counting the time?

If it’s only going to do something every hour, are you sure you want a  
daemon? Why not just have it run in a cron job (or scheduled task on  
windows). If it’s a daemon, it’s going to sit there and consume memory  
when it’s not doing anything.

If you really want your process to sit around, either use sleep(60\*60)  
or if you need more accuracy than that, do some calculations with  
DateTime to figure out how long it will be till the next hour mark.

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 7, 2007, 5:14am UTC](https://www.ruby-forum.com/t/is-there-any-timing-class-in-the-standard-lib/84707/3 "2007-02-07T05:14:22Z")

</div>

Daniel S. wrote:

> If you really want your process to sit around, either use sleep(60\*60)  
> or if you need more accuracy than that, do some calculations with  
> DateTime to figure out how long it will be till the next hour mark.

## Thanks 4 your advice but this’s part of the requirements, I have no access to system util. For testing, i try

class RSSdeamon  
def start  
puts “deamon started”  
while true  
sleep(5)  
puts “deamon running”  
end  
end  
end

## myProc = RSSdeamon.new() myProc.start

It doesnt print even a single line. Am I missing something in concurrent  
programming?

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 7, 2007, 6:21am UTC](https://www.ruby-forum.com/t/is-there-any-timing-class-in-the-standard-lib/84707/4 "2007-02-07T06:21:50Z")

</div>

> ## myProc = RSSdeamon.new() myProc.start
> 
> It doesnt print even a single line. Am I missing something in  
> concurrent  
> programming?

No, but I’m guessing you’re running windows and you’re missing something  
in IO flushing. stdout doesn’t get flushed automatically by puts.

Try putting a STDOUT.flush after your puts.

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 7, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/is-there-any-timing-class-in-the-standard-lib/84707/5 "2007-02-07T17:33:33Z")

</div>

Daniel S. wrote:

> > ## myProc = RSSdeamon.new() myProc.start
> > 
> > It doesnt print even a single line. Am I missing something in  
> > concurrent  
> > programming?
> 
> No, but I’m guessing you’re running windows and you’re missing something  
> in IO flushing. stdout doesn’t get flushed automatically by puts.
> 
> Try putting a STDOUT.flush after your puts.  
> Thanks man, it’s working now. BTW, in the case of not using STDOUT, the  
> methd puts is actually executed but only the result is not printed out,  
> am i correct?

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 8, 2007, 3:19am UTC](https://www.ruby-forum.com/t/is-there-any-timing-class-in-the-standard-lib/84707/6 "2007-02-08T03:19:14Z")

</div>

> Daniel S. wrote:
> 
> > Try putting a STDOUT.flush after your puts.

Some OSs, like linux, only flush the buffer to the target (STDOUT, in  
this case) when the internal storage buffer is full or when a certain  
event happens. Besides forcing flushing the buffer, One of this  
events is, in many cases, the occurence of a “\n”. At least, that’s  
my experience.

Paulo Jorge Duarte  
Köremoved\_email\_address@domain.invalid
