DateEx: Extended Date library

Share your advanced PureBasic knowledge/code with the community.
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: DateEx: Extended Date library

Post by jacdelad »

Wow, this is great! Thanks Little John!!!
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: DateEx: Extended Date library

Post by Mindphazer »

Hello Little John
Sorry if i'm being noob, but where can i find the PureUnit.res file, needed to make the Assert() macro work ?

Thanks
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
Little John
Addict
Addict
Posts: 4775
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: DateEx: Extended Date library

Post by Little John »

jacdelad wrote: Tue Aug 01, 2023 4:07 pm Wow, this is great! Thanks Little John!!!
Thanks for your kind words. :-) You are welcome!
I did a lot testing, and I think the module is pretty stable and versatile now.
Mindphazer wrote: Tue Aug 01, 2023 5:23 pm Sorry if i'm being noob, but where can i find the PureUnit.res file, needed to make the Assert() macro work ?
No problem. By default, the "PureUnit.res" file is located in the "Sdk\PureUnit\" subdirectory of the directory where PureBasic is installed.
Unfortunately, that doesn't make sense. :-(
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: DateEx: Extended Date library

Post by Mindphazer »

Little John wrote: Tue Aug 01, 2023 7:05 pm No problem. By default, the "PureUnit.res" file is located in the "Sdk\PureUnit\" subdirectory of the directory where PureBasic is installed.
Unfortunately, that doesn't make sense. :-(
Oh thanks, I wasn't aware of that
Just tried your code "as is", but it stops at line 1217 (CallDebugger)

Code: Select all

Assert(DateQ() = Date())
But as I'm on a MacBook M1, that may be the reason...
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
Little John
Addict
Addict
Posts: 4775
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: DateEx: Extended Date library

Post by Little John »

Mindphazer wrote: Tue Aug 01, 2023 7:56 pm Just tried your code "as is", but it stops at line 1217 (CallDebugger)

Code: Select all

Assert(DateQ() = Date())
But as I'm on a MacBook M1, that may be the reason...
For getting the current time, DateQ() has to call an API function of the respective operating system.
Unfortunately, I don't know anything about Mac OS, and I don't have a possibility to test code on a Mac computer.
I didn't mention that explicitely. However, at the very top of the code, it reads:

Code: Select all

; successfully tested with
; [v] PB 6.02 LTS (x86) on Windows 11         – only ASM backend
; [v] PB 6.02 LTS (x64) on Windows 11         – both ASM and C backend (code not optimized!)
; [v] PB 6.03 beta 3 (x64) on Linux Mint 20.3 – both ASM and C backend (code not optimized!)
I believed that not mentioning Mac there was clear enough. Obviously I was wrong.

In the code, there are the following lines:

Code: Select all

405            CompilerCase #PB_OS_MacOS
406               g_Error = #Err_OSnotSupported
407               ProcedureReturn -1
That's why you got an error. I am sorry for any inconveniences.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: DateEx: Extended Date library

Post by Mindphazer »

Little John wrote: Tue Aug 01, 2023 8:27 pm For getting the current time, DateQ() has to call an API function of the respective operating system.
Unfortunately, I don't know anything about Mac OS, and I don't have a possibility to test code on a Mac computer.
I didn't mention that explicitely. However, at the very top of the code, it reads:

Code: Select all

; successfully tested with
; [v] PB 6.02 LTS (x86) on Windows 11         – only ASM backend
; [v] PB 6.02 LTS (x64) on Windows 11         – both ASM and C backend (code not optimized!)
; [v] PB 6.03 beta 3 (x64) on Linux Mint 20.3 – both ASM and C backend (code not optimized!)
I believed that not mentioning Mac there was clear enough. Obviously I was wrong.
Not your fault.
Mine. I did read the top of your code. I just.... tried :mrgreen:
Little John wrote: Tue Aug 01, 2023 8:27 pm In the code, there are the following lines:

Code: Select all

405            CompilerCase #PB_OS_MacOS
406               g_Error = #Err_OSnotSupported
407               ProcedureReturn -1
That's why you got an error. I am sorry for any inconveniences.
Don't be sorry, it's okay. I'll try your code tomorrow on Windows
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
Little John
Addict
Addict
Posts: 4775
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: DateEx: Extended Date library

Post by Little John »

New version 1.21, 2023-08-10

Fixed
  • DayOfWeekQ() returned a wrong result for 0001-01-01. :-)
Changed
  • Better error message on operating systems other than Windows or Linux.
  • On success, the return value of DateDiff() is not always 1 anymore, but the difference endTime - startTime. This makes usage of the function sometimes more convenient.
  • Slightly improved comments.
New
  • Public macro Today(), that returns the date value for the current day at time 00:00:00. This comes in handy when calculating with whole days, regardless of the time of day.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: DateEx: Extended Date library

Post by Mindphazer »

Little John wrote: Thu Aug 10, 2023 2:30 pm [*]Better error message on operating systems other than Windows or Linux.
That's for me, right ? :mrgreen:
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
Little John
Addict
Addict
Posts: 4775
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: DateEx: Extended Date library

Post by Little John »

