Calculating Seconds Until a Given Date and Time in Joda Time - scala

I am trying to use the Joda Time library to help me schedule sending some messages to an Actor in Akka.
I would like to schedule sending emails every day at 8:30 AM. To do this, I have to tell the scheduler how many seconds (or milliseconds) to wait until the next message is sent.
I would like to account for daylight savings (to make sure it always fires around 8:30, and not 7:30 or 9:30) so I will use LocalDate and LocalTime.
So, basically, I have:
targetDate = LocalDate.now().plusDays(1) and targetTime = new LocalTime(8, 30)
and
rightNow = LocalDateTime.now()
I was wondering what is the best way to compose a targetDateTime based on targetDate and targetTime so I can use it to compute the time difference with rightNow
I know I can create a new LocalDateTime extracting all the values for the constructor from my targetDate and targetTime but: is there a more elegant way?

So far, I have settled for:
targetDateTime = targetDate.toLocalDateTime(targetTime)
secondsToWait = Seconds.secondsBetween(rightNow, targetDateTime)

Getting targetDateTime is easy if you have the targetDate and targetTime (as given in your question) :
targetDateTime = targetDate.toDateTime(targetTime);
Getting the seconds of the Duration between now and targetDateTime:
new Duration(new DateTime(), targetDateTime).getStandardSeconds();
The method is called standard seconds because it assumes every second to be a standard second of 1000 milliseconds. As its javadoc says, currently all Chronologies only have standard seconds.
But you can also simply use milliseconds (no conversion assumptions needed) :
new Duration(new DateTime(), targetDateTime).getMillis();
Disclaimer : I only just saw this was a scala question, so you may have to correct for any syntax differences, since I'm not versed in scala.

Related

Calculate the time difference

I am trying to calculate time difference between 2 ZonedTime dates in Scala. I am receiving dates in "2021-03-19T15:39:42.834248-07:00" format as a String. I need the difference in seconds between 2 dates in Scala. How to convert the string to zoned time and calculate the difference?
You'll want to use the between() method as offered on a temporal.ChronoUnit.
import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit.SECONDS
val start = ZonedDateTime.parse("2021-03-19T15:39:42.834248-07:00")
val stop = ZonedDateTime.parse("2021-03-19T15:49:42.834248-08:00")
val secsBetween:Long = SECONDS.between(start, stop) // 4200
An alternative is to use the until() method on the ZonedDateTime instance itself.
val secsBetween:Long = start.until(stop, SECONDS) //same result
[Java syntax, not Scala.]
tl;dr
Duration
.between
(
OffsetDateTime.parse( "2021-03-19T15:39:42.834248-07:00" ) ,
OffsetDateTime.parse( "2021-03-19T15:49:42.834248-08:00" )
)
.toString()
See this code run live at IdeOne.com.
PT1H10M
…which in standard ISO 8601 format means 1 hour and 10 minutes.
Details
The Answer by jwvh is close, but I would change a couple things.
OffsetDateTime, not ZonedDateTime
Your input strings have only a mere offset-from-UTC but no time zone. So parse those as OffsetDateTime.
An offset is simply a number of hours-minutes-seconds ahead or behind the baseline of UTC, the line drawn through Royal Observatory, Greenwich. An example of an offset is -07:00 which means seven hours behind UTC.
A time zone is much more. A time zone is history of the past, present, and future changes to the offset used by the people of a particular region. A time zone has a name in format of Continent/Region. Given our example above, on some dates, several time zones may share the offset of -07:00, including America/Dawson, America/Los_Angeles, America/Phoenix, America/Boise, and more.
OffsetDateTime odt = OffsetDateTime.parse( "2021-03-19T15:39:42.834248-07:00" ) ;
Duration
Represent a span-of-time using Duration, on the scale of hours-minutes-seconds-nanos.
Duration d = Duration.between( sooner , later ) ;
Generate text in standard ISO 8601 format.
String output = d.toString() ;

Same date in different time zone

My question is how can I get the same day, month, year, hour, minutes, seconds in a different time zone, for example:
var now = moment().valueOf();
var result1 = moment(now).format('DD-MM-YYYY HH:mm:SS Z');
In my time zone I get some this like this:
18-02-2015 21:08:34 +01:00
So how can I change only time zone without changing other values (days, months, ..., minutes, ...)
I want to get some thing like this:
result2: 18-02-2015 21:08:34 +01:00
result3: 18-02-2015 21:08:34 +10:00
result4: 18-02-2015 21:08:34 +05:00
result5: 18-02-2015 21:08:34 -06:00
result6: 18-02-2015 21:08:34 -11:00
Thanks in advance
Here's how you could do what you are asking:
// get a moment representing the current time
var now = moment();
// create a new moment based on the original one
var another = now.clone();
// change the offset of the new moment - passing true to keep the local time
another.utcOffset('+05:30', true);
// log the output
console.log(now.format()); // "2016-01-15T11:58:07-08:00"
console.log(another.format()); // "2016-01-15T11:58:07+05:30"
However, you must recognize two important things:
The another object no longer represents the current time - even in the target time zone. It's a completely different moment in time. (The world does not synchronize local clocks. If it did, we'd have no need for time zones!).
For this reason, even though the above code satisfies the question that was asked, I strongly recommend against using it. Instead, re-evaluate your requirements, as it's likely they are misunderstanding the nature of time and time zones.
A time zone cannot be fully represented by an offset alone. Read "Time Zone != Offset" in the timezone tag wiki. While some time zones have fixed offsets (such as +05:30 used by India), many time zones change their offsets at different points throughout the year to accommodate daylight saving time.
If you wanted to account for this, you could use moment-timezone instead of calling utcOffset(...). However, the issue in my first bullet would still apply.
// get a moment representing the current time
var now = moment();
// create a new moment based on the original one
var another = now.clone();
// change the time zone of the new moment - passing true to keep the local time
another.tz('America/New_York', true); // or whatever time zone you desire
// log the output
console.log(now.format()); // "2016-01-15T11:58:07-08:00"
console.log(another.format()); // "2016-01-15T11:58:07-05:00"
The most-voted answer is messy IMO. Here's a cleaner solution - similar to BlueSam's answer, but safer:
const myTime = moment.tz('2016-08-30T22:00:00', moment.ISO_8601, 'America/Denver')
myTime.format() //2016-08-30T22:00:00-06:00
const sameTimeDifferentZone = moment.tz(myTime.format('YYYY-MM-DDTHH:mm:ss.SSS'), moment.ISO_8601, 'America/New_York')
sameTimeDifferentZone.format() //2016-08-30T22:00:00-04:00
After reading the above comments, I thought I'd add in based on Joao's answer.
In my case I was trying to use a preexisting moment date with a timezone and converting it to another timezone while retaining the original date value (as asked in the question).
var newTimezone = 'America/Denver';
//date - contains existing moment with timezone i.e 'America/New_York'
moment.tz(date.format('YYYY-MM-DDTHH:mm:ss'), 'YYYY-MM-DDTHH:mm:ss', newTimezone);
From the moment docs: http://momentjs.com/timezone/docs/
reference moment-timezone-with-data.js and specify which timezone to go to, like so:
moment(date).tz("America/Los_Angeles").format()

