How do I convert seconds since Epoch to current date and time? - time

I know I posted this a while ago, but I figured out the solution. I wrote this code for a game called Roblox, but I'm just posting the code here in case anyone else who has this same problem needs a solution. Anyways, here's the code:
outputTime = true -- true: will print the current time to output window. false: won't print time
createVariable = true -- true: creates variables under game.Lighting. false: won't create variables
-----------------------------------------------------------------------------------------------
--DO NOT EDIT BELOW----------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
if(createVariable) then
yearVar = Instance.new("IntValue", game.Lighting)
yearVar.Name = "Year"
yearVar.Value = 0
monthVar = Instance.new("IntValue", game.Lighting)
monthVar.Name = "Month"
monthVar.Value = 0
dayVar = Instance.new("IntValue", game.Lighting)
dayVar.Name = "Day"
dayVar.Value = 0
hourVar = Instance.new("IntValue", game.Lighting)
hourVar.Name = "Hour"
hourVar.Value = 0
minuteVar = Instance.new("IntValue", game.Lighting)
minuteVar.Name = "Minute"
minuteVar.Value = 0
secondVar = Instance.new("IntValue", game.Lighting)
secondVar.Name = "Second"
secondVar.Value = 0
dayOfWeek = Instance.new("StringValue", game.Lighting)
dayOfWeek.Name = "DayOfWeek"
dayOfWeek.Value = "Thursday"
end
function giveZero(data)
if string.len(data) <= 1 then
return "0" .. data
else
return data
end
end
function hasDecimal(value)
if not(value == math.floor(value)) then
return true
else
return false
end
end
function isLeapYear(year)
if(not hasDecimal(year / 4)) then
if(hasDecimal(year / 100)) then
return true
else
if(not hasDecimal(year / 400)) then
return true
else
return false
end
end
else
return false
end
end
local eYear = 1970
local timeStampDayOfWeak = 5
local secondsInHour = 3600
local secondsInDay = 86400
local secondsInYear = 31536000
local secondsInLeapYear = 31622400
local monthWith28 = 2419200
local monthWith29 = 2505600
local monthWith30 = 2592000
local monthWith31 = 2678400
local monthsWith30 = {4, 6, 9, 11}
local monthsWith31 = {1, 3, 5, 7, 8, 10, 12}
local daysSinceEpoch = 0
local DOWAssociates = {"Tursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"}
while(true) do
now = tick()
year = 1970
secs = 0
daysSinceEpoch = 0
while((secs + secondsInLeapYear) < now or (secs + secondsInYear) < now) do
if(isLeapYear(year+1)) then
if((secs + secondsInLeapYear) < now) then
secs = secs + secondsInLeapYear
year = year + 1
daysSinceEpoch = daysSinceEpoch + 366
end
else
if((secs + secondsInYear) < now) then
secs = secs + secondsInYear
year = year + 1
daysSinceEpoch = daysSinceEpoch + 365
end
end
end
secondsRemaining = now - secs
monthSecs = 0
yearIsLeapYear = isLeapYear(year)
month = 1 -- January
while((monthSecs + monthWith28) < secondsRemaining or (monthSecs + monthWith30) < secondsRemaining or (monthSecs + monthWith31) < secondsRemaining) do
if(month == 1) then
if((monthSecs + monthWith31) < secondsRemaining) then
month = 2
monthSecs = monthSecs + monthWith31
daysSinceEpoch = daysSinceEpoch + 31
else
break
end
end
if(month == 2) then
if(not yearIsLeapYear) then
if((monthSecs + monthWith28) < secondsRemaining) then
month = 3
monthSecs = monthSecs + monthWith28
daysSinceEpoch = daysSinceEpoch + 28
else
break
end
else
if((monthSecs + monthWith29) < secondsRemaining) then
month = 3
monthSecs = monthSecs + monthWith29
daysSinceEpoch = daysSinceEpoch + 29
else
break
end
end
end
if(month == 3) then
if((monthSecs + monthWith31) < secondsRemaining) then
month = 4
monthSecs = monthSecs + monthWith31
daysSinceEpoch = daysSinceEpoch + 31
else
break
end
end
if(month == 4) then
if((monthSecs + monthWith30) < secondsRemaining) then
month = 5
monthSecs = monthSecs + monthWith30
daysSinceEpoch = daysSinceEpoch + 30
else
break
end
end
if(month == 5) then
if((monthSecs + monthWith31) < secondsRemaining) then
month = 6
monthSecs = monthSecs + monthWith31
daysSinceEpoch = daysSinceEpoch + 31
else
break
end
end
if(month == 6) then
if((monthSecs + monthWith30) < secondsRemaining) then
month = 7
monthSecs = monthSecs + monthWith30
daysSinceEpoch = daysSinceEpoch + 30
else
break
end
end
if(month == 7) then
if((monthSecs + monthWith31) < secondsRemaining) then
month = 8
monthSecs = monthSecs + monthWith31
daysSinceEpoch = daysSinceEpoch + 31
else
break
end
end
if(month == 8) then
if((monthSecs + monthWith31) < secondsRemaining) then
month = 9
monthSecs = monthSecs + monthWith31
daysSinceEpoch = daysSinceEpoch + 31
else
break
end
end
if(month == 9) then
if((monthSecs + monthWith30) < secondsRemaining) then
month = 10
monthSecs = monthSecs + monthWith30
daysSinceEpoch = daysSinceEpoch + 30
else
break
end
end
if(month == 10) then
if((monthSecs + monthWith31) < secondsRemaining) then
month = 11
monthSecs = monthSecs + monthWith31
daysSinceEpoch = daysSinceEpoch + 31
else
break
end
end
if(month == 11) then
if((monthSecs + monthWith30) < secondsRemaining) then
month = 12
monthSecs = monthSecs + monthWith30
daysSinceEpoch = daysSinceEpoch + 30
else
break
end
end
end
day = 1 -- 1st
daySecs = 0
daySecsRemaining = secondsRemaining - monthSecs
while((daySecs + secondsInDay) < daySecsRemaining) do
day = day + 1
daySecs = daySecs + secondsInDay
daysSinceEpoch = daysSinceEpoch + 1
end
hour = 0 -- Midnight
hourSecs = 0
hourSecsRemaining = daySecsRemaining - daySecs
while((hourSecs + secondsInHour) < hourSecsRemaining) do
hour = hour + 1
hourSecs = hourSecs + secondsInHour
end
minute = 0 -- Midnight
minuteSecs = 0
minuteSecsRemaining = hourSecsRemaining - hourSecs
while((minuteSecs + 60) < minuteSecsRemaining) do
minute = minute + 1
minuteSecs = minuteSecs + 60
end
second = math.floor(now % 60)
year = giveZero(year)
month = giveZero(month)
day = giveZero(day)
hour = giveZero(hour)
minute = giveZero(minute)
second = giveZero(second)
remanderForDOW = daysSinceEpoch % 7
DOW = DOWAssociates[remanderForDOW + 1]
if(createVariable) then
yearVar.Value = year
monthVar.Value = month
dayVar.Value = day
hourVar.Value = hour
minuteVar.Value = minute
secondVar.Value = second
dayOfWeek.Value = DOW
end
if(outputTime) then
str = "Year: " .. year .. ", Month: " .. month .. ", Day: " .. day .. ", Hour: " .. hour .. ", Minute: " .. minute .. ", Second: ".. second .. ", Day of Week: " .. DOW
print(str)
end
wait(1)
end
----ORIGINAL POST----
What are the formulas for calculating the following given no resources except the seconds since Epoch?
Here's a list of what I need:
Current Month of year Ex: 7
Current day of month Ex: 25
Current day of week Ex: Thursday (1-7 would be acceptable)
Current hour of day Ex: 22
Current minute of hour Ex: 34
Current second of minute: 07

