This page provides source code that solves some common programming problems.
All the code has been written, compiled and tested using Microsoft Visual C++ 5.0.
Date Parsing
This code accepts a single free format string that is intended to represent a date,
perhaps entered by a user. It will then attempt to convert that string into a valid
internal representation of a date, that is, a time_t value.
For example,
- “221099” will be interpreted as the 22nd of October 1999
- “15-Jan” will be interpreted as the 15th of January 2000
- “1/5/1901” will be interpreted as the 1st of May 1901
These following constants govern the behaviour when an ambiguous date is provided:
- CENTURY_BREAK — determines the point in time at which a two digit
year starts being interpreted as a year in the past rather than in the future.
- DATE_ORDER — determines the order in which day, month and year
components must be entered.
See the comments in the source code for more information.
Notes
- This code only caters for dates. Times are not interpreted.
- Other date and time parsing code written in C can be found on the
comp.lang.c newsgroup. The names of some other source files and
functions include parsdate.c, parse.c, strptime.c and makeTime.
Bit Manipulation
This code makes it possible to set or get a sequence of up to 32 bits in a string of
unsigned characters, ie. bytes. The following two functions are available:
- SetBits — must be provided with a string (unsigned char *), a bit offset
which is zero based, the number of bits to set and an unsigned long used
as the source of the bits to set.
- GetBits — must be provided with a string (unsigned char *), a bit offset
which is zero based, the number of bits to get and an unsigned long which
will be filled with the bits from the string.
Notes
- This code only allows for setting or getting of up to 32 bits at once.
- No account has been taken of little or big endianness.
Return to Michael’s Home Page