source: trunk/src/helpers/datetime.c@ 9

Last change on this file since 9 was 8, checked in by umoeller, 25 years ago

Initial checkin of helpers code which used to be in WarpIN.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1
2/*
3 *@@sourcefile datetime.c:
4 * contains various date and time helper functions.
5 * Some functions in here are OS/2-specific, others
6 * are plain C code.
7 *
8 * Functions marked with (C) Ray Gardner are from
9 * "scaldate.c":
10 * scalar date routines -- public domain by Ray Gardner
11 * These will work over the range 1/01/01 thru 14699/12/31
12 *
13 * Usage: All OS/2 programs.
14 *
15 * Function prefixes (new with V0.81):
16 * -- dat* date/time helper functions
17 *
18 * Note: Version numbering in this file relates to XWorkplace version
19 * numbering.
20 *
21 *@@header "helpers\datetime.h"
22 *@@added V0.9.0 [umoeller]
23 */
24
25/*
26 * 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
29 * it under the terms of the GNU General Public License as published
30 * by the Free Software Foundation, in version 2 as it comes in the
31 * "COPYING" file of the XWorkplace main distribution.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 */
37
38#define OS2EMX_PLAIN_CHAR
39 // this is needed for "os2emx.h"; if this is defined,
40 // emx will define PSZ as _signed_ char, otherwise
41 // as unsigned char
42
43#include <os2.h>
44
45#include <stdio.h>
46
47#include "setup.h" // code generation and debugging options
48
49#include "helpers\datetime.h"
50
51#pragma hdrstop
52
53/*
54 *@@category: Helpers\C helpers\Date/time helpers
55 */
56
57/*******************************************************************
58 * *
59 * Private declarations *
60 * *
61 ******************************************************************/
62
63const char *pcszFormatTimestamp = "%4u%02u%02u%02u%02u%02u%";
64
65/*
66 *@@ dtGetULongTime:
67 * this returns the current time as a ULONG value (in milliseconds).
68 * Useful for stopping how much time the machine has spent in
69 * a certain function. To do this, simply call this function twice,
70 * and subtract the two values, which will give you the execution
71 * time in milliseconds.
72 *
73 * Warning: this does not handle day information. When called twice
74 * in between day changes, this returns incorrect information.
75 */
76
77ULONG dtGetULongTime(VOID)
78{
79 DATETIME dt;
80 DosGetDateTime(&dt);
81 return (10*(dt.hundredths + 100*(dt.seconds + 60*(dt.minutes + 60*(dt.hours)))));
82}
83
84/*
85 *@@ dtCreateFileTimeStamp:
86 * this creates a time stamp string in pszTimeStamp
87 * from the given FDATE and FTIME structures (which
88 * are, for example, used in the FILESTATUS3 structure
89 * returned by DosQueryPathInfo).
90 *
91 * The time stamp string is exactly 15 bytes in length
92 * (including the terminating null byte) and has the
93 * following format:
94 + YYYYMMDDhhmmss
95 * (being year, mondth, day, hours, minutes, seconds).
96 * Your buffer must be large enough for that, this is
97 * not checked.
98 *
99 * This time stamp can be used to compare two dates
100 * simply by calling strcmp.
101 *
102 * Note that since FTIME only has a two-seconds resolution,
103 * the seconds part of the time stamp will have that too.
104 *
105 * This returns the string length (excluding the null
106 * terminator), which should be 14.
107 *
108 *@@added V0.9.0 [umoeller]
109 */
110
111int dtCreateFileTimeStamp(PSZ pszTimeStamp, // out: time stamp
112 FDATE* pfdate, // in: date
113 FTIME* pftime) // in: time
114{
115 return (sprintf(pszTimeStamp,
116 pcszFormatTimestamp,
117 pfdate->year + 1980,
118 pfdate->month,
119 pfdate->day,
120 pftime->hours,
121 pftime->minutes,
122 pftime->twosecs * 2));
123}
124
125/*
126 *@@ dtCreateDosTimeStamp:
127 * just like dtCreateFileTimeStamp,
128 * except that this takes a DATETIME
129 * structure as input. The time stamp
130 * string is exactly the same.
131 *
132 *@@added V0.9.0 [umoeller]
133 */
134
135int dtCreateDosTimeStamp(PSZ pszTimeStamp,
136 DATETIME* pdt)
137{
138 return (sprintf(pszTimeStamp,
139 pcszFormatTimestamp,
140 pdt->year,
141 pdt->month,
142 pdt->day,
143 pdt->hours,
144 pdt->minutes,
145 pdt->seconds));
146}
147
148/*
149 *@@ dtIsLeapYear:
150 * returns TRUE if yr is a leap year.
151 *
152 * (c) Ray Gardner.
153 */
154
155int dtIsLeapYear(unsigned yr)
156{
157 return ( (yr % 400 == 0)
158 || ( (yr % 4 == 0)
159 && (yr % 100 != 0)
160 )
161 );
162}
163
164/*
165 *@@ dtMonths2Days:
166 * returns the no. of days for month.
167 *
168 * (c) Ray Gardner.
169 */
170
171unsigned dtMonths2Days(unsigned month)
172{
173 return (month * 3057 - 3007) / 100;
174}
175
176/*
177 *@@ dtYears2Days:
178 * converts a year to the no. of days passed.
179 *
180 * (c) Ray Gardner.
181 */
182
183long dtYears2Days (unsigned yr)
184{
185 return ( yr * 365L
186 + yr / 4
187 - yr / 100
188 + yr / 400);
189}
190
191/*
192 *@@ dtDate2Scalar:
193 * returns a scalar (i.e. the no. of days) for
194 * the given date.
195 *
196 * (c) Ray Gardner.
197 */
198
199long dtDate2Scalar(unsigned yr, // in: year (e.g. 1999)
200 unsigned mo, // in: month (1-12)
201 unsigned day) // in: day (1-31)
202{
203 long scalar;
204 scalar = day + dtMonths2Days(mo);
205 if ( mo > 2 ) /* adjust if past February */
206 scalar -= dtIsLeapYear(yr) ? 1 : 2;
207 yr--;
208 scalar += dtYears2Days(yr);
209 return scalar;
210}
211
212/*
213 *@@ dtScalar2Date:
214 *
215 *
216 * (c) Ray Gardner.
217 */
218
219void dtScalar2Date(long scalar, // in: date scalar
220 unsigned *pyr, // out: year (e.g. 1999)
221 unsigned *pmo, // out: month (1-12)
222 unsigned *pday) // out: day (1-31)
223{
224 unsigned n; /* compute inverse of dtYears2Days() */
225
226 for ( n = (unsigned)((scalar * 400L) / 146097); dtYears2Days(n) < scalar;)
227 n++; /* 146097 == dtYears2Days(400) */
228 *pyr = n;
229 n = (unsigned)(scalar - dtYears2Days(n-1));
230 if ( n > 59 ) { /* adjust if past February */
231 n += 2;
232 if ( dtIsLeapYear(*pyr) )
233 n -= n > 62 ? 1 : 2;
234 }
235 *pmo = (n * 100 + 3007) / 3057; /* inverse of dtMonths2Days() */
236 *pday = n - dtMonths2Days(*pmo);
237}
238
239
Note: See TracBrowser for help on using the repository browser.