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.5  2007/06/03 08:49:56  haraldkipp
00036  * Bug #1724015 fixed. Simplified code in time().
00037  *
00038  * Revision 1.4  2006/10/05 17:20:13  haraldkipp
00039  * Standard C functions now support hardware RTC.
00040  * Ugly type conversion replaced.
00041  *
00042  * Revision 1.3  2003/12/19 22:26:38  drsung
00043  * Dox written.
00044  *
00045  * Revision 1.2  2003/11/26 11:14:32  haraldkipp
00046  * Portability issues
00047  *
00048  * Revision 1.1  2003/11/24 18:07:37  drsung
00049  * first release
00050  *
00051  *
00052  */
00053 
00054 #include <time.h>
00055 #include <sys/timer.h>
00056 #include <sys/atom.h>
00057 #include <stdlib.h>
00058 
00059 #include <dev/rtc.h>
00060 
00061 static u_long epo_offs;
00062 
00068 
00080 time_t time(time_t * timer)
00081 {
00082     struct _tm *tm;
00083     /* Initially use internal seconds counter. */
00084     time_t r = epo_offs + NutGetSeconds();
00085 
00086     /* Try to get time from hardware clock. */
00087     if ((tm = malloc(sizeof(struct _tm))) != NULL) {
00088         tm->tm_isdst = -1;
00089         if (NutRtcGetTime(tm) == 0) {
00090             /* Success. */
00091             r = _mkgmtime(tm);
00092         }
00093         free(tm);
00094     }
00095 
00096     /* Return result. */
00097     if (timer) {
00098         *timer = r;
00099     }
00100     return r;
00101 }
00102 
00110 int stime(time_t * timer)
00111 {
00112     /* Try to set hardware clock. */
00113     NutRtcSetTime(gmtime(timer));
00114     /* Set internal seconds counter. */
00115     epo_offs = (u_long)(*timer) - NutGetSeconds();
00116 
00117     return 0;
00118 }
00119 

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