source: timer.c @ fa00c5c

owl
Last change on this file since fa00c5c was fa00c5c, checked in by James M. Kretchmar <kretch@mit.edu>, 16 years ago
Correct license.
  • Property mode set to 100644
File size: 2.8 KB
Line 
1/* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar
2 *
3 * This file is part of Owl.
4 *
5 * Owl is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * Owl is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Owl.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 * ---------------------------------------------------------------
19 *
20 * As of Owl version 2.1.12 there are patches contributed by
21 * developers of the branched BarnOwl project, Copyright (c)
22 * 2006-2009 The BarnOwl Developers. All rights reserved.
23 */
24
25#include "owl.h"
26
27#define OWL_TIMER_DIRECTION_COUNTUP    1
28#define OWL_TIMER_DIRECTION_COUNTDOWN  2
29
30
31/* Create a "counting up" timer.  The counter starts running as soon
32 * as this is called.  Use owl_timer_reset() to reset it.
33 */
34void owl_timer_create_countup(owl_timer *t)
35{
36  t->direction=OWL_TIMER_DIRECTION_COUNTUP;
37  t->starttime=time(NULL);
38}
39
40/* create a "counting down" timer, which counts down from 'start'
41 * seconds.  The counter starts running as soon as this is called.
42 * Use owl_timer_reset to reset it.
43 */
44void owl_timer_create_countdown(owl_timer *t, int start)
45{
46  t->direction=OWL_TIMER_DIRECTION_COUNTDOWN;
47  t->start=start;
48  t->starttime=time(NULL);
49}
50
51/* Reset the timer.  For a "counting up" timer, it is reset to 0.  For
52 * a "counting down" timer it is set to the value initially set with
53 * owl_timer_create_countdown() or the last value set with
54 * owl_timer_reset_newstart() */
55void owl_timer_reset(owl_timer *t)
56{
57  t->starttime=time(NULL);
58}
59
60/* Only for a countdown timer.  Rest the timer, but this time (and on
61 * future resets) start with 'start' seconds.
62 */
63void owl_timer_reset_newstart(owl_timer *t, int start)
64{
65  t->start=start;
66  t->starttime=time(NULL);
67}
68
69/* Return the number of seconds elapsed or remaining.  If using a
70 * countdown timer, a negative value is never reported.  Once the
71 * timer gets to 0 it stays at 0 */
72int owl_timer_get_time(owl_timer *t)
73{
74  time_t now;
75  int rem;
76
77  now=time(NULL);
78
79  if (t->direction==OWL_TIMER_DIRECTION_COUNTUP) {
80    return(now-t->starttime);
81  } else if (t->direction==OWL_TIMER_DIRECTION_COUNTDOWN) {
82    rem=t->start-(now-t->starttime);
83    if (rem<0) return(0);
84    return(rem);
85  }
86
87  /* never reached */
88  return(0);
89}
90
91/* Only for countdown timer.  Return true if time has run out (the
92 * timer is at 0 seconds or less
93 */
94int owl_timer_is_expired(owl_timer *t)
95{
96  if (owl_timer_get_time(t)==0) return(1);
97  return(0);
98}
Note: See TracBrowser for help on using the repository browser.