25 April 2005

Representing time in C, C++, SQL, MFC, and Java

Options available in representing date and time values:

18 Jan 2006: Added java.util.Date
5 Apr 2006: Added Data column for types

(Links are to MSDN unless otherwise noted.)

Data Representations

Type Domain Header Data
tm C (struct) time.h
  • tm_sec - Seconds after minute (0 59).
  • tm_min - Minutes after hour (0 59).
  • tm_hour - Hours after midnight (0 23).
  • tm_mday - Day of month (1 31).
  • tm_mon - Month (0 11; January = 0).
  • tm_year - Year (current year minus 1900).
  • tm_wday - Day of week (0 6; Sunday = 0).
  • tm_yday - Day of year (0 365; January 1 = 0).
  • tm_isdst - Positive value if daylight saving time is in effect; 0 if daylight saving time is not in effect; negative value if status of daylight saving time is unknown.
time_t, __time64_t C (long) time.h
SYSTEMTIME Win32 (C struct) winbase.h
  • wYear - The current year.
  • wMonth - The current month; January is 1.
  • wDayOfWeek - The current day of the week; Sunday is 0, Monday is 1, and so on.
  • wDay - The current day of the month.
  • wHour - The current hour.
  • wMinute - The current minute.
  • wSecond - The current second.
  • wMilliseconds - The current millisecond.
FILETIME Win32 (C struct) winbase.h
  • dwLowDateTime - Specifies the low 32 bits of the file time.
  • wHighDateTime - Specifies the high 32 bits of the file time.
DATE COM (double) wtypes.h
DBTIMESTAMP COM (C struct) oledb.h
  • year - The year (0 to 9999) is measured from 0 A.D.
  • month - The month ranges from 1 to 12 representing January through December.
  • day - The day ranges from 1 to a maximum of 31, depending on the number of days in the month.
  • hour - The hour ranges from 0 to 23.
  • minute - The minute ranges from 0 to 59.
  • second - The second ranges from 0 to 59.
  • fraction - The fraction represents billionths of a second ranging from 0 to 999,999,999.
CTime MFC (class, stored as time_t member) afx.h
COleDateTime MFC (class, stored as DATE member) afxdisp.h
java.util.Date Java

Conversion

From Types ->
To Types V
tm time_t, __time64_t SYSTEM TIME FILE TIME DATE DBTIME STAMP CTime COle DateTime java.util. Date
tm gmtime, localtime, _gmtime64, _localtime64 X
time_t, __time64_t mktime, _mktime64 time, _time64 (current system time) X Date. getTime() / 1000
SYSTEM TIME FileTime ToSystemTime
FILETIME SystemTime ToFileTime
DATE
DBTIME STAMP
CTime X X X X
COle DateTime X X X X X
java.util. Date new Date(time_t * 1000)

Limits

SQL information taken from How to search for date and time values using Microsoft SQL Server 2000 by Bryan Syverson and MySQL Reference Manual :: 11.3.1 The DATETIME, DATE, and TIMESTAMP Types.

Type Minimum (YYYY-MM-DD) Maximum (YYYY-MM-DD) Precision
SQL datetime 1753-01-01 (SQL Server), 1000-01-01 (MySQL) 9999-12-31 To the nearest 3.33 milliseconds
SQL smalldatetime 1900-01-01 2079-06-06 23:59 (1 minute until midnight) To the nearest minute
time_t/tm, __time64_t 1970-01-01 00:00:00 2038-01-18 19:14:07, 3000-12-31 23:59:59 seconds from the minimum time
FILETIME 1601-01-01
DATE Zero is 1899-12-31 00:00:00
CTime 1970-01-01 2038-01-18
COleDateTime 0100-01-01 9999-12-31
java.util.Date zero is 1970-01-01 00:00:00, milliseconds from zero

TODO: Add relevant functions, facets, and boost utilities.

[ posted by sstrader on 25 April 2005 at 10:41:17 AM in Programming ]