Is it possible to set up a clock trigger in Google Apps to send spreadsheet hourly?

I have a spreadsheet that logs incoming answered and missed calls in Google Drive.
It is currently set to send an email every hour between 10am and 7pm.
Ideally I would like it to not send the email during the weekend.
Setting up each hour Monday to Friday uses too many triggers.
Is there a way to construct a trigger that will send an email every hour (10am to 7pm) only Monday to Friday?
I've read the documentation at Google and a few (unrelated as it turns out) examples on here and I am stumped!
I tried putting a load of trigger conditions together:
function autoSendHourly() {
ScriptApp.newTrigger("hourlyUpdate()")
.timeBased()
.onWeekDay(ScriptApp.WeekDay.MONDAY)
.onWeekDay(ScriptApp.WeekDay.TUESDAY)
.onWeekDay(ScriptApp.WeekDay.WEDNESDAY)
.onWeekDay(ScriptApp.WeekDay.THURSDAY)
.onWeekDay(ScriptApp.WeekDay.FRIDAY)
.atHour(10)
.atHour(11)
.atHour(12)
.atHour(13)
.atHour(14)
.atHour(15)
.atHour(16)
.atHour(17)
.atHour(18)
.atHour(19)
.create();
}
I wasn't entirely surprised that it didn't work, but I was mildly surprised that it threw up no errors.
Any help (including "you're mad it can't be done") would be greatly appreciated.
The simplest thing to do is use the create trigger like you did but for every hour every day and then in you handler function add a small piece of code that will return if day and time don't meet specific conditions like explained in this (old) post.
the code may look like something like this :
function officeHours(){
var nowH=new Date().getHours();
var nowD=new Date().getDay();
Logger.log('day : '+nowD+' Hours : '+nowH)
if(nowH>17||nowH<8||nowD==6||nowD==0){return}
Browser.msgBox('time to work !');//normally your real function should begin here...
}
I haven't messed around with java in awhile but this article might help.
Android: how to get the current day of the week (Monday, etc...) in the user's language?
If it was me, I would get the day of the week and check it in a switch. Then if it matches a week day call a function to check the time between 10am and 7pm.
Best of luck.

Is there a class in JDK to represent an hour of the day, but not necessarily a specific hour at a specific date?

Is there a class in JDK or Guava to represent an hour of the day, but not necessarily a specific hour at a specific date?
If not, why?
In JDK 1.3-1.7, the answer is no. A specific time within a day is much easier to calculate then date, because you don't have to deal with leap year, leap month, such headache stuff. A simple integer is just enough. When you need to convert the time to a locale string, using SimpleDateFormatter or whatever, you can simply convert the time to a Date, just ignore the date part:
int time = 8 * 60 + 34; // 8:34 am
Date date = new Date(60000L * time);
Reset the time zone to +0, and pass the date to the formatter:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+0"));
sdf.format(date);
You could simply wrap a byte into a class and every time that the current hour passes 23 within your increment() (or appropriate name) method, set the value of the byte to 0, and whenever the value passes below 0 in your decrement() (or appropriate name) method, set the value of the byte to 23.
As far as I know, there is not a specific class representing Hour (in the JDK or Guava), but there are easy to use classes to fetch the hours from a specific instance of time (which is what I am assuming you are after with this question).
You could use JODA-Time, as Paŭlo Ebermann mentions, but that is an external library. Within the JDK, there is a class called Calendar, which has many useful methods.
To get the hour of a long representing the current time, you could do this:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
int hour = c.get(Calendar.HOUR); //returns 0-11
int hourOfDay = c.get(Calendar.HOUR_OF_DAY); //returns 0-23

Objective C: Compare timeInMillis with current time

In my iPhone application, I need to calculate the time difference between the time a message was created on the server, and the time my phone received it.
The server (Java) puts in a number returned by System.currentTimeMillis() as metadata along with the message.
How do I compare this number with the current time on the device? Could not find a suitable NSDate method to do this comparison.
Thanks in advance!
You might take a look at this SO answer and the -timeIntervalSinceDate: method.
You can use (NUInteger) ([[NSDate date] timeIntervalSince1970] * 1000)