How can I get the current hour from a linux timestamp? I can get the year, month, day, minute, and second, but not the hour. Any help?
Current code:
local secondsPassed = os ~= nil and os.time() or tick()
local year = 1970 + math.floor(secondsPassed / (86400 * 365.25))
local days = math.floor((secondsPassed % (365.25 * 86400)) / 86400)
days = days + (year - 2011)
local minutes = math.floor((secondsPassed % 3600) / 60)
local seconds = math.floor(secondsPassed % 60)
Broken:
local hours = math.floor((secondsPassed % 86400) / 3600)-- +1
There are several issues here:
UNIX time is relative to midnight 1970-01-01, GMT. If you are not in GMT, you will see an offset that depends on your time zone. If your region observes daylight savings time, this offset will vary based on the date in a potentially rather complex fashion.
Years are not 365.25 days long. They are either 365 or 366 days long, based on the year. (Which doesn't even average out to 365.25; due to special cases for years divisible by 100 and 400, it averages out to 365.2425.)
Unless there is some reason that you cannot use the Lua date and time modules, I would strongly recommend that you do so, rather than trying to recreate them yourself.
You were super close. I hours correct so far by:
local hours = math.floor((secondsPassed % 86400) / 1440) + 1
Related
H H(22-7)/2 * * 1-5 ----- not working
Please let me know correct example
0 0,2,4,6,22 * * *
This will trigger the job at 10 PM, 12 AM, 2 AM, 4 AM and 6 AM everyday.
In case you need to distribute load, you can replace the first 0 with an H.
I'm working on a thing that calculates that turns a number eg 900 into a human readable date.
I've got turning 365 into 1 year 0 months & 0 days.
But, how do I turn 365 into 20/3/15
Lua standard library os provides the functions time and date for such things.
But can use other libraries as well. Like wxLua e.g.
First you need the current time:
local currentTimeInSeconds = os.time()
Then you need to go back in time. Remeber 2016 is a leap year! So instead of 365 you have to go 366 days back.
local timeAgo = 366 * 24 * 60 * 60
Then call os.date() to convert the time in seconds to a date
print(os.date("%d/%m/%y", currentTimeInSeconds - timeAgo))
Which will give you the output
20/03/15
Please refer to the Lua 5.0 PIL for more info
local t = os.date("*t", os.time())
t.day = t.day - 900
local ago = os.time(t)
ago is the timestamp of the time 900 days ago. You can get the formatted date as you want:
print(os.date("%d/%m/%y", ago))
Is possible to scheduled a jenkins job to repeat a task every 45 min in a time interval?
Example (Repeat the job every 45 min as many times as possible since 10:00 am to 16:00 pm) - result would be 8 executions.
Thanks!!!
Yes. In the job configuration page, search for the section Build Triggers. In this section, enable Build periodically and put the following lines:
0,45 0-23/3 * * *
30 1-23/3 * * *
15 2-23/3 * * *
Reference: Is the following cron expression means every 45 minutes?
Explanation:
0,45 means to run at 0 mins and 45 mins for ex. 2 am and 2:45 am
0-23/3 in hour field means that a particular activity has to be performed every 3 hrs. In this case, it means 0, 3, 6, 9,... till 21 hrs (or 2100 hrs). Similarly for others.
So, first line i.e., 0,45 0-23/3 * * * takes care of 0000 hrs and 0045 hrs. Next time (+45 mins) will be 0130 hrs which will be taken care by 30 1-23/3 * * * and so on.
You can check the cron format details here: http://en.wikipedia.org/wiki/Cron
I have a ruby application where i need to get date-time difference in Days-Hours-Minutes format. For this i m using following function
def duration (from_time, to_time)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_seconds = ((to_time - from_time).abs).round
secs = distance_in_seconds.to_int
mins = secs / 60
hours = mins / 60
days = hours / 24
if days > 0
"#{days}d #{hours % 24}h"
elsif hours > 0
"#{hours}h #{mins % 60}m"
elsif mins > 0
"#{mins}m"
end
end
The above called like this from another function
duration(aw_updated, Time.now)
But some time it gives me wrong result,
when i display above values
aw_updated is 2012-09-19 04:23:34 UTC
Time.now is 2012-09-19 16:33:09 +0530
Time.now.utc is 2012-09-19 11:03:09 UTC
And
Diff is 6h 26m
But my system time is 2012-09-19 16:33:09
Not sure where i m doing wrong , some UTC issue?
please advise
For correct answer your both time should have same timezone utc in this case
So it is converting 2012-09-19 16:33:09 +0530 into utc which gives 2012-09-19 11:03:09 UTC and hence difference is Diff is 6h 26m
Would enter this as a comment but can't yet.
I haven't looked in detail at your function but do you have to build it from scratch? Why don't you use the Ruby in built DateTime class. You can parse strings to DateTime objects, do the calculation and use the strftime method to output the time in a format that you want
when users sign up to one of my sites for a free trial, i set their account expiry to be "14.days.from_now". Then on the home page i show how many days they have remaining, which i get with:
(user.trial_expires - Time.now)/86400
(because there are 86400 seconds in a day, ie 60 * 60 * 24)
The funny thing is, this comes out as more than 14, so gets rounded up to 15. On closer investigation in the console this happens for just two days in the future (if you know what i mean). eg
>> Time.now
=> Fri Oct 29 11:09:26 0100 2010
>> future_1_day = 1.day.from_now
=> Sat, 30 Oct 2010 11:09:27 BST 01:00
#ten past eleven tomorrow
>> (future_1_day - Time.now)/86400
=> 0.999782301526931
#less than 1, what you'd expect right?
>> future_2_day = 2.day.from_now
=> Sun, 31 Oct 2010 11:09:52 GMT 00:00
>> (future_2_day - Time.now)/86400
=> 2.04162248861183
#greater than 2 - why?
I thought maybe it was to do with timezones - i noticed that the time from 1.day from now was in BST and the time 2 days from now was in GMT. So, i tried using localtime and got the same results!
>> future_2_day = 2.day.from_now.localtime
=> Sun Oct 31 11:11:24 0000 2010
>> (future_2_day - Time.now)/86400
=> 2.04160829127315
>> (future_2_day - Time.now.localtime)/86400
=> 2.04058651585648
I then wondered how big the difference is, and it turns out that it is exactly an hour out. So it looks like some time zone weirdness, or at least something to do with time zones that i don't understand. Currently my time zone is BST (british summer time) which is one hour later than UTC at the moment (till this sunday at which point it reverts to the same as UTC).
The extra hour seems to be introduced when i add two days to Time.now: check this out. I start with Time.now, add two days to it, subtract Time.now, then subtract two days of seconds from the result, and am left with an hour.
It just occurred to me, in a head slapping moment, that this is occurring BECAUSE the clocks go back on sunday morning: ie at 11.20 on sunday morning it will be two days AND an extra hour from now. I was about to delete all of this post, but then i noticed this: i thought 'ah, i can fix this by using (24*daynum).hours instead of daynum.days, but i still get the same result: even when i use seconds!
>> (Time.now + (2*24).hours - Time.now) - 86400*2
=> 3599.99969500001
>> (Time.now + (2*24*3600).seconds - Time.now) - 86400*2
=> 3599.999855
So now i'm confused again. How can now plus two days worth of seconds, minus now, minus two days worth of seconds be an hour worth of seconds? Where does the extra hour sneak in?
As willcodejavaforfood has commented, this is due to daylight saving time which ends this weekend.
When adding a duration ActiveSupport has some code in it to compensate for if the starting time is in DST and the resulting time isn't (or vice versa).
def since(seconds)
f = seconds.since(self)
if ActiveSupport::Duration === seconds
f
else
initial_dst = self.dst? ? 1 : 0
final_dst = f.dst? ? 1 : 0
(seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
end
rescue
self.to_datetime.since(seconds)
end
If you have 11:09:27 and add a number of days you will still get 11:09:27 on the resulting day even if the DST has changed. This results in an extra hour when you come to do calculations in seconds.
A couple of ideas:
Use the distance_of_time_in_words helper method to give the user an indication of how long is left in their trial.
Calculate the expiry as Time.now + (14 * 86400) instead of using 14.days.from_now - but some users might claim that they have lost an hour of their trial.
Set trials to expire at 23:59:59 on the expiry day regardless of the actual signup time.
You could use the Date class to calculate the number of days between today and the expire date.
expire_date = Date.new(user.trial_expires.year, user.trial_expires.month, user.trial_expires.day)
days_until_expiration = (expire_date - Date.today).to_i
Use since, example:
14.days.since.to_date