Epoch time.

Windows specific questions.
srvaldez
Posts: 2344
Joined: Sep 25, 2005 21:54

Re: Epoch time.

Postby srvaldez » Mar 09, 2020 13:46

thanks deltarho[1859]
time and date are interesting contraptions.
deltarho[1859]
Posts: 2360
Joined: Jan 02, 2017 0:34
Location: UK

Re: Epoch time.

Postby deltarho[1859] » Mar 09, 2020 15:18

Yeah, I have always ben fascinated by time especially time dilation when travelling at very fast speeds or travelling past a massive body.

All my watches and clocks get a signal from the Anthorn Radio Station near Anthorn, Cumbria - I don't have a mechanical time piece to my name.

A few years I wrote an application to poll 20 time-servers in America using a 'round robin' method and I have been using it ever since. The system clock on all the PC's that I have had have drifted quite a bit and got worse with each new PC. When it got to about three minutes in a day I decided to do something about it. My clock is silently corrected every two hours. A log is kept on the corrections, and they are usually about 250ms. If my machine is off or asleep for more than two hours then a correction is invoked immediately and every two hours thereafter. If a server does not respond quickly enough then another server is considered. It is rare to contact more than five servers. Another log is kept to record 'failures' so I can check which are the most reliable. It is configurable using command line switches Silent, DisplayOnce, RunOnce, Query, Wait:, and Schedule. The Wait: is for folk on 'Dial Up' where they can delay execution until they are online.

I would make the exe available but it takes a bit to set up. If we have UAC at 'full throttle' then an application in the StartUp folder which would invoke UAC will be skipped over, so we have to ensure that is has elevated privileges to bypass the UAC. It is not difficult to achieve but I can imagine many folk who would not be prepared to put any effort in. It is very accurate as it corrects the time got according to a ping method built in. It has been compared with the atomic clock at the National Physical Laboratory (NPL), Teddington, only a few miles from me, and the corrections after a NPL check are around a few milliseconds to about 40ms even with a server in California. So, why not use the NPL server? I wanted to spread my checks over long intervals. With 20 servers and an average of about three servers per check then a correction every two hours will see me 'knocking at the door', on average, every 13 hours. That will not be burden to anyone.
dodicat
Posts: 6390
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Epoch time.

Postby dodicat » Mar 09, 2020 15:47

I must admit I haven't noticed any appreciable difference when I am in a hurry.
But then again I haven't really been measuring.
The chronometer was a crucial piece of equipment at my job.
It had to be inverted properly and the winding key had to be turned only by the amount of turns written in the manufacturer's specification.
Messing about with the chronometer was a No No, whether by the watchkeepers, the quartermasters or the captain, and especially by any engineers who might be visiting the wheelhouse.
deltarho[1859]
Posts: 2360
Joined: Jan 02, 2017 0:34
Location: UK

Re: Epoch time.

Postby deltarho[1859] » Mar 09, 2020 16:02

I have always regarded the marine chronometer a beautiful piece of kit and, as you probably know, was invented by John Harrison a, <cough>, Yorkshireman. Image
deltarho[1859]
Posts: 2360
Joined: Jan 02, 2017 0:34
Location: UK

Re: Epoch time.

Postby deltarho[1859] » Mar 09, 2020 18:53

Talking about my clock correction program reminded me of some code I wrote some years ago which shows my interest in time goes back a quite a while. In fact the code was published in December 1988 in St World's 'Type in Listing' section. St World's preamble says: "Calendar XII, by David Roberts, is a desk accessory written in Hisoft BASIC for use with a high resolution monitor. It displays a calendar of a twelve-month period within a moveable GEM window".

In those days if one or more windows sat on top of ours then when we clicked on ours it was our responsibility to do the redrawing. The program kept a full image in a buffer and used a block image transfer (blit) for the redrawing. It was fairly swift but with a 8MHz CPU in my Atari ST it was quite obvious what was going on.

