Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Better date/time functions (for QM < 2.3.2)
#1
For QM 2.3.1 and older. It is available in QM 2.3.2, with bug fixes.

Macro DateTime help
Code:
Copy      Help
;A variable of class DateTime represents a date and time.
;Class DateTime is similar to DATE.
;;;Advantages: more functions, better precision (supports milliseconds and microseconds).
;;;Disadvantages: there is no implicit conversion from/to str and some other types (need to use ToStr etc).

;Internally date/time is stored as long, in FILETIME format.
;It is number of ticks since January 1, 1601. A tick is 0.1 microseconds.

;A DateTime variable stores an absolute time. It doesn't store a time span (difference between two times).
;A time span can be stored in a variable of type long, in FILETIME format (number of 0.1 microsecond intervals). Can be negative.

;Also you can use operators with DateTime variables.
;Below are examples. Assume x and y are variables of DateTime type, and ts is variable of type long.
;;;x=y ;;set x = y
;;;ts=x-y ;;set ts = time span between x and y
;;;x=x+ts ;;add time span to x
;;;if(x<y) ... ;;if x is less than y

;Also there are several global (non member) functions to work with date/time. TimeSpanFromStr, DaysInMonth, etc.
;To show list of these functions, type "DateTime.".

;Most functions throw error if passed or calculated date/time values are out of range.
;Valid ranges of date/time parts are:
;;;year - 1601 to 9999. Actually may be stored dates up to about 30000, but some functions don't support it.
;;;month - 1 to 12.
;;;day - 1 to 28, 29, 30 or 31.
;;;hour - 0 to 23.
;;;minute - 0 to 59.
;;;second - 0 to 59.
;;;millisecond - 0 to 999.
;;;microsecond - 0 to 999.9.
;;;day of week - 0 (Sunday) to 6 (Saturday).
;;;day of year - 1 to 365 or 366.



;EXAMPLES

#compile "__DateTime"
DateTime x
x.FromComputerTime
out x.ToStr(8)

FUNCTIONS:
DateTime.FromComputerTime
DateTime.FromParts
DateTime.GetParts
DateTime.GetDatePart
DateTime.GetTimePart
DateTime.GetDayOfYear
DateTime.FromStr
DateTime.ToStr
DateTime.ToStrFormat
DateTime.FromDATE
DateTime.ToDATE
DateTime.FromFILETIME
DateTime.ToFILETIME
DateTime.FromSYSTEMTIME
DateTime.ToSYSTEMTIME
DateTime.AddParts
DateTime.AddStr
DateTime.AddMonths
DateTime.AddYears
DateTime.UtcFromLocal
DateTime.UtcToLocal
DateTime.SetComputerTime
TimeSpanFromParts
TimeSpanGetParts
TimeSpanGetPartsTotal
TimeSpanFromStr
TimeSpanToStr
DaysInMonth


Attached Files
.qml   DateTime.qml (Size: 17.55 KB / Downloads: 878)
#2
Looks great...!

Could you add "32 time only with s and ms" to the flags for DateTime.ToStr
Otherwise I have to do a bunch of trimming up.

Thank you so much!
Jimmy Vig
#3
Macro Macro1272
Code:
Copy      Help
DateTime x.FromComputerTime
out x.ToStrFormat("{ss}.{F}")
#4
I have this function...

Code:
Copy      Help
str sLength="800.32"

ARRAY(str) arr
int i nt
nt = tok(sLength arr 2 ".")
str MS.from("." arr[1])
err
,MS.from(".")
double iMS=val(MS 2);err
MS.format("%.03f" iMS)
MS.remove(0 1)

int Length=val(sLength 2)
if Length>86400
,ret
int Hours=Length/3600
int Minutes=(Length-(Hours*3600))/60
int Seconds=(Length-(Hours*3600)-(Minutes*60))
str HMS.from(Hours ":" Minutes ":" Seconds MS)
DateTime x
x.FromStr(HMS)
sLength=x.ToStrFormat("{HH}:{mm}:{ss}.{F}")
out sLength