Here is some Lua code adapted from some C code found by Google. It does not handle timezones or Daylight Saving Time and so the outputs refers to Universal Coordinated Time (UTC).
-- based on http://www.ethernut.de/api/gmtime_8c_source.html
local floor=math.floor
local DSEC=24*60*60 -- secs in a day
local YSEC=365*DSEC -- secs in a year
local LSEC=YSEC+DSEC -- secs in a leap year
local FSEC=4*YSEC+DSEC -- secs in a 4-year interval
local BASE_DOW=4 -- 1970-01-01 was a Thursday
local BASE_YEAR=1970 -- 1970 is the base year
local _days={
-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364
}
local _lpdays={}
for i=1,2 do _lpdays[i]=_days[i] end
for i=3,13 do _lpdays[i]=_days[i]+1 end
function gmtime(t)
print(os.date("!\n%c\t%j",t),t)
local y,j,m,d,w,h,n,s
local mdays=_days
s=t
-- First calculate the number of four-year-interval, so calculation
-- of leap year will be simple. Btw, because 2000 IS a leap year and
-- 2100 is out of range, this formula is so simple.
y=floor(s/FSEC)
s=s-y*FSEC
y=y*4+BASE_YEAR -- 1970, 1974, 1978, ...
if s>=YSEC then
y=y+1 -- 1971, 1975, 1979,...
s=s-YSEC
if s>=YSEC then
y=y+1 -- 1972, 1976, 1980,... (leap years!)
s=s-YSEC
if s>=LSEC then
y=y+1 -- 1971, 1975, 1979,...
s=s-LSEC
else -- leap year
mdays=_lpdays
end
end
end
j=floor(s/DSEC)
s=s-j*DSEC
local m=1
while mdays[m]<j do m=m+1 end
m=m-1
local d=j-mdays[m]
-- Calculate day of week. Sunday is 0
w=(floor(t/DSEC)+BASE_DOW)%7
-- Calculate the time of day from the remaining seconds
h=floor(s/3600)
s=s-h*3600
n=floor(s/60)
s=s-n*60
print("y","j","m","d","w","h","n","s")
print(y,j+1,m,d,w,h,n,s)
end
local t=os.time()
gmtime(t)
t=os.time{year=1970, month=1, day=1, hour=0} gmtime(t)
t=os.time{year=1970, month=1, day=3, hour=0} gmtime(t)
t=os.time{year=1970, month=1, day=2, hour=23-3, min=59, sec=59} gmtime(t)