I also have HiSoft DevPac, a full editor/assembler/debugger environment, and I exchanged a few letters with the author, I cannot remember his name - he eventually joined Microsoft.

That month the magazine had 104 pages and my unsolicited contribution, comments and code, didn't get edited at all resulting in five pages. Believe it or not I got paid £200 for that which, nearly 32 years ago, was better than a poke in the eye with a sharp stick. My Atari had 1MB of RAM, so I 'splashed out' £100 to have 4MB.

Needless to say I wrote that code when I was in short trousers. Image
deltarho[1859]
Posts: 2360
Joined: Jan 02, 2017 0:34
Location: UK

Re: Epoch time.

Postby deltarho[1859] » Mar 10, 2020 19:01

Another update. Image

EpochTimeToSystemTime has two new parameters.

Code: Select all

Function EpochTimeToSystemTime( ByVal ulGivenEpochTime As UlongInt, ByVal Flag As Long, _
 Byval FormatNum as Long, byval ms As Boolean ) As String

FormatNum is used in a 'Select/End Select' construct to choose from a bunch of format presets. I have some in the code for you but you can, of course, design your own.

ms is a Boolean and displays milliseconds if True and nothing if False. Needless to say this is only considered if 'TimeFormat' is not empty in the 'Select/End Select' construct.

These additions are tested in the edited opening post.

This is what I get:

Code: Select all

119731017000
1000000000000
1234567890000
2000000000000
2147483648000
3000000000000
4294967295000
 
Epoch time now: 1583866009531
 
Milliseconds included
 
Tuesday, 10 March 2020 18:46:49:531
10/03/2020
10/3/20
10/03/2020 18:46:531
10/3/20 18:46:531
Tuesday
 
Milliseconds excluded
 
Tuesday, 10 March 2020 18:46:49
10/03/2020
10/3/20
10/03/2020 18:46
10/3/20 18:46
Tuesday

Of course, we would be in a sorry state if we did not know what the day of the week was for today but that format comes into its own when we want to know what day of the week a particular date fell or falls on.
deltarho[1859]
Posts: 2360
Joined: Jan 02, 2017 0:34
Location: UK

Re: Epoch time.

Postby deltarho[1859] » Mar 11, 2020 12:49

I tend to only use 24 hour times. Here is an example of 12 hour times.

Code: Select all

DateFormat = "MMMM d',' yyyy"
TimeFormat = "h':'mm tt"

which will give something like this: March 11, 2020 6:51 PM
dodicat
Posts: 6390
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Epoch time.

Postby dodicat » Mar 12, 2020 19:55

It's three o'clock.

Code: Select all

  Sub plot(x As Long,y As Long,c As Single,col As Ulong,i As Any Ptr)
        Dim As Ubyte rd=Cast(Ubyte Ptr,@col)[2]
        Dim As Ubyte gr=Cast(Ubyte Ptr,@col)[1]
        Dim As Ubyte bl=Cast(Ubyte Ptr,@col)[0]
        Dim As Ubyte al=Cast(Ubyte Ptr,@col)[3]*c
        Pset i,(x,y),Rgba(rd,gr,bl,al)
    End Sub
   
