Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Date functions
#4
My TIM library:

Code: (Select All)
Dim As _Unsigned _Integer64 stamp, now, utc
Dim As _Unsigned Long days, secs

stamp = TIM.stamps("2024-03-04 16:45:22") 'seconds since 01-01-0001
stamp = TIM.stamp(2024, 3, 4, 16, 45, 22) 'seconds since 01-01-0001
Print TIM.format$(stamp) 'long format
now = TIM.now: Print "now="; TIM.string$(now)
utc = TIM.utc: Print "utc="; TIM.string$(utc)
Print TIM.dateTime$(stamp, year%, month%, day%, hour%, minute%, seconds%) ' fills vars & returns datetime without -/:
days = TIM.days(year%, month%, day%) 'days since 01-01-0001
secs = TIM.seconds(hour%, minute%, seconds%) 'seconds since midnight
Print "is"; year%; " a leapyear:"; TIM.leapYear(year%)
Print "weekday (0=sun - 6=sat)"; TIM.weekDay(now)
Print TIM.dst(now, 1) 'added seconds to UTC for CET (UTC+1) daylight savings

End

Function TIM.stamps~&& (dt$)
  year% = Val(Left$(dt$, 4)): month% = Val(Mid$(dt$, 6, 2)): day% = Val(Mid$(dt$, 9, 2))
  hour% = Val(Mid$(dt$, 12, 2)): minute% = Val(Mid$(dt$, 15, 2)): second% = Val(Mid$(dt$, 18, 2))
  TIM.stamps~&& = TIM.stamp(year%, month%, day%, hour%, minute%, second%)
End Function

Function TIM.string$ (dt~&&)
  dt$ = TIM.dateTime$(dt~&&, year%, month%, day%, hour%, minute%, second%)
  TIM.string$ = Left$(dt$, 4) + "-" + Mid$(dt$, 5, 2) + "-" + Mid$(dt$, 7, 2) + " " + _
    Mid$(dt$, 9, 2) + ":" + Mid$(dt$, 11, 2) + ":" + Mid$(dt$, 13, 2)
End Function

Function TIM.now~&& ()
  dat$ = Date$: tim~& = Timer
  month% = Val(Left$(dat$, 2))
  day% = Val(Mid$(dat$, 4, 2))
  year% = Val(Mid$(dat$, 7, 4))
  TIM.now~&& = TIM.days~&(year%, month%, day%) * 86400~&& + tim~&
End Function

Function TIM.utc~&& ()
  Type UTCtype
    year As Integer
    month As Integer
    weekday As Integer
    day As Integer
    hour As Integer
    minute As Integer
    second As Integer
    millis As Integer
  End Type
  Declare Dynamic Library "Kernel32"
    Sub GetUTC Alias GetSystemTime (lpSystemTime As UTCtype)
  End Declare
  Dim utc As UTCtype: GetUTC utc
  TIM.utc~&& = TIM.stamp~&&(utc.year, utc.month, utc.day, utc.hour, utc.minute, utc.second)
End Function

Function TIM.stamp~&& (year%, month%, day%, hour%, minute%, second%)
  TIM.stamp~&& = TIM.days~&(year%, month%, day%) * 86400~&& + TIM.seconds~&(hour%, minute%, second%)
End Function

Function TIM.days~& (year%, month%, day%)
  leap% = TIM.leapYear(year%): prevYear% = year% - 1
  dPrevYears& = ((((prevYear% * 365) + (prevYear% \ 4)) - (prevYear% \ 100)) + (prevYear% \ 400))
  Select Case month%
    Case 1: TIM.days~& = ((dPrevYears&) + day%) - 1
    Case 2: TIM.days~& = ((dPrevYears& + 31) + day%) - 1
    Case 3: TIM.days~& = ((dPrevYears& + 59 - leap%) + day%) - 1
    Case 4: TIM.days~& = ((dPrevYears& + 90 - leap%) + day%) - 1
    Case 5: TIM.days~& = ((dPrevYears& + 120 - leap%) + day%) - 1
    Case 6: TIM.days~& = ((dPrevYears& + 151 - leap%) + day%) - 1
    Case 7: TIM.days~& = ((dPrevYears& + 181 - leap%) + day%) - 1
    Case 8: TIM.days~& = ((dPrevYears& + 212 - leap%) + day%) - 1
    Case 9: TIM.days~& = ((dPrevYears& + 243 - leap%) + day%) - 1
    Case 10: TIM.days~& = ((dPrevYears& + 273 - leap%) + day%) - 1
    Case 11: TIM.days~& = ((dPrevYears& + 304 - leap%) + day%) - 1
    Case 12: TIM.days~& = ((dPrevYears& + 334 - leap%) + day%) - 1
    Case Else: TIM.days~& = 0
  End Select
End Function

Function TIM.seconds~& (hour%, minute%, second%)
  TIM.seconds~& = hour% * 3600 + minute% * 60 + second%
End Function