The formula is not simple for a few reasons, especially leap years. You should probably use the date function on this page rather than trying to calculate it yourself.

You could use luatz
x = 1234567890
t = require "luatz.timetable".new_from_timestamp ( x )
print(t.year,t.month,t.day,t.hour,t.min,t.sec,t.yday,t.wday)
-- Or just
print(t:rfc_3339())

This is how I do it.
> time0=os.time()
> time0
1571439964
> os.date("%Y%m%d%H%M%S",time0)
20191019120604
>

os.date is a standard Lua function, if passing the first argument as "%t", it will return a table containing the following fields: year (four digits), month (1--12), day (1--31), hour (0--23), min (0--59), sec (0--61), wday (weekday, Sunday is 1), yday (day of the year), and isdst (daylight saving flag, a boolean).
Give it a test:
time = os.time()
print("time since epoch: " .. time)
date = os.date("*t", time)
print("year: " .. date.year)
print("month: " .. date.month)
print("day: " .. date.day)
print("hour: " .. date.hour)
print("minute: " .. date.min)
print("second: " .. date.sec)
print("weekday: " .. date.wday)
Output:
time since epoch: 1374826427
year: 2013
month: 7
day: 26
hour: 16
minute: 13
second: 47
weekday: 6

A much faster solution would be to use my method, which I haven't really seen anyone else use because most have access to os.date()
Since I do not have access to os.date(), here is my solution:
local tabIndexOverflow = function(seed, table)
-- This subtracts values from the table from seed until an overflow
-- This can be used for probability :D
for i = 1, #table do
if seed - table[i] <= 0 then
return i, seed
end
seed = seed - table[i]
end
end
local getDate = function(unix)
-- Given unix date, return string date
assert(unix == nil or type(unix) == "number" or unix:find("/Date%((%d+)"), "Please input a valid number to \"getDate\"")
local unix = (type(unix) == "string" and unix:match("/Date%((%d+)") / 1000 or unix or os.time()) -- This is for a certain JSON compatability. It works the same even if you don't need it
local dayCount, year, days, month = function(yr) return (yr % 4 == 0 and (yr % 100 ~= 0 or yr % 400 == 0)) and 366 or 365 end, 1970, math.ceil(unix/86400)
while days >= dayCount(year) do days = days - dayCount(year) year = year + 1 end -- Calculate year and days into that year
month, days = tabIndexOverflow(days, {31,(dayCount(year) == 366 and 29 or 28),31,30,31,30,31,31,30,31,30,31}) -- Subtract from days to find current month and leftover days
-- hours = hours > 12 and hours - 12 or hours == 0 and 12 or hours -- Change to proper am or pm time
-- local period = hours > 12 and "pm" or "am"
-- Formats for you!
-- string.format("%d/%d/%04d", month, days, year)
-- string.format("%02d:%02d:%02d %s", hours, minutes, seconds, period)
return {Month = month, day = days, year = year, hours = math.floor(unix / 3600 % 24), minutes = math.floor(unix / 60 % 60), seconds = math.floor(unix % 60)}
end
You are, however, on your own when it comes to finding the day of the week. I never cared to find whether it be the day of Thor or the day of Frige.