Sub drawLineAA(x0 As Long,y0 As Long,x1 As Long,y1 As Long,col As Ulong,i As Any Ptr)
    #define ipart(x) Int((x))
    #define round(x) ipart((x) + 0.5)
    #define fpart(x) Frac((x))
    #define rfpart(x) 1-fpart((x))
   
   Dim As boolean steep = Abs(y1 - y0) > Abs(x1 - x0)
   
    If steep Then
        Swap x0, y0
        Swap x1, y1
    End If
    If x0 > x1 Then
        Swap x0, x1
        Swap y0, y1
    End If
   
   Dim As Long dx = x1 - x0
   Dim As Long dy = y1 - y0
   Var gradient = dy / dx
    If dx = 0 Then
        gradient = 1
    End If

   Dim As Long xend = round(x0)
   Dim As Long  yend = y0 + gradient * (xend - x0)
   Dim As Single xgap = rfpart(x0 + 0.5)
   Dim As Long  xpxl1 = xend
   Dim As Long ypxl1 = ipart(yend)
    If steep Then
        plot(ypxl1,   xpxl1, rfpart(yend) * xgap,col,i)
        plot(ypxl1+1, xpxl1,  fpart(yend) * xgap,col,i)
    Else
        plot(xpxl1, ypxl1  , rfpart(yend) * xgap,col,i)
        plot(xpxl1, ypxl1+1,  fpart(yend) * xgap,col,i)
    End If
   
    Dim As Single intery = yend + gradient 'first y-intersection for the main loop
   
    ' handle second endpoint
    xend = round(x1)
    yend = y1 + gradient * (xend - x1)
    xgap = fpart(x1 + 0.5)
    Dim As Long xpxl2 = xend 'this will be used in the main loop
    Dim As Long ypxl2 = ipart(yend)
    If steep Then
        plot(ypxl2  , xpxl2, rfpart(yend) * xgap,col,i)
        plot(ypxl2+1, xpxl2,  fpart(yend) * xgap,col,i)
    Else
        plot(xpxl2, ypxl2,  rfpart(yend) * xgap,col,i)
        plot(xpxl2, ypxl2+1, fpart(yend) * xgap,col,i)
    End If
   
    '// main loop
    If steep Then
        For x As Long = xpxl1 + 1 To xpxl2 - 1
                plot(ipart(intery)  , x, rfpart(intery),col,i)
                plot(ipart(intery)+1, x,  fpart(intery),col,i)
                intery = intery + gradient
           Next
    Else
        For x As Long = xpxl1 + 1 To xpxl2 - 1
                plot(x, ipart(intery),  rfpart(intery),col,i)
                plot(x, ipart(intery)+1, fpart(intery),col,i)
                intery = intery + gradient
           Next
    End If
End Sub

Sub LineByAngle(x As Long,y As Long,angle As Single,length As Single,col As Ulong,Byref x2 As Long=0,Byref y2 As Long=0,f As Long=0)
    x2=x+length*Cos(angle)
    y2=y-length*Sin(angle)
  If f=0 Then drawLineAA(x,y,x2,y2,col,0)
End Sub

Sub digits(t As String,x As Long,y As Long,clr As Ulong,sz As Long,img As Any Pointer=0)
        x=x-2*sz
        Dim As Single s=Any,c=Any
        #macro LineaAA(x1,y1,x2,y2)
            drawlineAA(x1,y1,x2,y2,clr,img)
        #endmacro
        #macro display(_a,_b,_c,_d,_e,_f,_g)
        x=x+2*sz
        If _a=1 Then :LineaAA(x,y,(x+sz),y):End If
        If _b=1 Then :LineaAA((x+sz),y,(x+sz),(y+sz)):End If
        If _c=1 Then :LineaAA((x+sz),(y+sz),(x+sz),(y+2*sz)):End If
        If _d=1 Then :LineaAA((x+sz),(y+2*sz),x,(y+2*sz)):End If
        If _e=1 Then :LineaAA(x,(y+2*sz),x,(y+sz)):End If
        If _f=1 Then :LineaAA(x,(y+sz),x,y):End If
        If _g=1 Then :LineaAA(x,(y+sz),(x+sz),(y+sz)):End If
        #endmacro
        For z As Long=0 To Len(t)-1
            Select Case As Const t[z]
            Case 48 :display(1,1,1,1,1,1,0)             '"0"
            Case 49 :display(0,1,1,0,0,0,0)             '"1"
            Case 50 :display(1,1,0,1,1,0,1)             '"2"
            Case 51 :display(1,1,1,1,0,0,1)             '"3"
            Case 52 :display(0,1,1,0,0,1,1)             '"4"
            Case 53 :display(1,0,1,1,0,1,1)             '"5"
            Case 54 :display(1,0,1,1,1,1,1)             '"6"
            Case 55 :display(1,1,1,0,0,0,0)             '"7"
            Case 56 :display(1,1,1,1,1,1,1)             '"8"
            Case 57 :display(1,1,1,1,0,1,1)            '"9"
            Case 58                                     '":"                   
                Circle img,((x+2*sz),(y+sz/2)),(sz/5),clr,,,,f
                Circle img,((x+2*sz),(y+1.5*sz)),(sz/5),clr,,,,f
                x=x+sz
            Case 45 :display(0,0,0,0,0,0,1)              '"-"                       
            Case 46                                      '"."                       
                Circle img,((x+2*sz),(y+1.9*sz)),(sz/5),clr,,,,f
                x=x+sz
            Case 32                                      '" "
                x=x+sz
            End Select
        Next z
    End Sub 
   
Sub rotateimage(Byref dest As Any Ptr=0,im As Any Ptr,angle As Single,shiftx As Long=0,shifty As Long=0,sc As Single=1,miss As Ulong=Rgb(255,0,255),fixedpivot As boolean=false)
    Static As Integer pitch,pitchs,xres,yres,runflag
    Static As Any Ptr row,rows
    Static As Integer ddx,ddy,resultx,resulty
    Imageinfo im,ddx,ddy,,pitch,row
    If dest=0 Then
    Screeninfo xres,yres,,,pitchS
    rowS=Screenptr
    Else
    If sc<>1 Then
        Dim As Integer x,y
        Imageinfo dest,x,y
    Imagedestroy dest:dest=0: dest=Imagecreate(x*sc,y*sc)
    End If
    Imageinfo dest, xres,yres,,pitchS,rows
    End If
    Dim As Long centreX=ddx\2,centreY=ddy\2
    Dim As Single sx=Sin(angle)
    Dim As Single cx=Cos(angle)
    Dim As Long mx=Iif(ddx>=ddy,ddx,ddy),shftx,shfty
    Var fx=sc*.7071067811865476,sc2=1/sc
    If fixedpivot=false Then
     shiftx+=centreX*sc-centrex
     shiftY+=centrey*sc-centrey
     End If
    For y As Long=centrey-fx*mx+1 To centrey+ fx*mx
        Dim As Single sxcy=Sx*(y-centrey),cxcy=Cx*(y-centrey)
        shfty=y+shifty
        For x As Long=centrex-mx*fx To centrex+mx*fx
                 If x+shiftx >=0 Then 'on the screen
                    If x+shiftx <xres Then
                        If shfty >=0 Then
                            If shfty<yres Then
            resultx=sc2*(Cx*(x-centrex)-Sxcy) +centrex:resulty=sc2*(Sx*(x-centrex)+Cxcy) +centrey
                If resultx >=0 Then 'on the image
                    If resultx<ddx Then
                        If resulty>=0 Then
                            If resulty<ddy Then
    Dim As Ulong u=*Cast(Ulong Ptr,row+pitch*((resultY))+((resultX)) Shl 2 ) 'point(image)
   If u<>miss Then *Cast(Ulong Ptr,rowS+pitchS*(y+shifty)+(x+shiftx) Shl 2)= u 'pset (screen)
                End If:End If:End If:End If
                End If:End If:End If:End If
        Next x
    Next y
End Sub

