• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

libkholidays

lunarphase.cpp

Go to the documentation of this file.
00001 /*
00002   This file is part of the kholidays library.
00003 
00004   Copyright (c) 2004,2007 Allen Winter <winter@kde.org>
00005 
00006   Copyright (c) 1989, 1993  //krazy:exclude=copyright
00007   The Regents of the University of California.  All rights reserved.
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Library General Public
00011   License as published by the Free Software Foundation; either
00012   version 2 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017   GNU Library General Public License for more details.
00018 
00019   You should have received a copy of the GNU Library General Public License
00020   along with this library; see the file COPYING.LIB.  If not, write to the
00021   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00022   Boston, MA 02110-1301, USA.
00023 */
00024 
00025 #include "lunarphase.h"
00026 #include <config-libkholidays.h>
00027 
00028 #include <QDateTime>
00029 #include <KLocale>
00030 #include <KGlobal>
00031 
00032 using namespace LibKHolidays;
00033 
00034 LunarPhase::LunarPhase()
00035 {
00036 }
00037 
00038 LunarPhase::~LunarPhase()
00039 {
00040 }
00041 
00042 QString LunarPhase::phaseStr( const QDate &date ) const
00043 {
00044   return phaseName( phase( date ) );
00045 }
00046 
00047 QString LunarPhase::phaseName( LunarPhase::Phase phase )
00048 {
00049   switch ( phase ) {
00050   case NewMoon:
00051     return( i18n( "New Moon" ) );
00052   case FullMoon:
00053     return( i18n( "Full Moon" ) );
00054   case FirstQuarter:
00055     return( i18n( "First Quarter Moon" ) );
00056   case LastQuarter:
00057     return( i18n( "Last Quarter Moon" ) );
00058   default:
00059   case None:
00060     return QString();
00061   }
00062 }
00063 
00064 LunarPhase::Phase LunarPhase::phase( const QDate &date ) const
00065 {
00066   Phase retPhase = None;
00067 
00068   // compute percent-full for the middle of today and yesterday.
00069   QTime noontime( 12, 0, 0 );
00070   QDateTime today( date, noontime );
00071   double todayPer = percentFull( today.toTime_t() );
00072   QDateTime yesterday( date.addDays(-1), noontime );
00073   double yesterdayPer = percentFull( yesterday.toTime_t() );
00074 
00075   if ( ( todayPer < 0.50 ) && ( yesterdayPer > 0.50 ) ) {
00076      retPhase = NewMoon;
00077   } else if ( ( todayPer > 99.50 ) && ( yesterdayPer < 99.50 ) ) {
00078       retPhase = FullMoon;
00079   } else {
00080     // compute percent-full for the start of today.
00081     QTime sqt( 0, 0, 0 );
00082     QDateTime start( date, sqt );
00083     double startPer = percentFull( start.toTime_t() );
00084     // compute percent-full for the end of today.
00085     QTime eqt( 23, 59, 59 );
00086     QDateTime end( date, eqt );
00087     double endPer = percentFull( end.toTime_t() );
00088 
00089     if ( ( startPer <= 50 ) && ( endPer > 50 ) ) {
00090       retPhase = FirstQuarter;
00091     }
00092     if ( ( endPer <= 50 ) && ( startPer > 50 ) ) {
00093       retPhase = LastQuarter;
00094     }
00095     // Note: if you want to support crescent and gibbous phases then please
00096     //  read the source for the original BSD 'pom' program.
00097   }
00098   return( retPhase );
00099 }
00100 
00101 /*
00102  * Copyright (c) 1989, 1993
00103  *  The Regents of the University of California.  All rights reserved.
00104  *
00105  * This code is derived from software posted to USENET.
00106  *
00107  * Redistribution and use in source and binary forms, with or without
00108  * modification, are permitted provided that the following conditions
00109  * are met:
00110  * 1. Redistributions of source code must retain the above copyright
00111  *    notice, this list of conditions and the following disclaimer.
00112  * 2. Redistributions in binary form must reproduce the above copyright
00113  *    notice, this list of conditions and the following disclaimer in the
00114  *    documentation and/or other materials provided with the distribution.
00115  * 3. All advertising materials mentioning features or use of this software
00116  *    must display the following acknowledgement:
00117  *  This product includes software developed by the University of
00118  *  California, Berkeley and its contributors.
00119  * 4. Neither the name of the University nor the names of its contributors
00120  *    may be used to endorse or promote products derived from this software
00121  *    without specific prior written permission.
00122  *
00123  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00124  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00125  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00126  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00127  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00128  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00129  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00130  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00131  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00132  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00133  * SUCH DAMAGE.
00134  */
00135 
00136 #ifdef HAVE_SYS_CDEFS_H
00137 #include <sys/cdefs.h>
00138 #endif
00139 
00140 /*
00141  * Phase of the Moon.  Calculates the current phase of the moon.
00142  * Based on routines from `Practical Astronomy with Your Calculator',
00143  * by Duffett-Smith.  Comments give the section from the book that
00144  * particular piece of code was adapted from.
00145  *
00146  * -- Keith E. Brandt  VIII 1984
00147  *
00148  * Updated to the Third Edition of Duffett-Smith's book, Paul Janzen, IX 1998
00149  *
00150  */
00151 
00152 #include <ctype.h>
00153 #ifdef HAVE_ERR_H
00154 #include <err.h>
00155 #endif
00156 #include <math.h>
00157 #include <string.h>
00158 #include <stdlib.h>
00159 #include <time.h>
00160 #include <unistd.h>
00161 
00162 #ifndef PI
00163 #define PI    3.14159265358979323846
00164 #endif
00165 
00166 /*
00167  * The EPOCH in the third edition of the book is 1990 Jan 0.0 TDT.
00168  * In this program, we do not bother to correct for the differences
00169  * between UTC (as shown by the UNIX clock) and TDT.  (TDT = TAI + 32.184s;
00170  * TAI-UTC = 32s in Jan 1999.)
00171  */
00172 #define EPOCH_MINUS_1970        (20 * 365 + 5 - 1) /* 20 years, 5 leaps, back 1 day to Jan 0 */
00173 #define EPSILONg  279.403303    /* solar ecliptic long at EPOCH */
00174 #define RHOg      282.768422    /* solar ecliptic long of perigee at EPOCH */
00175 #define ECCEN     0.016713      /* solar orbit eccentricity */
00176 #define lzero     318.351648    /* lunar mean long at EPOCH */
00177 #define Pzero     36.340410     /* lunar mean long of perigee at EPOCH */
00178 #define Nzero     318.510107    /* lunar mean long of node at EPOCH */
00179 
00180 /*
00181  * percentFull --
00182  *  return phase of the moon as a percentage of full
00183  */
00184 double LunarPhase::percentFull( uint tmpt ) const
00185 {
00186   double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
00187   double A4, lprime, V, ldprime, D, Nm;
00188 
00189   double days;
00190   days = ( tmpt - EPOCH_MINUS_1970 * 86400 ) / 86400.0;
00191 
00192   N = 360 * days / 365.242191;                                 /* sec 46 #3 */
00193   adj360( &N );
00194   Msol = N + EPSILONg - RHOg;                                  /* sec 46 #4 */
00195   adj360( &Msol );
00196   Ec = 360 / PI * ECCEN * sin( degreesToRadians( Msol ) );     /* sec 46 #5 */
00197   LambdaSol = N + Ec + EPSILONg;                               /* sec 46 #6 */
00198   adj360( &LambdaSol );
00199   l = 13.1763966 * days + lzero;                               /* sec 65 #4 */
00200   adj360( &l );
00201   Mm = l - ( 0.1114041 * days ) - Pzero;                       /* sec 65 #5 */
00202   adj360( &Mm );
00203   Nm = Nzero - ( 0.0529539 * days );                           /* sec 65 #6 */
00204   adj360( &Nm );
00205   Ev = 1.2739 * sin( degreesToRadians( 2 * ( l - LambdaSol ) - Mm ) ); /* sec 65 #7 */
00206   Ac = 0.1858 * sin( degreesToRadians( Msol ) );               /* sec 65 #8 */
00207   A3 = 0.37 * sin( degreesToRadians( Msol ) );
00208   Mmprime = Mm + Ev - Ac - A3;                                 /* sec 65 #9 */
00209   Ec = 6.2886 * sin( degreesToRadians( Mmprime ) );            /* sec 65 #10 */
00210   A4 = 0.214 * sin( degreesToRadians( 2 * Mmprime ) );         /* sec 65 #11 */
00211   lprime = l + Ev + Ec - Ac + A4;                              /* sec 65 #12 */
00212   V = 0.6583 * sin( degreesToRadians( 2 * ( lprime - LambdaSol ) ) );/* sec 65 #13 */
00213   ldprime = lprime + V;                                        /* sec 65 #14 */
00214   D = ldprime - LambdaSol;                                     /* sec 67 #2 */
00215   D = 50.0 * ( 1 - cos( degreesToRadians( D ) ) );             /* sec 67 #3 */
00216   return D;
00217 }
00218 
00219 /*
00220  * degreesToRadians --
00221  *  convert degrees to radians
00222  */
00223 double LunarPhase::degreesToRadians( double degree ) const
00224 {
00225   return( degree * PI / 180 );
00226 }
00227 
00228 /*
00229  * adj360 --
00230  *  adjust value so 0 <= degree <= 360
00231  */
00232 void LunarPhase::adj360( double *degree ) const
00233 {
00234   for ( ;; ) {
00235     if ( *degree < 0 ) {
00236       *degree += 360;
00237     } else if ( *degree > 360 ) {
00238       *degree -= 360;
00239     } else {
00240       break;
00241     }
00242   }
00243 }

libkholidays

Skip menu "libkholidays"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal