Nyt kun uptime tuli puheeksi niin voisin "julkaista" koodin jota olen sen laskemiseen ja seuraamiseen kehittänyt. Eli ohjelma osaa ajan ja päivämäärän perustelleella määritellä tietokoneen käynnistymisajan ja kirjoittaa data-tiedostoon käynnistämispäivämäärän ja ajan sekä käynnissä olleet sekunnit. Ohjelma myöskin osaa tunnistaa että onko ohjelma jo kirjoittanut ko. tiedostoon nykyisen käynnistymisajan ja tunnistaa sen samaksi käynnistykseksi. Lähinnä siis tarkoitettu tietokoneella vietetyn ajan seuraamiseen.
Code: Select all
SCREEN 400,1
Type oldtime
Field datetxt$
Field timetxt$
Field seconds
End Type
Const SecondsInMinute = 60
Const SecondsInHour = 3600
Const SecondsInDay = 86400
Const SecondsInWeek = 604800
Global ComputerStartTime As String , ComputerStartDate As String , UptimeSeconds As integer
file$ = "uptimedata.dat"
ComputerStartTime = GetComputerStartTime()
ComputerStartDate = GetComputerStartDate()
UptimeSeconds = Timer()/1000
ReadPastData(file$)
WriteNewData(file$)
SetWindow "Uptime: "+GetSecondsInTextTime(Timer()/1000),2
Repeat
UptimeSeconds = Timer()/1000
SetWindow "Uptime: "+GetSecondsInTextTime(UptimeSeconds)
If (UptimeSeconds Mod 5)=0 Then WriteNewData(file$) : DrawScreen
Wait 1000
Forever
Function DrawOnOffDiagramm(x,y,dat$,dat2$,w,h)
If dat$="" Then
o.oldtime=First (oldtime)
dat$ = o\datetxt$
EndIf
days = GetDateDifferenceInDays(dat2$,dat$)
seconds = days*SecondsInDay+GetTimeInSeconds(Time())
multiplier# = Float(w)/Float(seconds)
count=0
For o.oldtime=Each oldtime
dsec = GetDateDifferenceInSeconds(o\datetxt$,dat$)+GetTimeDifferenceInSeconds("00:00:00",o\timetxt$)
If dsec<=seconds And dsec=>0 Then
count=count+1
dx=Int(Float(dsec)*multiplier#)
dw=dx+Int(Float(o\seconds)*multiplier#)
Color 64,64,64
If count=1 Then
Box x,y+h/2,dx,-h/2
'Line x,y,x+dx,y
Else
Box x+pw,y+h/2,dx-pw,-h/2
'Line x+pw,y,x+dx,y
EndIf
Color cbwhite
Box x+dx,y+h,dw-dx,-h
'Line x+dx,y+h,x+dw,y+h
px = dx
pw = dw
EndIf
Next o
End Function
Function WriteNewData(file$)
found=0
For o.oldtime=Each oldtime
If o\datetxt$ = ComputerStartDate Then
If GetTimeDifferenceInSeconds(ComputerStartTime,o\timetxt$)<3 Then
o\seconds=UptimeSeconds
found=1
Exit
EndIf
EndIf
Next o
If Not found Then
o.oldtime=New(oldtime)
o\datetxt$ = ComputerStartDate
o\timetxt$ = ComputerStartTime
o\seconds = UptimeSeconds
EndIf
f=OpenToWrite(file$)
For o.oldtime=Each oldtime
WriteLine f,o\datetxt$
WriteLine f,o\timetxt$
WriteLine f,o\seconds
Next o
CloseFile f
End Function
Function ReadPastData(file$)
If FileExists(file$) Then
f = OpenToRead(file$)
While Not EOF(f)
o.oldtime=New(oldtime)
o\datetxt$ = ReadLine (f)
o\timetxt$ = ReadLine (f)
o\seconds = Int(ReadLine (f))
Wend
CloseFile f
EndIf
End Function
Function GetTimeDifferenceInSeconds(timetxt1$,timetxt2$)
hours = Int(GetWord(timetxt1$,1,":"))
minutes = Int(GetWord(timetxt1$,2,":"))
seconds = Int(GetWord(timetxt1$,3,":"))
time1seconds = hours*SecondsInHour+minutes*SecondsInMinute+seconds
hours = Int(GetWord(timetxt2$,1,":"))
minutes = Int(GetWord(timetxt2$,2,":"))
seconds = Int(GetWord(timetxt2$,3,":"))
time2seconds = hours*SecondsInHour+minutes*SecondsInMinute+seconds
Return time1seconds-time2seconds
End Function
Function GetTimeInSeconds(clock$)
hours = Int(GetWord(clock$,1,":"))
minutes = Int(GetWord(clock$,2,":"))
seconds = Int(GetWord(clock$,3,":"))
Return hours*SecondsInHour+minutes*SecondsInMinute+seconds
End Function
Function GetComputerStartDate()
clock$ = Time()
SecondCounter = Timer()/1000
dat$ = Date()
year = Int(GetWord(dat$,3))
month = GetDateMonth(dat$)
day = Int(GetWord(dat$,1))
hours = Int(GetWord(clock$,1,":"))
minutes = Int(GetWord(clock$,2,":"))
seconds = Int(GetWord(clock$,3,":"))
SecondsToday = hours*SecondsInHour+minutes*SecondsInMinute+seconds
If SecondCounter>SecondsToday Then day=day-1
SecondCounter= SecondCounter - SecondsToday
If day=0 Then
month=month-1
If month=0 Then month=12 : year = year - 1
day = GetMonthDays(month,year)
EndIf
While SecondCounter>0
If SecondCounter>SecondsInDay Then day=day-1
SecondCounter=SecondCounter-SecondsInDay
If day=0 Then
month=month-1
If month=0 Then month=12 : year = year - 1
day = GetMonthDays(month,year)
EndIf
Wend
Return GetDayMonthYearDate(day,month,year)
End Function
Function GetComputerStartTime()
clock$ = Time()
SecondCounter = Timer()/1000
hours = Int(GetWord(clock$,1,":"))
minutes = Int(GetWord(clock$,2,":"))
seconds = Int(GetWord(clock$,3,":"))
SecondsToday = hours*SecondsInHour+minutes*SecondsInMinute+seconds
StartSeconds = SecondsToday-SecondCounter
If StartSeconds<0 Then
StartSeconds = SecondCounter-SecondsToday
Return GetSecondsInTime(secondsInDay - (StartSeconds Mod secondsInDay))
Else
Return GetSecondsInTime(StartSeconds)
EndIf
End Function
Function GetSecondsInTime(second)
hours = RoundDown(Float(second)/Float(SecondsInHour))
second=second-hours*SecondsInHour
minutes = RoundDown(Float(second)/float(SecondsInMinute))
second = second-minutes*SecondsInMinute
shours$=Str(hours)
If hours < 10 Then shours$="0"+shours$
sminutes$=Str(minutes)
If minutes < 10 Then sminutes$="0"+sminutes$
sseconds$=Str(second)
If second < 10 Then sseconds$="0"+sseconds$
Return shours$+":"+sminutes$+":"+sseconds$
End Function
Function GetSecondsInTextTime(second)
weeks = RoundDown(Float(second)/Float(SecondsInWeek))
second=second-weeks*SecondsInWeek
days = RoundDown(Float(second)/Float(SecondsInDay))
second=second-days*SecondsInDay
hours = RoundDown(Float(second)/Float(SecondsInHour))
second=second-hours*SecondsInHour
minutes = RoundDown(Float(second)/float(SecondsInMinute))
second = second-minutes*SecondsInMinute
txt$=""
If weeks Then txt$=txt$+weeks+" weeks "
If days Then txt$=txt$+days+" days "
If hours Then txt$=txt$+hours+" hours "
If minutes Then txt$=txt$+minutes+" minutes "
If second Then txt$=txt$+second+" seconds "
Return txt$
End Function
Function GetDateDifferenceInSeconds(dat$,dat2$)
Return GetDateDifferenceInDays(dat$,dat2$)*SecondsInDay
End Function
Function GetMonthDays(month,year)
Select month
Case 1,3,5,7,8,10,12
Return 31
Case 4,6,9,11
Return 30
Case 2
If IsLeapYear(year) Then
Return 29
Else
Return 28
EndIf
End Select
MakeError "Not valid month year combination: "+month+" "+year
End Function
Function GetDateDifferenceInDays(dat$,dat2$)
//dat$-dat2$
year1 = Int(GetWord(dat$,3))
year2 = Int(GetWord(dat2$,3))
month1 = GetDateMonth(dat$)
month2 = GetDateMonth(dat2$)
day1 = Int(GetWord(dat$,1))
day2 = Int(GetWord(dat2$,1))
days = 0
If year1<>year2 Or month1<>month2 Or day1<>day2 Then
Repeat
days = days + 1
day2 = day2 + 1
If day2 = GetMonthDays(month2,year2)+1 Then day2=1 : month2 = month2 + 1
If month2=13 Then month2=1 : year2=year2+1
If year1=year2 And month1=month2 And day1=day2 Then Return days
Forever
Else
Return 0
EndIf
End Function
Function IsLeapYear(year)
If (year Mod 4)=0 Then
If (year Mod 100)=0 Then
If (year Mod 400)=0 Then Return True Else Return False
EndIf
Return True
EndIf
Return False
End Function
Function GetDayMonthYearDate(day,month,year)
Select month
Case 1
monthtxt$= "Jan"
Case 2
monthtxt$= "Feb"
Case 3
monthtxt$= "Mar"
Case 4
monthtxt$= "Apr"
Case 5
monthtxt$= "May"
Case 6
monthtxt$= "Jun"
Case 7
monthtxt$= "Jul"
Case 8
monthtxt$= "Aug"
Case 9
monthtxt$= "Sep"
Case 10
monthtxt$= "Oct"
Case 11
monthtxt$= "Nov"
Case 12
monthtxt$= "Dec"
Default
MakeError "Month: "+month+" not valid!"
End Select
Return Str(day+" "+monthtxt$+" "+year)
End Function
Function GetDateMonth(dat$)
month$ = GetWord(dat$,2)
Select month$
Case "Jan"
Return 1
Case "Feb"
Return 2
Case "Mar"
Return 3
Case "Apr"
Return 4
Case "May"
Return 5
Case "Jun"
Return 6
Case "Jul"
Return 7
Case "Aug"
Return 8
Case "Sep"
Return 9
Case "Oct"
Return 10
Case "Nov"
Return 11
Case "Dec"
Return 12
Default
Return 0
End Select
End Function
Function WriteConfig(tiedosto$, otsikko$, arvo$)
Dim cfg(100) As String
otsikko$ = Lower(otsikko$)
f = OpenToRead(tiedosto$)
While Not EOF(f)
rivimäärä+1
cfg(rivimäärä) = Lower(ReadLine(f))
Wend
CloseFile f
For i = 1 To rivimäärä
If Left(cfg(i), Len(otsikko$)+1) = otsikko$+"=" Then
cfg(i) = otsikko$+"="+arvo$
muokattu = 1
EndIf
Next i
If Not muokattu Then
rivimäärä+1
cfg(rivimäärä) = otsikko$+"="+arvo$
EndIf
f = OpenToWrite(tiedosto$)
For i = 1 To rivimäärä
WriteLine f, cfg(i)
Next i
CloseFile f
Return 1
End Function
Function ReadConfig(tiedosto$, otsikko$)
otsikko$ = Lower(otsikko$)
If FileExists(tiedosto$) Then
f = OpenToRead(tiedosto$)
While Not EOF(f)
rivi$ = Lower(ReadLine(f))
If Left(rivi$, Len(otsikko$)+1) = otsikko$+"=" Then
arvo$ = Replace(rivi$, otsikko$+"=", "")
Return arvo$
EndIf
Wend
Return 0
Else
MakeError "Config file not found!"
EndIf
End Function
Muistaakseni siinä on vielä joku vika, tiedosto korruptoitui jostain syystä pitkän käytön aikana. Vielä tarvitsisi jonkinlaisen seurantasysteemin, kenties kuvaajan uptime ajoista.
En minäkään, ois pitänyt pistää tää offtopic ketjuun tai jättää kokonaan pöytälaatikkoon homehtumaan.