Function Clock(clr As Ulong=Rgb(0,50,125)) As Any Ptr
    Static As Any Ptr im
    Static As Long blink
    Static As String LastTime
    Dim As String t=Time
    If LastTime<>t Then
        'do all this stuff only once per second:
        blink+=1
        LastTime=t
        If im Then Imagedestroy im:im=0
       im= Imagecreate(210,210)
        Circle im,(105,105),105,clr,,,,f
    #define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
    #macro mod12(n,m)
        m= n Mod 12
        If m=0 Then m=12
    #endmacro
    #macro lineto(x1,y1,x2,y2,d,col,flag)
        If flag Then drawlineAA(x1,y1,x1+(x2-x1)*d,y1+(y2-y1)*d ,col,im)
        catchx=x1+(x2-x1)*d:catchy=y1+(y2-y1)*d
    #endmacro
    #macro drawline(x,y,ang,length,col,x2,y2,flag)
        ang2=ang:ang2=ang2*.0174532925199433
        x2=(x)+length*Cos(ang2)
        y2=(y)-length*Sin(ang2)
        If flag Then drawlineAA(x,y,x2,y2,col,im)       
    #endmacro
   
    Dim As Long m2,catchx,catchy
    Dim As Single b1,b2,tmp1,tmp2,ang2
    Dim As Long hm=Valint(Left(t,2)),mm=Valint(Mid(t,4,2)),sm=Valint(Right(t,2))
    mod12(hm,m2)
    Dim As Single h=map(0,12,m2,360,0)
    Dim As Single m=map(0,60,mm,360,0)
    Dim As Single s=map(0,60,sm,360,0)
    For z As Long=0 To 360 Step 6
        drawline((105),(105),z,100,15,b1,b2,0)
        'spots
        If z Mod 30=0 Then
            lineto(b1,b2,(105),(105),.1,Rgb(200,0,0),1) 
            Circle im,(b1,b2),3,Rgb(100,100,255),,,,f
        Else
            lineto(b1,b2,105,105,.05,Rgb(0,0,200),1)
            Circle im,(b1,b2),1,Rgb(0,0,100),,,,f
        End If
        drawline(105,105,z,85,Rgb(0,0,0),b1,b2,0)
        Dim As Single n=map(360,0,(z-90),0,12)
        'numbers
        If z Mod 30=0 Then
            mod12(n,m2)
            lineto(105,105,b1,b2,.9,0,0)
            digits(Str(m2),catchx-5,catchy-7,Rgb(0,200,0),5,im)
        End If
    Next z
    digits(Time,105-13-15,105-45,Rgb(255,127,0),4,im)
    If blink Mod 2 Then
        blink=-1
        drawlineAA(105-13-15,105-32,105-13+40,105-32,Rgb(200,0,0),im)
        End If
        drawline(105,105,(h+90)-5*(360-m)/60,60,Rgb(0,200,0),tmp1,tmp2,1)'hour
        drawline(105,105,(m+90)-(360-s)/60,85,Rgb(0,200,0),tmp1,tmp2,1)  'minute
        drawline(105,105,(s+90),95,Rgb(100,100,100),tmp1,tmp2,1)             'second
        Circle im,(105,105),3,Rgb(100,100,100),,,,f                  'centre
    End If'every second
    Return im
End Function

Function Regulate(Byval MyFps As Long,Byref fps As Long=0) As Long
    Static As Double timervalue,_lastsleeptime,t3,frames
    Var t=Timer
    frames+=1
    If (t-t3)>=1 Then t3=t:fps=frames:frames=0
    Var sleeptime=_lastsleeptime+((1/myfps)-T+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    _lastsleeptime=sleeptime
    timervalue=T
    Return sleeptime
End Function

function fbmain as long
Screenres 1024,820,32,,64
Dim As Integer w,h
Dim As Long x,y,fps
Screeninfo w,h
Dim As Single a,pi=4*Atn(1)
Windowtitle "FRAMERATE"
Do
    a+=.02
    Screenlock
    Cls
    digits(Str(fps),20,20,Rgb(255,255,255),20)
    LineByAngle(w/2,120,.5*Sin(a)-pi/2,.35*h,Rgb(200,0,0),x,y)
    Put(w/2-105,120-105),clock,trans
    rotateimage(,clock,.5*Sin(a),x-105,y-105,1,Rgb(255,0,255),false)
    LineByAngle(w/2,120,.5*Sin(a)-pi/2,.7*h,Rgb(200,0,0),x,y,1)
    Circle(w/2,120),.7*h+107,Rgb(255,255,255),4+.2,5.45-.2
    rotateimage(,clock,1.8*Sin(-a),x-105,y-105,1,Rgb(255,0,255),false)
Screenunlock
Sleep regulate(50,fps),1
Loop Until Len(Inkey)
Sleep
return 0
end function

end fbmain

 
deltarho[1859]
Posts: 2360
Joined: Jan 02, 2017 0:34
Location: UK

Re: Epoch time.

Postby deltarho[1859] » Mar 12, 2020 20:21

@dodicat

That is absolutely awesome! Image
UEZ
Posts: 523
Joined: May 05, 2017 19:59
Location: Germany

Re: Epoch time.

Postby UEZ » Mar 12, 2020 21:25

dodicat wrote:It's three o'clock.

Indeed, looks very cool.
jj2007
Posts: 1403
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Epoch time.

Postby jj2007 » Mar 12, 2020 23:13

deltarho[1859] wrote:... used a block image transfer (blit) for the redrawing. It was fairly swift but with a 8MHz CPU in my Atari ST
Welcome to the club ;-)
I used GfaBasic, though, and an assembler called "GST 68000". Memories of a distant past...
deltarho[1859]
Posts: 2360
Joined: Jan 02, 2017 0:34
Location: UK

