time.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2007 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 /*
00034  * $Log: time.c,v $
00035  * Revision 1.6  2008/08/11 06:59:41  haraldkipp
00036  * BSD types replaced by stdint types (feature request #1282721).
00037  *
00038  * Revision 1.5  2007/06/03 08:49:56  haraldkipp
00039  * Bug #1724015 fixed. Simplified code in time().
00040  *
00041  * Revision 1.4  2006/10/05 17:20:13  haraldkipp
00042  * Standard C functions now support hardware RTC.
00043  * Ugly type conversion replaced.
00044  *
00045  * Revision 1.3  2003/12/19 22:26:38  drsung
00046  * Dox written.
00047  *
00048  * Revision 1.2  2003/11/26 11:14:32  haraldkipp
00049  * Portability issues
00050  *
00051  * Revision 1.1  2003/11/24 18:07:37  drsung
00052  * first release
00053  *
00054  *
00055  */
00056 
00057 #include <stdint.h>
00058 
00059 #include <time.h>
00060 #include <sys/timer.h>
00061 #include <sys/atom.h>
00062 #include <stdlib.h>
00063 
00064 #include <dev/rtc.h>
00065 
00066 static uint32_t epo_offs;
00067 
00073 
00085 time_t time(time_t * timer)
00086 {
00087     struct _tm *tm;
00088     /* Initially use internal seconds counter. */
00089     time_t r = epo_offs + NutGetSeconds();
00090 
00091     /* Try to get time from hardware clock. */
00092     if ((tm = malloc(sizeof(struct _tm))) != NULL) {
00093         tm->tm_isdst = -1;
00094         if (NutRtcGetTime(tm) == 0) {
00095             /* Success. */
00096             r = _mkgmtime(tm);
00097         }
00098         free(tm);
00099     }
00100 
00101     /* Return result. */
00102     if (timer) {
00103         *timer = r;
00104     }
00105     return r;
00106 }
00107 
00115 int stime(time_t * timer)
00116 {
00117     /* Try to set hardware clock. */
00118     NutRtcSetTime(gmtime(timer));
00119     /* Set internal seconds counter. */
00120     epo_offs = (uint32_t)(*timer) - NutGetSeconds();
00121 
00122     return 0;
00123 }
00124 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/