I would like to use it as a member function for a string variable so I can call it like:
str Length.GetDateTime("800.32")

or better yet...
A member function of DateTime...
str Length="800.32"
DateTime x.SecondsToDateTime(Length)

Thanks,
Jimmy Vig
#5
Code:
Copy      Help
out
str Length="800.32"
ARRAY(str) arr
tok(Length arr 2 ".")
int ss=val(arr[0]);err
int ms=val(arr[1]);err
long TimeSpan=TimeSpanFromParts(0 0 0 ss ms)
DateTime s=TimeSpan
out s.ToStrFormat("{HH}:{mm}:{ss}.{F}")

I just noticed that this example is converting .32 to .032 for the ms
Needs to use "320" instead of 32.

Good Night,
jimmy Vig
#6
Updated. Now TimeSpanFromStr and .AddStr support more formats.

Macro Macro1275
Code:
Copy      Help
str s="800.32"
out TimeSpanToStr(TimeSpanFromStr(s) 2)
#7
Can this class convert:

[hh]h [mm]m [ss]s -----> hh:mmConfuseds

ex.

10m 15s ----> 00:10:15

?
#8
No.
#9
Do you know a easy way to do it?
#10
Macro Macro1269
Code:
Copy      Help
str timeSpanStr="10m 15s"

ARRAY(str) a
tok timeSpanStr a
int h m s i j k
for i 0 a.len
,j=val(a[i] 0 k)
,sel a[i][k]
,,case 'h' h=j
,,case 'm' m=j
,,case 's' s=j

long ts=DateTime.TimeSpanFromParts(0 h m s)
out ts
#11
Thanks.

Macro Macro4
Code:
Copy      Help
str timeSpanStr="10m 15s"

ARRAY(str) a
tok timeSpanStr a
int h m s i j k
for i 0 a.len
,j=val(a[i] 0 k)
,sel a[i][k]
,,case 'h' h=j
,,case 'm' m=j
,,case 's' s=j

long ts=TimeSpanFromParts(0 h m s)
out ts
str t=TimeSpanToStr(ts)
out t
#12
I'm trying to figure out a way to find just the number of days between two dates... here's my logic...
 
Code:
Copy      Help
str Today = "10/26/2022"
str Target = "12/25/2022"

int DaysUntil = Days(Target) - Days(Today)

out F"There are {DaysUntil} Christmas left"

#sub Days
function str'DateStr
;;   Date strings can be either MM/DD/YYYY or MM/DD/YY
     int NumDays
;;   Calculate the number of days since 1/1/1970
     NumDays = ??????(DateStr)
     ret NumDays

I guess I still don't quite understand how to use the DateTime functions properly... I'm sure Gintaras knows exactly how they work thou.  I've scoured over the Help and there's just not enough examples to clear it up for me I suppose.  Any help would be appreciated!
#13
Code:
Copy      Help
str Today = "10/26/2022"
str Target = "12/25/2022"

DATE d1(Today) d2(Target)
int DaysUntil = d2 - d1

out F"There are {DaysUntil} Christmas left"

or

Code:
Copy      Help
str Today = "10/26/2022"
str Target = "12/25/2022"

DateTime d1.FromStr(Today) d2.FromStr(Target)
int DaysUntil; DateTime.TimeSpanGetParts(d2-d1 DaysUntil)

out F"There are {DaysUntil} Christmas left"
#14
You are the best G... I've been programming for over 40 years now in various languages, but I'm sure there are times that you are like "These guys must be newbies or something" in your head.  Your response time is unbelievable as well... My hat is off to you sir!  Having been in tech support, I know how frustrating it can be at times.  Might I suggest an additional function to add to your collection... DateTime.DayOfWeek which would return a 3 character weekday "SUN,MON,TUE,WED,THU,FRI,SAT" for any given date?  Just a thought! Smile
#15
Code:
Copy      Help
;str s.timeformat("{ddd}")
;out s

DateTime d.FromComputerTime
str s = d.ToStrFormat("{ddd}" )
out s
#16
Thanks G... you're awesome!


Forum Jump:


Users browsing this thread: 1 Guest(s)