Re: Epoch time.

Postby deltarho[1859] » Mar 12, 2020 23:52

And going back further to the Sinclair ZX81, programming the Z80 CPU going flat out at 2.5MHz, being kept cool in the summer with a little Pifco fan and 'duck' tape on the 16KiB ram pack to stop it wobbling and crashing the machine, displayed on an old 14" black and white TV. Until I got an assembler I programmed in hex code. I wouldn't' have believed what I would have nearly 40 years later.
jj2007
Posts: 1403
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Epoch time.

Postby jj2007 » Mar 13, 2020 4:04

I had a ZX Spectrum, and before that a programmable Casio calculator. The Atari ST was a huge step forward for me, especially when I could replace the "built-in" Basic dialect with GfaBasic
dodicat
Posts: 6390
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Epoch time.

Postby dodicat » Mar 13, 2020 9:44

I still have a ZX Spectrum, and a programmable Casio PB 80.
http://pocket.free.fr/html/casio/pb-80_e.html
Just tested, the battery must be flat.
I was way ahead of the game when I was 1st. Mate on oil tankers, I used it for the cargo calculations.
And look at me now, still coding in basic, what went wrong?
deltarho[1859]
Posts: 2360
Joined: Jan 02, 2017 0:34
Location: UK

Re: Epoch time.

Postby deltarho[1859] » Mar 13, 2020 12:12

I had a Sharp PC-1350, or was it a PC-1360, I cannot remember. Anyway, back in 1990 a friend of mine was a Collector of Taxes with the UK's Inland Revenue, now Her Majesty's Revenue and Customs. He took legal action on tax debtors and calculated, manually, interest due on the debt from the date it was due to the day of the legal action. The interest was simple interest, and he had to consider interest changes since the debt was due going back no more than six years, so there could have been a few changes in the interim.

I got the interest changes and wrote a little program for him: Inputting the debt, due date and the day legal action was taken. With the calculator he could do this when he met the debtor rather than sit in his office doing quite a few calculations before going out.

A few months down the line he was told by the folk at the Inland Revenues's main frame in Bradford that his calculations were wrong - not by much but wrong, nevertheless. I asked him to give me some examples of the Bradford figures, and they were not the same as mine. When an interest increased it was deemed, by law, to take effect from the midnight following. With a reduction it was deemed to take effect from the previous midnight. So, both situations were in the debtor's favour, albeit, in practice, only a small amount.

It wasn't a long story but to keep it short it transpired that the Inland Revenue were using the opposite formula to the one laid down by law and there were some red faces at Bradford. I joked that the press would have a field day if they found out. My friend said: "You wouldn't, would you?" I said that I wouldn't tell anyone if he didn't and that was the end of that. A few months later my friend got a job in Bradford, and we didn't keep in touch.

At some point, I don't know when, desktop computers were installed in the Revenue's offices and the little Sharp would have had its day.

I bet you don't believe me but that is a true story.

Return to “Windows”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 1 guest