Get current UTC time in actionscript - actionscript

How do I get the number of milliseconds, in UTC time, since the epoch?
Just like myDate.getTime() returns the milliseconds in local time, I need this in universal (UTC) time.
Any ideas?

The ActionScript 2 Date object has a getTimezoneOffset method that will give you the difference between UTC and the local timezone in minutes.
var utc = myDate.getTime() + (myDate().getTimezoneOffset() * 60000);

Related

Storing and comparing UTC times

Say this is the current server time in UTC 2021-07-30 00:33:25 UTC, which in local time is just 2021-07-29 5:33 PM
25 hours from that time is 2021-07-31 01:33:25 UTC, which in local time is 2021-07-30 6:33 PM
The stored time being compared is saved as 2021-07-30T19:00:00+00:00, which in local time should be 2021-07-30 7:00 PM
The problem is that the stored time should be 2021-07-31T02:00:00+00:00 to equal 2021-07-30 7:00 PM,
but I'm not sure of the best approach to get that time.
Here's how I get the time being sent to my rails backend
import * as moment from 'moment-timezone';
...
const start_time = moment
.tz(
listingAvailability.listing_availability_ranges_attributes[0]
.start_time,
'YYYY-MM-DD hh:mm a'
)
.tz('America/Phoenix')
.toISOString(true)
I fixed it with
moment.utc(moment(listingAvailabilityTime).utc().local()).format();

Date substraction returning odd value

I am trying to allow the destruction of a record only if this record has been created within the last 30 minutes.
Then I retrieve the created_at value of my record (date2) and check against Time.now (date1) :
date1 = 2016-09-21 19:44:52 +0200
date2 = 2016-09-21 17:23:16 UTC
then I just substract the two :
(date1-date2).minutes.to_i
But the result returned is in the 10s of thousands (something like 97000) and increasing very fast when i refresh..(as Time.now changes) whereas we should only get 141 minutes as per above example values
The .minutes is what makes the thing don't work. Remove it and it should work.
If you want to find the gap in minutes between to date you just have to substract them, divide the result per 60 and round it.
((date1 - date2) / 60).round
Hope it helped, happy ruby coding!
You need to first convert the local time (time1) to UTC time, or UTC time (time2) to local time.
require 'time'
time1 = Time.parse('2016-09-21 19:44:52 +0200')
#=> 2016-09-21 19:44:52 +0200
time2 = Time.parse('2016-09-21 17:23:16 UTC')
#=> 2016-09-21 17:23:16 UTC
(time1.utc - time2)/60
#=> 21.6

Converting a variable with a UTC time to a Seconds integer (Ruby)

I have a variable that has a UTC time tied with it, example:
offer_ends_at => Sun, 25 Nov 2012 07:59:59 UTC +00:00,
My goal is to create a new variable, and convert the Time to a seconds integer. I tried with some methods like
seconds = offer_ends_at.to_time.to_i
I end up getting a epoc time. I'm after an actual integer that contains the amount of seconds left until the date happens.
I you want " an actual integer that contains the amount of seconds left until the date happens.", then you can substract the timestamp you're getting, to the current timestamp, ie :
seconds = offer_ends_at.to_time.to_i - Time.now.to_i
In fact, you don't need to put to_i, you can just substract the two times (Time#- method):
seconds = offer_ends_at.to_time - Time.now

How does one create objects in the future but ignore yearly/annual TimeZone switches?

I create multiple scheduled objects with different scheduled_on attributes. For example, each object would have a date to land on 4:00pm the first of every month.
Once one of those objects hits a timezone change. The app intelligently configures it an hour ahead or behind so that its relative to its parent's timezone.
The problem is that the app will save an object as 4:00PM (in Pacific Standard) for times that will eventually be displayed as (PDT or an hour ahead or 5:00pm). This would mean that I need it to save an hour off in UTC so that when the time comes about, it will display as 4PM regardless of what timezone we are in.
Whats the best technique for ensuring this in Rails?
I'm going to answer this question by pointing out some good things to know about adding time in Rails in relation to timezone.
When you add time, time is allocated in UTC to stay the same time despite timezone changes :
t = Time.now
-> 2012-08-10 13:17:01 +0200
t + 90.days
-> 2012-11-08 13:17:01 +0100
A DateTime will not do this. A DateTime will go up an hour or down an hour in the same TimeZone it began in :
dt = DateTime.now
=> Fri, 10 Aug 2012 13:16:54 +0200
dt + 90.days
=> Thu, 08 Nov 2012 13:16:54 +0200
But a DateTime is the only way to get the number of days between two dates which you can do by subtracting two DateTimes. This, you can't do with a Time, because when substracting a time, it will not divide by a perfect 24 hours, you'll get an irrational number because of the timezone switch.
This is specific to my issue. But I solved my problem by converting my Time to DateTimes to find the number of days in distance, and then reconverted back to time to find a later time in UTC relative to a TimeZone change :
original_job.to_time + ( new_date.to_datetime - original_job.to_datetime ).to_i.days

What time am I dealing with in Quartz.net?

I am wondering when I set something like this
Trigger trigger = TriggerUtils.MakeDailyTrigger("abc", 5, 00);
I am setting it for 5:00am. Is this 5:00am server time or UTC time?
It uses UTC time, however this is not properly documented.
Edit: actually it looks like it has used both! Versions prior to 0.9 used local time, those after use UTC (source), so it should be UTC as long as you are using a recent version.
5:00am UTC time. Public Quartz.NET API always expects times in UTC format. Just FYI, MakeDailyTrigger is just a shortcut to CronTrigger with following format:
string.Format("0 {0} {1} ? * *", minute, hour)
I believe that when you enter an hour in the hour argument in the MakeDailyTrigger method that Quartz.Net is expecting local time...Internally Quartz.net converts this time to UTC, but if you enter 5 in the hour argument the trigger will fire at 5AM local time.
Try this
Trigger trigger =
TriggerUtils.MakeDailyTrigger("trigger",5,0);
var ttimes = TriggerUtils.ComputeFireTimes(trigger, null, 1);
foreach (DateTime ttime in ttimes)
{
Console.WriteLine(ttime);
'ttime is in UTC - so for EST, ttime.Hour would be 10AM
'however ttime.ToLocalTime().Hour would be 5AM
}

Resources