Related

How can I create a specific time interval in Ruby?

What I have tried so far ...
start_hour = 7
start_minute = 0 * 0.01
end_hour = 17
end_minute = 45 * 0.01
step_time = 25
start_time = start_hour + start_minute
end_time = end_hour + end_minute
if step_time > 59
step_time = 1 if step_time == 60
step_time = 1.3 if step_time == 90
step_time = 2 if step_time == 120
else
step_time *= 0.01
end
hours = []
(start_time..end_time).step(step_time).map do |x|
next if (x-x.to_i) > 0.55
hours << '%0.2f' % x.round(2).to_s
end
puts hours
If I enter the step interval 0, 5, 10, 20, I can get the time interval I want. But if I enter 15, 25, 90, I can't get the right range.
You currently have:
end_hour = 17
end_minute = 45 * 0.01
end_time = end_hour + end_minute
#=> 17.45
Although 17.45 looks like the correct value, it isn't. 45 minutes is 3 quarters (or 75%) of an hour, so the correct decimal value is 17.75.
You could change your code accordingly, but working with decimal hours is a bit strange. It's much easier to just work with minutes. Instead of turning the minutes into hours, you turn the hours into minutes:
start_hour = 7
start_minute = 0
start_time = start_hour * 60 + start_minute
#=> 420
end_hour = 17
end_minute = 45
end_time = end_hour * 60 + end_minute
#=> 1065
The total amount of minutes can easily be converted back to hour-minute pairs via divmod:
420.divmod(60) #=> [7, 0]
1065.divmod(60) #=> [17, 45]
Using the above, we can traverse the range without having to convert the step interval:
def hours(start_time, end_time, step_time)
(start_time..end_time).step(step_time).map do |x|
'%02d:%02d' % x.divmod(60)
end
end
hours(start_time, end_time, 25)
#=> ["07:00", "07:25", "07:50", "08:15", "08:40", "09:05", "09:30", "09:55",
# "10:20", "10:45", "11:10", "11:35", "12:00", "12:25", "12:50", "13:15",
# "13:40", "14:05", "14:30", "14:55", "15:20", "15:45", "16:10", "16:35",
# "17:00", "17:25"]
hours(start_time, end_time, 90)
#=> ["07:00", "08:30", "10:00", "11:30", "13:00", "14:30", "16:00", "17:30"]

Ruby - Get time at start of next minute

