Recent

Author Topic: [SOLVED] Epoch seconds conversion to day of the week fails?  (Read 1863 times)

Wings2018

  • New Member
  • *
  • Posts: 16
Hello,

I'm facing a topic on the determination of the day of the week based on epoch seconds.
Below a simple program which outputs following:

Epoch seconds (s) = 1684793346
Name of day       = 4

Epoch seconds (s) = 1684793347
Name of day       = 5

Epoch seconds value of 1684793346 reflects Monday 22-May-2023 22:09:06 (checked on https://www.epochconverter.com) and that's about the moment I wrote this thread.

Topic 1:
Day of the week should be 1 which equals Monday. Why isn't this the case?

Topic 2:
If I add 1 s to the initial epoch value the day of the week value jumps by one (to 5) which I believe is incorrect. Only after adding 86,400 s to any second in a day the day of the week should be increased by 1.

Both variables are of the type tDateTime so I suppose they're interchangeable. Do I misunderstand the epoch / name of day functionality?

Program:

program EpochValidation (input,output);

uses
  crt, dateutils, dos, math, strutils, sysutils, video;
var
  i64EpochSeconds: int64;

{====================================================}

procedure DetermineNameOfDay(i64EpochSeconds: int64);
var
  sConvert: ansistring;
  wNameOfDay: word;
begin

  str(i64EpochSeconds,sConvert);
  writeln('Epoch seconds: ' + sConvert);

  wNameOfDay:= DayOfTheWeek(i64EpochSeconds);
  str(wNameOfDay,sConvert);
  writeln('Name of day: ' + sConvert);

  writeln();

end;

{====================================================}

{ start of program }

begin
  clrscr();

  i64EpochSeconds:= DateTimeToUnix(Now);

  DetermineNameOfDay(i64EpochSeconds);

  i64EpochSeconds:= i64EpochSeconds + 1;

  DetermineNameOfDay(i64EpochSeconds);

  repeat
  until keypressed;

end.
« Last Edit: October 18, 2023, 09:01:31 pm by Wings2018 »

dseligo

  • Hero Member
  • *****
  • Posts: 1219
Re: Epoch seconds conversion to day of the week fails?
« Reply #1 on: May 23, 2023, 01:20:26 am »
DayOfTheWeek function uses TDateTime as parameter.

Use UnixToDateTime (https://www.freepascal.org/docs-html/rtl/dateutils/unixtodatetime.html) to convert epoch seconds to TDateTime.

TRon

  • Hero Member
  • *****
  • Posts: 2503
Re: Epoch seconds conversion to day of the week fails?
« Reply #2 on: May 23, 2023, 02:57:16 am »
As stated by dseligo.

small example that might be helpful:
Code: Pascal  [Select][+][-]
  1. uses
  2.   Sysutils, DateUtils;
  3.  
  4. function NameOfDay(DateTime: TDateTime): string; overload;
  5. begin
  6.   result := FormatDateTime('dddd', DateTime);
  7. end;
  8.  
  9. function NameOfDay(EPoch: int64): string; overload;
  10. begin
  11.   result := NameOfDay(UnixToDateTime(EPoch));
  12. end;
  13.  
  14. function FormatEpoch(fmt: string; Epoch: int64): string;
  15. begin
  16.   result := FormatDateTime(fmt, UnixToDateTime(EPoch));
  17. end;
  18.  
  19. var
  20.   epoch: int64;
  21.   fmt  : string = 'mmmm d, yyyy  hh":"nn":"ss';
  22. begin
  23.   epoch := DateTimeToUnix(Now);
  24.   WriteLn('Name of day for date ', FormatEpoch(fmt, epoch), ' = ', NameOfDay(epoch));
  25.  
  26.   epoch := 1684793346;
  27.   WriteLn('Name of day for date ', FormatEpoch(fmt, epoch), ' = ', NameOfDay(epoch));
  28.  
  29.   epoch := 1684793347;
  30.   WriteLn('Name of day for date ', FormatEpoch(fmt, epoch), ' = ', NameOfDay(epoch));
  31. end.
  32.  

One thing I did notice though is that the names of the days that are stored in DefaultFormatSettings uses another starting day (Sunday instead of Monday).

Is that default or does that depend on system settings as well (on some OS it is possible to set your preferred starting day of the week) ?

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Epoch seconds conversion to day of the week fails?
« Reply #3 on: May 23, 2023, 08:23:50 am »
One thing I did notice though is that the names of the days that are stored in DefaultFormatSettings uses another starting day (Sunday instead of Monday).

Is that default or does that depend on system settings as well (on some OS it is possible to set your preferred starting day of the week) ?
https://www.timeanddate.com/calendar/days/first-day-of-the-week.html

EDIT: Another Link, but to VBA: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/weekday-function
« Last Edit: May 23, 2023, 09:04:16 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Wings2018

  • New Member
  • *
  • Posts: 16
[SOLVED] Epoch seconds conversion to day of the week fails?
« Reply #4 on: October 18, 2023, 09:00:54 pm »
Hello dseligo, TRon and Zvoni,

A late reply - was busy on other stuff lately, but thanks to all of you and especially the example code provided by Tron I was able to compose the desired day-count function (a little bit different from my original proposal).  :) :D

I'm closing this topic as SOLVED.

 

TinyPortal © 2005-2018