Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-08

date handling routines

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Hey guys,
Wondering if there was already a unit available that could tell me the day of week a certain date was. An example perhaps would be:

function GetDayOfWeek(date:datetimerecord):integer;

where result would be 1-7 (depending on the date).

If not, then I will just write my own, but why reinvent the wheel, eh?

- Matt

Marco van de Voort
to Unknown author

[ Charset ISO-8859-1 unsupported, converting... ]

Show quoted text 8 lines
Hey guys, Wondering if there was already a unit available that could tell me the day of week a certain date was. An example perhaps would be: function GetDayOfWeek(date:datetimerecord):integer; where result would be 1-7 (depending on the date). If not, then I will just write my own, but why reinvent the wheel, eh?

I can't remember the exact name, but some function like that is in sysutils.

Check the documentation for more details.

Unknown author
Wondering if there was already a unit available that could tell me the day of week a certain date was. An example perhaps would be:

Try passing this function a packed longint and it should return the weekday in a 0..6 sequence, with 0 being Sunday. Note I didn't compile this so it might need to be tweaked. :-)

Jim

Function DayOfWeek (DateToCheck : longint) : byte
const
  FileDays : array[1..12] of byte = (6,2,2,5,0,3,5,1,4,6,2,4);
var
  DateAndTime : datetime;
  AdjustedYear : integer;
begin
  unpacktime (DateToCheck,DateAndTime);
  //  leap year adjustment
  if DateAndTime.month > 2 then AdjustedYear := DateAndTime.year else
AdjustedYear := pred (DateAndTime.year);
  WeekDay := ((DateAndTime.day + (AdjustedYear div 4) - (AdjustedYear div
100) + (AdjustedYear div 400) + DateAndTime.year + FileDays
[DateAndTime.month]) mod 7);
end;
Unknown author
to Unknown author: Wondering if there was already a unit available that could tell me the day of week a…
Show quoted text 8 lines
Hey guys, Wondering if there was already a unit available that could tell me the day of week a certain date was. An example perhaps would be: function GetDayOfWeek(date:datetimerecord):integer; where result would be 1-7 (depending on the date). If not, then I will just write my own, but why reinvent the wheel, eh?

The sysutils unit contains a function

  function DayOfWeek(DateTime: TDateTime): integer;

You'll need to convert your date to TDateTime format, though. The sysutils unit contains routines for this as well.

Michael.

Gabor DEAK JAHN
to Unknown author: Wondering if there was already a unit available that could tell me the day of week a…

At 8/30/01 01:46 PM, you wrote:

Matt,

Wondering if there was already a unit available that could tell me the day of week a certain date was. An example perhaps would be:

There is one such function in the SysUtils unit already but here you have the implementation of the original and famous Zeller closed-formula algorithm (Zeller was a mathematician way before the computer era). The wonderful part is that it doesn't need a table to know the length of the individual months but uses a multiplication and a division to calculate it... It returns 0 for Saturday, 1 for Sunday, 2 for Monday, etc. The formula is universal, it works across all centuries.

function Date2DOW (Y, D, M : cardinal) : cardinal;
var
   Century : cardinal;
begin
   Century := Y div 100;
   Y := Y mod 100;
   if M < 3 then
     begin dec (Y); inc (M, 12); end;
   Date2DOW := (((M + 1) * 26 div 10) + D + Y + (Y div 4) + (Century div 4)
- 2 * Century) mod 7;
end;

Bye,
Gábor

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
The Citroën BX Do-It-Yourself Site (with XM Pages): www.tramontana.co.hu/citroen
E-mail:

Brad Campbell
to Gabor DEAK JAHN

G'day all..

Using fpc in OBJPAS mode I manipulate variables like this..

Type
	TTheRec = Record
	A : String[80];
	B : Cardinal;
	End;
	PTheRec = ^TTheRec;
Var
	DynArray : PTheRec;
Begin
GetMem(DynArray,SizeOf(TTheRec)*30);
DynArray[1].A := 'Hello';
DynArray[2].B := 42;
FreeMem(DynArray,SizeOf(TTheRec)*30);
End;

Under Mode Delphi, I have to do this..

Type
	TTheRec = Record
	A : String[80];
	B : Cardinal;
	End;
	PTheRec = Array [1..(MaxInt Div SizeOf(TTheRec))-1] of TTheRec;
Var
	DynArray : ^PTheRec;
Begin
GetMem(DynArray,SizeOf(TTheRec)*30);
DynArray[1].A := 'Hello';
DynArray[2].B := 42;
FreeMem(DynArray,SizeOf(TTheRec)*30);
End;

Is this the best way ?

Brad.... /"\ Save the Forests \ / ASCII RIBBON CAMPAIGN Burn a Greenie. X AGAINST HTML MAIL / \
Michael Van Canneyt
to Brad Campbell
Brad Campbell wrote 37 lines
G'day all.. Using fpc in OBJPAS mode I manipulate variables like this.. Type TTheRec = Record A : String[80]; B : Cardinal; End; PTheRec = ^TTheRec; Var DynArray : PTheRec; Begin GetMem(DynArray,SizeOf(TTheRec)*30); DynArray[1].A := 'Hello'; DynArray[2].B := 42; FreeMem(DynArray,SizeOf(TTheRec)*30); End; Under Mode Delphi, I have to do this.. Type TTheRec = Record A : String[80]; B : Cardinal; End; PTheRec = Array [1..(MaxInt Div SizeOf(TTheRec))-1] of TTheRec; Var DynArray : ^PTheRec; Begin GetMem(DynArray,SizeOf(TTheRec)*30); DynArray[1].A := 'Hello'; DynArray[2].B := 42; FreeMem(DynArray,SizeOf(TTheRec)*30); End; Is this the best way ?

It depends. If you're using the 1.1 compiler, the best way is to use dynamic arrays:

Type
 TheRec = Record
   A : String[80];
   B : Cardinal;
 End;
Var
  DynArray :  array of TTheRec; // No length specified
Begin
  SetLength(DynArray,30);
  DynArray[1].A := 'Hello';
  DynArray[2].B := 42;
End; // Compiler disposes of dynarray by itself.

For version 1.0 I would use the objfpc way, as it resembles more the dynamic array method.

Michael.

Brad Campbell
to Michael Van Canneyt
Michael Van Canneyt wrote 24 lines
It depends. If you're using the 1.1 compiler, the best way is to use dynamic arrays: Type TheRec = Record A : String[80]; B : Cardinal; End; Var DynArray : array of TTheRec; // No length specified Begin SetLength(DynArray,30); DynArray[1].A := 'Hello'; DynArray[2].B := 42; End; // Compiler disposes of dynarray by itself. For version 1.0 I would use the objfpc way, as it resembles more the dynamic array method. Michael.

Problem being, it will not let me use the objfpc way with mode delphi.. I use that where I can, but some of my software needs delphi mode set, though I can't remember why for the moment..

Brad.... /"\ Save the Forests \ / ASCII RIBBON CAMPAIGN Burn a Greenie. X AGAINST HTML MAIL / \