I'm looking for a concise way to get a Ruby Time object representing the top of the next minute (and hour/day/month/year, if possible). I want this to work in a pure Ruby environment, so the Rails function Time.change or similar doesn't fit the bill.
At first this seems simple - just add 1 to Time.now, but there are edge cases where if, for example, you try to instantiate a Time object with Time.now.min + 1 when the current minute is 59, you get an ArgumentError: min out of range. This goes for hour, day, and month as well.
I have some lengthy code that does the job. It's ugly, but I'm just experimenting:
def add_minute
now = Time.local
year = now.year
month = now.month
day = now.day
hour = now.hour
min = now.min
if now.min == 59
if now.hour == 23
if now.day == Date.civil(now.year, now.month, -1).day
if month == 12
year = year + 1
month = 1
day = 1
hour = 0
min = 0
else
month = now.month + 1
day = 1
hour = 0
min = 0
end
else
day = now.day + 1
hour = 0
min = 0
end
else
hour = now.hour + 1
min = 0
end
else
min = now.min + 1
end
Time.local year, month, day, hour, min, 0
end
This seems absurdly verbose for what seems like it should be a simple or built-in task, but I haven't found a native Ruby solution. Does one exist?
You could convert the Time object to UNIX epoch time (seconds since 1970) using #to_i, add 60 s, and then convert back to a Time object.
time_unix = Time.now.to_i
time_unix_one_min_later = time_unix + 60
time_one_min_later = t = Time.at(time_unix_one_min_later)
time_one_min_later_rounded_down = Time.new(t.year, t.month, t.day, t.hour, t.min)
EDIT: Even shorter - you can just add integer seconds to Time.now directly:
time_one_min_later = t = Time.now + 60
time_one_min_later_rounded_down = Time.new(t.year, t.month, t.day, t.hour, t.min)
EDIT 2: One-liner - just subtract Time.now.sec:
time_one_min_later_rounded_down = Time.now + 60 - Time.now.sec
Other option, given one second to midnight:
require 'time'
now = Time.strptime('2018-12-31 23:59:59', '%Y-%m-%d %H:%M:%S')
Within one minute:
Time.at(now + 60) #=> 2019-01-01 00:00:59 +0100
Time.at(now + 60 - now.sec) #=> 2019-01-01 00:00:00 +0100
You get: # HAPPY NEW YEAR!
Ruby has built in methods for adding months (>>) and days (+). A year is 12 months, and an hour is 1/24th of a day.
require 'date'
def add_time(time, year: 0 ,month: 0, day: 0, hour: 0, minute: 0)
time >>= 12*year
time >>= month
time += day
time += Rational(hour,24) # or (hour/24.0) if you dislike rationals
time += Rational(minute, 24*60) # (minute/24.0*60) if you dislike rationals
end
p t = DateTime.now
p add_time(t, year: 1, minute: 30)
Not that clean without ActiveSupport:
new_date = (DateTime.now + 1.to_f / (60*24))
DateTime.new(new_date.year, new_date.month, new_date.day, new_date.hour, new_date.minute)
We can make this calculation easier to understand by getting the current number of seconds we are through the day. (optional)
DateTime.current.to_i gives us the number of seconds since 1970
DateTime.current.to_i - DateTime.current.beginning_of_day.to_i gives us the number of seconds since the start of the day.
(((number_of_seconds_through_the_day + 60)/60) * 60) gives us the number of seconds we will be at when the next minute starts
Then we subtract the two to give us the number of seconds until the top of the next minute.
If we want the exact time at start of the next minute then we can do:
DateTime.current + seconds_until_start_of_the_next_minute.seconds
def seconds_until_start_of_the_next_minute
number_of_seconds_through_the_day = DateTime.current.to_i - DateTime.current.beginning_of_day.to_i
number_of_seconds_through_the_day_at_next_minute = (((number_of_seconds_through_the_day + 60)/60) * 60)
seconds_until_next_minute_starts = number_of_seconds_through_the_day_at_next_minute - number_of_seconds_through_the_day
return seconds_until_next_minute_starts
end

3 condition python while loop regarding checking if two dates are the same