:-)
You really pointed out a weakness. Thanks for that!
Little John
Addict
Addict
Posts: 4775
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: DateEx: Extended Date library

Post by Little John »

jassing wrote: Wed Jul 05, 2023 6:15 pm I tried with pb6.00, asm - line 412 _splitdateq() "Procedure stack has been corrupted"
As I wrote before, I wasn't able to reproduce that bug on Windows 11, but today I stumbled across it on Linux Mint (x64). :o


New version 1.22, 2023-09-10

Fixed
  • One parameter of one procedure was a pointer to a not fitting structure,
    under some circumstances resulting in the error “Procedure stack has been corrupted.”
Quin
Addict
Addict
Posts: 1122
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: DateEx: Extended Date library

Post by Quin »

I was actually going to write something that makes Date() and Weekday() much nicer, but you beat me to it! Thanks a ton! :)
Little John
Addict
Addict
Posts: 4775
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: DateEx: Extended Date library

Post by Little John »

Hi Quin,

I'm glad that the code is useful for you. :-)
Little John
Addict
Addict
Posts: 4775
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: DateEx: Extended Date library

Post by Little John »

Demo program

Code: Select all

; tested with PB 5.73 LTS, 6.04 LTS
; Create an annual overview calendar

EnableExplicit

XIncludeFile "DateEx.pbi"
UseModule DateEx


Procedure.i CalcOneMonth (year.i, month.i, Array monthCal.i(2))
   ; in : year, month
   ; out: monthCal()
   ;      return value: column index of last week in this month
   Protected.i firstMo, lastMo, lastWeek, week, day, lastDay, weekDay
   
   ; -- Preparation
   firstMo = DayQ(nth_WeekDay_InMonth(#Monday,  1, year, month))
   lastMo  = DayQ(nth_WeekDay_InMonth(#monday, -1, year, month))
   lastWeek = Int((lastMo - firstMo) / 7)
   If firstMo > 1
      lastWeek + 1
      day = firstMo
   Else   
      day = firstMo + 7
   EndIf   
   Dim monthCal(7, lastWeek)
   
   ; -- Week numbers
   monthCal(0, 0) = WeekNumber(DateQ(year, month, 1))
   For week = 1 To lastWeek
      monthCal(0, week) = WeekNumber(DateQ(year, month, day))
      day + 7
   Next
   
   ; -- Days
   lastDay = DaysInMonth(year, month)
   week = 0
   For day = 1 To lastDay
      weekDay = DayOfWeekQ(DateQ(year, month, day), #True)
      monthCal(weekDay, week) = day
      If weekDay = #SundayISO
         week + 1
      EndIf   
   Next
   
   ProcedureReturn lastWeek
EndProcedure


Procedure Calendar (year.i)
   Protected Dim monthCal.i(7, 0)
   Protected.i month, lastWeek, week, weekDay, day
   Protected line$
   
   Debug "# " + year
   Debug ""
   
   For month = 1 To 12
      lastWeek = CalcOneMonth(year, month, monthCal())
      
      ; -- Markdown output
      Debug "## " + LocalizedMonthName(month)
      Debug ""
      
      line$ = "|w |"              ; week number
      For week = 0 To lastWeek
         line$ + RSet(Str(monthCal(0, week)), 2) + "|"
      Next
      Debug line$
      
      line$ = "|--|"
      For week = 0 To lastWeek
         line$ + "--|"
      Next   
      Debug line$
      
      For weekDay = 1 To 7
         line$ = "|" + LocalizedDayName(weekDay, #True) + "|"
         For week = 0 To lastWeek
            day = monthCal(weekDay, week)
            If day = 0
               line$ + "  |"
            Else
               line$ + RSet(Str(day), 2) + "|"
            EndIf   
         Next   
         Debug line$
      Next   
      Debug ""
   Next
EndProcedure


Calendar(2024)
Last edited by Little John on Fri Dec 29, 2023 6:40 pm, edited 1 time in total.
Little John
Addict
Addict
Posts: 4775
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: DateEx: Extended Date library

Post by Little John »

New version 1.30, 2023-12-29

Fixed
  • Bug in Procedure AddDateQ() with #PB_Date_Month
  • Bug in Procedure DateDiff()
Changed
  • Procedure Julian2PureDate() now does bounds checking.
  • additional error codes for more precise information
  • cosmetic changes
  • improved some comments
New
  • support for Mac OS
  • some additional examples
  • demo program how to easily create an annual overview calendar (see previous post)
victorprogra
User
User
Posts: 24
Joined: Wed Jul 06, 2022 3:49 pm

Re: DateEx: Extended Date library

Post by victorprogra »

Thank you very much!

Runs Perfect! on macOS (intel)
Tested version 1.30, 2023-12-29
Also tested Demo Program
on:
PureBasic 6.04 LTS - C Backend (MacOS X - x64)
IDE build on 12/14/2023 [16:02] by fred
Branch: v6.04 Revision: 1fa823f13c64

MacBook Pro 16-inch, 2019
2.3 GHz 8-Core Intel Core i9
16GB 2667 MHz DDR4
Storage 1TB
macOS Ventura 13.6.3 (22G436)
Post Reply