LCOV - code coverage report
Current view: top level - drivers/rtc - rtc-lib.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 48 60.4 %
Date: 2014-02-18 Functions: 5 7 71.4 %
Branches: 11 34 32.4 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * rtc and date/time utility functions
       3                 :            :  *
       4                 :            :  * Copyright (C) 2005-06 Tower Technologies
       5                 :            :  * Author: Alessandro Zummo <a.zummo@towertech.it>
       6                 :            :  *
       7                 :            :  * based on arch/arm/common/rtctime.c and other bits
       8                 :            :  *
       9                 :            :  * This program is free software; you can redistribute it and/or modify
      10                 :            :  * it under the terms of the GNU General Public License version 2 as
      11                 :            :  * published by the Free Software Foundation.
      12                 :            : */
      13                 :            : 
      14                 :            : #include <linux/module.h>
      15                 :            : #include <linux/rtc.h>
      16                 :            : 
      17                 :            : static const unsigned char rtc_days_in_month[] = {
      18                 :            :         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
      19                 :            : };
      20                 :            : 
      21                 :            : static const unsigned short rtc_ydays[2][13] = {
      22                 :            :         /* Normal years */
      23                 :            :         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
      24                 :            :         /* Leap years */
      25                 :            :         { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
      26                 :            : };
      27                 :            : 
      28                 :            : #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
      29                 :            : 
      30                 :            : /*
      31                 :            :  * The number of days in the month.
      32                 :            :  */
      33                 :          0 : int rtc_month_days(unsigned int month, unsigned int year)
      34                 :            : {
      35 [ -  - ][ #  # ]:          8 :         return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
      36                 :            : }
      37                 :            : EXPORT_SYMBOL(rtc_month_days);
      38                 :            : 
      39                 :            : /*
      40                 :            :  * The number of days since January 1. (0 to 365)
      41                 :            :  */
      42                 :          0 : int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
      43                 :            : {
      44                 :          0 :         return rtc_ydays[is_leap_year(year)][month] + day-1;
      45                 :            : }
      46                 :            : EXPORT_SYMBOL(rtc_year_days);
      47                 :            : 
      48                 :            : /*
      49                 :            :  * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
      50                 :            :  */
      51                 :          0 : void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
      52                 :            : {
      53                 :            :         unsigned int month, year;
      54                 :            :         int days;
      55                 :            : 
      56                 :          4 :         days = time / 86400;
      57                 :          4 :         time -= (unsigned int) days * 86400;
      58                 :            : 
      59                 :            :         /* day of the week, 1970-01-01 was a Thursday */
      60                 :          4 :         tm->tm_wday = (days + 4) % 7;
      61                 :            : 
      62                 :          4 :         year = 1970 + days / 365;
      63                 :          4 :         days -= (year - 1970) * 365
      64                 :          4 :                 + LEAPS_THRU_END_OF(year - 1)
      65                 :            :                 - LEAPS_THRU_END_OF(1970 - 1);
      66         [ -  + ]:          4 :         if (days < 0) {
      67                 :            :                 year -= 1;
      68                 :          0 :                 days += 365 + is_leap_year(year);
      69                 :            :         }
      70                 :          4 :         tm->tm_year = year - 1900;
      71                 :          4 :         tm->tm_yday = days + 1;
      72                 :            : 
      73         [ +  - ]:          7 :         for (month = 0; month < 11; month++) {
      74                 :            :                 int newdays;
      75                 :            : 
      76                 :          3 :                 newdays = days - rtc_month_days(month, year);
      77         [ +  - ]:          3 :                 if (newdays < 0)
      78                 :            :                         break;
      79                 :            :                 days = newdays;
      80                 :            :         }
      81                 :          0 :         tm->tm_mon = month;
      82                 :          0 :         tm->tm_mday = days + 1;
      83                 :            : 
      84                 :          0 :         tm->tm_hour = time / 3600;
      85                 :          0 :         time -= tm->tm_hour * 3600;
      86                 :          0 :         tm->tm_min = time / 60;
      87                 :          0 :         tm->tm_sec = time - tm->tm_min * 60;
      88                 :            : 
      89                 :          0 :         tm->tm_isdst = 0;
      90                 :          0 : }
      91                 :            : EXPORT_SYMBOL(rtc_time_to_tm);
      92                 :            : 
      93                 :            : /*
      94                 :            :  * Does the rtc_time represent a valid date/time?
      95                 :            :  */
      96                 :          0 : int rtc_valid_tm(struct rtc_time *tm)
      97                 :            : {
      98         [ +  - ]:          1 :         if (tm->tm_year < 70
      99         [ +  - ]:          1 :                 || ((unsigned)tm->tm_mon) >= 12
     100         [ +  - ]:          1 :                 || tm->tm_mday < 1
     101         [ #  # ]:          0 :                 || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
     102         [ +  - ]:          1 :                 || ((unsigned)tm->tm_hour) >= 24
     103         [ +  - ]:          1 :                 || ((unsigned)tm->tm_min) >= 60
     104         [ +  - ]:          1 :                 || ((unsigned)tm->tm_sec) >= 60)
     105                 :            :                 return -EINVAL;
     106                 :            : 
     107                 :          1 :         return 0;
     108                 :            : }
     109                 :            : EXPORT_SYMBOL(rtc_valid_tm);
     110                 :            : 
     111                 :            : /*
     112                 :            :  * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
     113                 :            :  */
     114                 :          0 : int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
     115                 :            : {
     116                 :          2 :         *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
     117                 :          6 :                         tm->tm_hour, tm->tm_min, tm->tm_sec);
     118                 :          2 :         return 0;
     119                 :            : }
     120                 :            : EXPORT_SYMBOL(rtc_tm_to_time);
     121                 :            : 
     122                 :            : /*
     123                 :            :  * Convert rtc_time to ktime
     124                 :            :  */
     125                 :          0 : ktime_t rtc_tm_to_ktime(struct rtc_time tm)
     126                 :            : {
     127                 :            :         time_t time;
     128                 :          1 :         rtc_tm_to_time(&tm, &time);
     129                 :          2 :         return ktime_set(time, 0);
     130                 :            : }
     131                 :            : EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
     132                 :            : 
     133                 :            : /*
     134                 :            :  * Convert ktime to rtc_time
     135                 :            :  */
     136                 :          0 : struct rtc_time rtc_ktime_to_tm(ktime_t kt)
     137                 :            : {
     138                 :            :         struct timespec ts;
     139                 :            :         struct rtc_time ret;
     140                 :            : 
     141                 :          1 :         ts = ktime_to_timespec(kt);
     142                 :            :         /* Round up any ns */
     143         [ -  + ]:          1 :         if (ts.tv_nsec)
     144                 :          0 :                 ts.tv_sec++;
     145                 :          1 :         rtc_time_to_tm(ts.tv_sec, &ret);
     146                 :          1 :         return ret;
     147                 :            : }
     148                 :            : EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);
     149                 :            : 
     150                 :            : MODULE_LICENSE("GPL");

Generated by: LCOV version 1.9