Function TIM.dateTime$ (timestmp~&&, year%, month%, day%, hour%, minute%, second%)
  tdays~& = timestmp~&& \ 86400 + 306
  secs~& = timestmp~&& Mod 86400
  era% = tdays~& \ 146097
  doe~& = tdays~& Mod 146097 ' [0, 146096]
  yoe% = (doe~& - doe~& \ 1460 + doe~& \ 36524 - doe~& \ 146096) \ 365 ' [0, 399]
  year% = yoe% + era% * 400
  doy% = doe~& - (365 * yoe% + yoe% \ 4 - yoe% \ 100) ' [0, 365]
  mp% = (5 * doy% + 2) \ 153 ' [0, 11]
  day% = doy% - (153 * mp% + 2) \ 5 + 1 ' [1, 31]
  If mp% < 10 Then month% = mp% + 3 Else month% = mp% - 9 ' [1, 12]
  year% = year% - (month% <= 2)
  dat$ = Right$(Str$(year% + 10000), 4) + Right$(Str$(month% + 100), 2) + Right$(Str$(day% + 100), 2)
  hour% = secs~& \ 3600
  minsec% = secs~& - (hour% * 3600)
  minute% = minsec% \ 60
  second% = minsec% - (minute% * 60)
  TIM.dateTime$ = dat$ + Right$(Str$(hour% + 100), 2) + _
    Right$(Str$(minute% + 100), 2) + Right$(Str$(second% + 100), 2)
End Function

Function TIM.format$ (ts~&&)
  dt$ = TIM.dateTime$(ts~&&, year, month, day, hour, minute, second)
  dt2$ = Mid$("SuMoTuWeThFrSa", TIM.weekDay(ts~&&) * 2 + 1, 2)+" " + _
    Mid$(dt$, 7, 2) + "-" + Mid$(dt$, 5, 2) + "-" + Mid$(dt$, 1, 4) + " " + _
    Mid$(dt$, 9, 2) + ":" + Mid$(dt$, 11, 2) + ":" + Mid$(dt$, 13, 2)
  TIM.format$ = dt2$
End Function

Function TIM.leapYear% (year%)
  If (year% Mod 4) <> 0 Then
    TIM.leapYear% = 0
  ElseIf (year% Mod 100) = 0 Then
    TIM.leapYear% = (year% Mod 400) = 0
  Else
    TIM.leapYear% = Not 0
  End If
End Function

Function TIM.weekDay% (ts~&&)
  tdays~& = ts~&& \ 86400
  TIM.weekDay% = (tdays~& + 1) Mod 7
End Function

Function TIM.dst& (ts~&&, timezone%)
  dt$ = TIM.dateTime$(ts~&&, year%, month%, day%, hour%, minute%, second%)
  summer~&& = TIM.stamps(Right$(Str$(year% + 10000), 4) + "-03-31 01:00:00") 'UTC
  summer~&& = summer~&& - (TIM.weekDay(summer~&&) * 86400)
  winter~&& = TIM.stamps(Right$(Str$(year% + 10000), 4) + "-10-31 01:00:00") 'UTC
  winter~&& = winter~&& - (TIM.weekDay(winter~&&) * 86400)
  If ts~&& >= summer~&& And ts~&& < winter~&& Then
    TIM.dst = timezone% * 3600 + 3600 ' UTC + timezone% + dst (summer)
  Else
    TIM.dst = timezone% * 3600 ' ' UTC + timezone% (winter)
  End If
End Function 
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply


Messages In This Thread
Date functions - by dritter - 03-04-2024, 05:49 AM
RE: Date functions - by Pete - 03-04-2024, 06:43 AM
RE: Date functions - by Dimster - 03-04-2024, 03:04 PM
RE: Date functions - by mdijkens - 03-04-2024, 04:00 PM
RE: Date functions - by Dimster - 03-04-2024, 04:43 PM
RE: Date functions - by SMcNeill - 03-04-2024, 05:51 PM
RE: Date functions - by Kernelpanic - 03-04-2024, 06:12 PM
RE: Date functions - by mdijkens - 03-04-2024, 06:46 PM
RE: Date functions - by madscijr - 03-05-2024, 07:18 PM
RE: Date functions - by Kernelpanic - 03-05-2024, 10:52 PM
RE: Date functions - by madscijr - 03-05-2024, 11:15 PM
RE: Date functions - by bplus - 03-04-2024, 06:16 PM
RE: Date functions - by Kernelpanic - 03-04-2024, 06:19 PM
RE: Date functions - by Pete - 03-04-2024, 06:43 PM
RE: Date functions - by Kernelpanic - 03-05-2024, 11:13 PM
RE: Date functions - by madscijr - 03-05-2024, 11:28 PM
RE: Date functions - by mdijkens - 03-06-2024, 07:47 AM
RE: Date functions - by Kernelpanic - 03-05-2024, 11:27 PM
RE: Date functions - by Kernelpanic - 03-05-2024, 11:48 PM
RE: Date functions - by SMcNeill - 03-06-2024, 12:23 AM
RE: Date functions - by SpriggsySpriggs - 03-06-2024, 12:24 PM
RE: Date functions - by Kernelpanic - 03-07-2024, 11:17 PM
RE: Date functions - by mdijkens - 03-08-2024, 08:03 AM
RE: Date functions - by SMcNeill - 03-07-2024, 11:29 PM
RE: Date functions - by Kernelpanic - 03-07-2024, 11:41 PM
RE: Date functions - by SpriggsySpriggs - 03-08-2024, 12:48 PM
RE: Date functions - by Kernelpanic - 03-08-2024, 03:20 PM
RE: Date functions - by mdijkens - 03-08-2024, 04:06 PM
RE: Date functions - by bplus - 03-08-2024, 04:48 PM
RE: Date functions - by mdijkens - 03-08-2024, 05:01 PM
RE: Date functions - by Kernelpanic - 03-08-2024, 09:30 PM
RE: Date functions - by dano - 05-20-2024, 04:22 PM



Users browsing this thread: 5 Guest(s)