Ignore:
Timestamp:
Dec 9, 2000, 8:19:42 PM (25 years ago)
Author:
umoeller
Message:

Major updates; timers, LVM, miscellaneous.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/datetime.c

    r8 r14  
    2525/*
    2626 *      This file Copyright (C) 1997-2000 Ulrich M”ller.
    27  *      This file is part of the XWorkplace source package.
    28  *      XWorkplace is free software; you can redistribute it and/or modify
     27 *      This file is part of the "XWorkplace helpers" source package.
     28 *      This is free software; you can redistribute it and/or modify
    2929 *      it under the terms of the GNU General Public License as published
    3030 *      by the Free Software Foundation, in version 2 as it comes in the
     
    6363const char  *pcszFormatTimestamp = "%4u%02u%02u%02u%02u%02u%";
    6464
     65ULONG G_ulDateScalarFirstCalled = 0;
     66
    6567/*
    6668 *@@ dtGetULongTime:
     
    7173 *      time in milliseconds.
    7274 *
    73  *      Warning: this does not handle day information. When called twice
    74  *      in between day changes, this returns incorrect information.
     75 *      Even though this does handle date information (i.e.
     76 *      will still return an increasing number when the
     77 *      clock switches from 23:59:59:9999 to 0:00:00:0000),
     78 *      this will not work forver after the first call.
     79 *      Here's the calculation:
     80 *
     81 +          1000 ms per second
     82 +            * 60 secs per minute
     83 +                * 60 minutes per hour
     84 +                    * 24 hours per day
     85 +                      = 86'400'000       after 23:59:59:9999
     86 *
     87 *      A ULONG can hold a max value of 4'294'967'295.
     88 *      So this overflows after 49.71... days.
     89 *
     90 *@@changed V0.9.7 (2000-12-05) [umoeller]: now handling date also
    7591 */
    7692
     
    7894{
    7995    DATETIME    dt;
     96    ULONG       ulTime,
     97                ulDateScalarPassed = 1;
    8098    DosGetDateTime(&dt);
    81     return (10*(dt.hundredths + 100*(dt.seconds + 60*(dt.minutes + 60*(dt.hours)))));
     99    ulTime = (10*(dt.hundredths + 100*(dt.seconds + 60*(dt.minutes + 60*(dt.hours)))));
     100
     101    if (G_ulDateScalarFirstCalled == 0)
     102    {
     103        // first call:
     104        G_ulDateScalarFirstCalled = dtDate2Scalar(dt.year,
     105                                                  dt.month,
     106                                                  dt.day);
     107    }
     108    else
     109    {
     110        // not first call:
     111        ULONG ulDateScalarNow = dtDate2Scalar(dt.year,
     112                                              dt.month,
     113                                              dt.day);
     114        // calculate days passed since first call;
     115        // this should be 1 if the date hasn't changed
     116        ulDateScalarPassed = (G_ulDateScalarFirstCalled - ulDateScalarNow) + 1;
     117    }
     118
     119    return (ulTime * ulDateScalarPassed);
    82120}
    83121
     
    147185
    148186/*
     187**
     188**
     189**   day:    day of month
     190**   mon:    month (1-12)
     191**   yr:     year
     192**
     193**
     194*/
     195
     196/*
     197 *@@ dtDayOfWeek:
     198 *      returns an integer that represents the day of
     199 *      the week for the date passed as parameters.
     200 *
     201 *      Returns 0-6 where 0 is sunday.
     202 *
     203 *@@added V0.9.7 (2000-12-05) [umoeller]
     204 */
     205
     206ULONG dtDayOfWeek(ULONG day,
     207                  ULONG mon,    // 1-12
     208                  ULONG yr)
     209{
     210    int dow;
     211
     212    if (mon <= 2)
     213    {
     214          mon += 12;
     215          yr -= 1;
     216    }
     217    dow = (   day
     218            + mon * 2
     219            + ((mon + 1) * 6) / 10
     220            + yr
     221            + yr / 4
     222            - yr / 100
     223            + yr / 400
     224            + 2);
     225    dow = dow % 7;
     226    return ((dow ? dow : 7) - 1);
     227}
     228
     229/*
    149230 *@@ dtIsLeapYear:
    150231 *      returns TRUE if yr is a leap year.
     
    164245/*
    165246 *@@ dtMonths2Days:
    166  *      returns the no. of days for month.
     247 *      returns the no. of days for the beginning
     248 *      of "month" (starting from 1).
     249 *
     250 *      For example, if you pass 1 (for january),
     251 *      you get 0 because there's no days at jan 1st
     252 *      yet.
     253 *
     254 *      If you pass 2 (for february), you get 31.
     255 *
     256 *      If you pass 3 (for march), you get 61.
     257 *
     258 *      This is useful for computing a day index
     259 *      for a given month/day pair. Pass the month
     260 *      in here and add (day-1); for march 3rd,
     261 *      you then get 63.
    167262 *
    168263 *      (c) Ray Gardner.
     
    171266unsigned dtMonths2Days(unsigned month)
    172267{
    173    return (month * 3057 - 3007) / 100;
     268    return (month * 3057 - 3007) / 100;
    174269}
    175270
     
    228323   *pyr = n;
    229324   n = (unsigned)(scalar - dtYears2Days(n-1));
    230    if ( n > 59 ) {                       /* adjust if past February */
     325   if ( n > 59 )    /* adjust if past February */
     326   {
    231327      n += 2;
    232328      if ( dtIsLeapYear(*pyr) )
     
    237333}
    238334
    239 
     335/*
     336 *@@ dtIsValidDate:
     337 *      returns TRUE if the given date is valid.
     338 *
     339 *@@added V0.9.7 (2000-12-05) [umoeller]
     340 */
     341
     342BOOL dtIsValidDate(LONG day,      // in: day (1-31)
     343                   LONG month,    // in: month (1-12)
     344                   ULONG year)    // in: year (e.g. 1999)
     345{
     346    BOOL brc = FALSE;
     347    if (day > 0)
     348    {
     349        switch( month )
     350        {
     351            case 1  :
     352            case 3  :
     353            case 5  :
     354            case 7  :
     355            case 8  :
     356            case 10 :
     357            case 12 :
     358                if (day <= 31)
     359                    brc = TRUE;
     360            break;
     361
     362            case 4  :
     363            case 6  :
     364            case 9  :
     365            case 11 :
     366                if (day <= 30)
     367                    brc = TRUE;
     368            break;
     369
     370            case 2 :
     371                if (day < 29)
     372                    brc = TRUE;
     373                else
     374                    if (day == 29)
     375                        if (dtIsLeapYear(year))
     376                            return 1 ;
     377        }
     378    }
     379    return (brc);
     380}
     381
Note: See TracChangeset for help on using the changeset viewer.