...
Dates in Macro
Macro has specific date-handling abiltiesabilities. Dates are a built-in data type which in fact describe both a date and a time.
...
Create a new Macro icon, rename it to dates and define a date:
Code Block | ||
---|---|---|
| ||
dd1 = 2015-03-11 print(type(dd1)) print(dd1) |
Try adding a time:
Code Block |
---|
dd1 = 2015-03-11 09:00 |
Converting numbers into dates
The date()
function converts numbers into dates using the same syntax that MARS understands. For example:
d1 = date(20150105)
today = date(0)
yesterday = date(-1)
This syntax can be useful if reading dates from a text file or some other source.
Again, the time will be 00:00 unless we add it. We can consider the time to be a fraction of a day:
midday = date(20150105.5)
Use this syntax to add another variable, d2
, which contains the date and time for 13:00h at 2015-03-13. Print it to check it.
Date arithmetic
When dealing with dates, the number 1 represents one day. So the expression d1 + 1 gives a date one day later than day 1. To compute the difference, in days, between two dates, it's simply:
diff = d2 - d1
Times can be added as fractions of days, and there are some helper functions too:
d1 = d1 + 0.5 # add 12 hours
d1 = d1 + hour(12) # hour(12) returns 0.5
d1 = d1 + hour(23) + minute(58) + second(0) # 2 minutes to midnight