I'm currently working on this problem of counting how many days between two dates including leap years.
However it keeps skipping the loop from the beginning, even though the two months and days aren't the same?
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
day_count = 0
while (year2 != year1) and (month2 != month1) and (day2! = day 1):
# print "in loop" // tester
if (day2 != 0):
day2 = day2 - 1
day_count = day_count + 1
else:
if(month2 != 0):
month2 = month2 - 1
if month2 == (9 or 4 or 6 or 11):
day2 = 30
if month2 == 2:
day2 = 28
if (month2 == 2) and (year2 % 4):
day2 = 29
else:
day2 == 31
else:
year2 = year2 - 1
month2 = 12
#print day_count //tester
return day_count
# Test routine
def test():
test_cases = [((2012,1,1,2012,2,28), 58),
((2012,1,1,2012,3,1), 60),
((2011,6,30,2012,6,30), 366),
((2011,1,1,2012,8,8), 585 ),
((1900,1,1,1999,12,31), 36523)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()
Here's one way.
>>> from datetime import datetime
>>> diff = datetime(2012, 2, 28)-datetime(2012, 1, 1)
>>> diff.days
58

How to convert minutes to days, hours, and minutes?

I am a beginner to Python so I do not know a lot of terms or anything really. Can I ask on how to convert minutes to hours and minutes
EX: 75 minutes ->0 days, 1 hour, 15 minutes
print("Welcome to the Scheduler!")
print("What is your name?")
name = input()
print("How many chocolates are there in the order?")
chocolates = input()
print("How many chocolate cakes are there in the order?")
chocolate_cakes = input()
print("How many chocolate ice creams are in the order?")
chocolate_ice_creams = input()
total_time = float(chocolates) + float(chocolate_cakes) + float(chocolate_ice_creams)
print("Total Time:")
print("How many minutes do you have before the order is due?")
minutes = input()
extra_time = float(minutes) - float(total_time)
print("Your extra time for this order is", extra_time)
time = extra_time // 60
print("Thank you,", name)
Well if you're given an input in minutes that is greater than equal to 1440 minutes, then you have at least a day. So to handle this (and the other aspects of time) we can use modulus (%).
days = 0
hours = 0
mins = 0
time = given_number_of_minutes
days = time / 1440
leftover_minutes = time % 1440
hours = leftover_minutes / 60
mins = time - (days*1440) - (hours*60)
print(str(days) + " days, " + str(hours) + " hours, " + str(mins) + " mins. ")
This should work.
# Python Program to Convert seconds
# into hours, minutes and seconds
def convert(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%02d" % (hour, minutes, seconds)
# Driver program
n = 12345
print(convert(n))
method = a
import datetime
str(datetime.timedelta(seconds=666))
'0:11:06'
method = b
def convert(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%02d" % (hour, minutes, seconds)
from datetime import datetime
day = minutes = hours = 0
day = datetime.now ()
day = day.day
minutes = datetime.now ()
minutes = minutes.minute
hours = datetime.now ()
hours = hours.hour
print ("It's day" + str (day) + "and it's" + str (minutes) + "minutes and" + str (hours) + "hours.")
This code above does not work I started to make my own version of how this code could help you remember I am an amateur is true
from datetime import datetime
day = minutes = hours = 0
time = datetime.now().minute
days = time / 1440
leftover_minutes = time % 1440
hours = leftover_minutes / 60
mins = time - (days*1440) - (hours*60)
print(str(days) + " days, " + str(hours) + " hours, " + str(mins) + " mins. ")
You actually have to round down values in order to get integers.
import math
def transform_minutes(total_minutes):
days = math.floor(total_minutes / (24*60))
leftover_minutes = total_minutes % (24*60)
hours = math.floor(leftover_minutes / 60)
mins = total_minutes - (days*1440) - (hours*60)
#out format = "days-hours:minutes:seconds"
out = '{}-{}:{}:00'.format(days, hours, mins)
return out
n = int (input())
day = int (n // 1440)
hours = int (n % 1440) // 60
mins = int (n % 1440) % 60
print(day)
print(hours)
print(mins)

Comparing variables to multiple values and using "and" & "or in "If" condition vbscript

I am learning qtp and vbs mostly by myself. I need a function in qtp/vbs server 2008 that ads one day to current date then changes the places of dd and mm and transforms it to a numeric string for qtp to insert it to application under test. This is what I came up with, but the function gives back nothing at the end.
'5) DATEFORMATFUNC
'Function that gets current system date, adds one day to it and transforms it to a string
'usable as a date parameter in UAT "Flight Reservation"
Function DATEFORMATFUNC
'Defining variables
Dim currentday, currentmonth, currentyear, oday, omonth, oyear, NextDayDate, LeapYear
'* Checking if current year is leap year and transforming answer to numeric variant wher 1=True and 0=False
If DatePart("yyyy", Now) mod 4=0 then
LeapYear = 1
Else LeapYear = 0
End If
'* getting current day of month
currentday = DatePart("d", Now)
'* getting current month of year
currentmonth = DatePart("m", Now)
'* reporting to get it into "test results"
Reporter.ReportEvent micDone, "Current month of year is " & currentmonth, currentmonth
'* getting last two numbers of current year
currentyear = Right((DatePart("yyyy", Now)),2)
'* reporting to get it into "test results"
Reporter.ReportEvent micDone, "Current year is " & currentyear, currentyear
'* If currentday is 31(when 31 days in month) then date becomes the first day of next month
If currentmonth = 1 or 3 or 5 or 7 or 8 or 10 and currentday = 31 Then
omonth = currentmonth +1 and oday = 1
'* If currentday is less then 31(when 31 days in month) then adding 1 day to currentday
ElseIf currentmonth = 1 or 3 or 5 or 7 or 8 or 10 or 12 and currentday < 31 Then
oday = currentday + 1
'* if current day is 31 of december then date becomes first of january next year
ElseIf currentmonth = 12 and currentday = 31 Then
omonth = 1 and oday = 1 and oyear = currentyear+1
'* if current day is 30 (when 30 days in month) then date becomes first day of next month
ElseIf omonth = 4 or 6 or 9 or 11 and oday = 30 Then
omonth =currentmonth + 1 and oday = 1
'* if current day is less then 30 (when 30 days in month) then adding 1 day to current day
ElseIf currentmonth = 4 or 6 or 9 or 11 and currentday < 30 Then
oday = currentday +1
'* if it is leap year and current day is 28 of February then adding 1 day to current day
ElseIf currentmonth = 2 and currentday< 29 and LeapYear = 1 Then
oday = currentday +1
'* if it is leap year and date is 29 of February then date jumps to first of March
ElseIf currentmonth = 2 and currentday = 29 and LeapYear = 1 Then
oday = 1 and omonth = 3
'* if it is not leap year and current day is 28 of February then date becomes first of March
ElseIf currentmonth = 2 and currentday = 28 and LeapYear = 0 Then
oday = 1 and omonth = currentmonth + 1
'* if it is not leap year and current day is less then 28 then adding 1 day to current day
ElseIf currentmonth = 2 and currentday < 28 and LeapYear = 0 Then
oday = currentday + 1
'* End
End If
'if the day of the month is a one digit number then concatinating "0" before it
If oday < 10 then
oday = 0 & oday
end If
'if the month of the year is a one digit number then concatenating "0" before it
If omonth < 10 then
omonth = 0 & omonth
End If
'concatinating the parts of the date in a manner that it can be used as a date parameter (mmddyy) for "Flight Reservation"
NextDayDate = omonth & oday & oyear
Reporter.ReportEvent micDone, "Current date vs changed date", "Current date is: " & Date & " Changed date is: " & NextDayDate
'reporting to get it into "test results"
Reporter.ReportEvent micDone, "The parameter inserted to Data Table " , DataTable.Value ("Next_Day_Date", dtGlobalSheet)
'inserting parameter to data table
DataTable.Value ("Next_Day_Date", dtGlobalSheet) = NextDayDate
End Function
Conditions like your
If currentmonth = 1 or 3 or 5 or 7 or 8 or 10 and currentday = 31 Then
don't 'work', because VBScript's logical operators do bitwise operations on numbers (cf. Or, Lippert):
>> For i = 0 To 1
>> If i = 1 Or i = 2 Then
>> WScript.Echo i, "is 1 Or 2"
>> End If
>> Next
>>
1 is 1 Or 2
For i = 0 the condition evaluates to False Or False i.e. False. But in
>> For i = 0 To 1
>> If i = 1 Or 2 Then
>> WScript.Echo i, "is 1 Or 2"
>> End If
>> Next
>>
0 is 1 Or 2
1 is 1 Or 2
the False of i = 0 is taken as the number 0 and bitwise Or'ed to 2 which is 2 and therefore True.
Before you add lots of currentmonth = to your conditionals, consider DateAdd():
>> dtToday = Date()
>> WScript.Echo TypeName(dtToday), dtToday
>> dtTomorrow = DateAdd("d", 1, dtToday)
>> WScript.Echo TypeName(dtTomorrow), dtTomorrow
>>
Date 03.05.2015 ' german locale
Date 04.05.2015
>> For Each dtX In Array(#12/31/2014#, #2/28/2012#, #2/28/2013#)
>> WScript.Echo dtX, DateAdd("d", 1, dtX)
>> Next
>>
31.12.2014 01.01.2015 ' german locale
28.02.2012 29.02.2012
28.02.2013 01.03.2013
And to format a Date as MMDDYYYY:
>> dtX = Date()
>> sX = Right(100 + Month(dtx), 2) & Right(100 + Day(dtX), 2) & Year(dtX)
>> WScript.Echo TypeName(sX), sX
>>
String 05032015

Resources