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:
Select All      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: 480)
#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:
Select All      Help
DateTime x.FromComputerTime
out x.ToStrFormat("{ss}.{F}")
#4
I have this function...

Code:
Select All      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:
Select All      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:
Select All      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:
Select All      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:
Select All      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


Forum Jump:


Users browsing this thread: 1 Guest(s)