Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time.toSecondOfDay() #730

Closed
thojanssens opened this issue Jul 7, 2020 · 7 comments
Closed

time.toSecondOfDay() #730

thojanssens opened this issue Jul 7, 2020 · 7 comments
Labels
feedback v2? Candidate for being moved to a Temporal v2 proposal

Comments

@thojanssens
Copy link

Can we provide a function to convert a Time to the total number of seconds of the day?

Similar to what Java and js-joda provide:
https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html#toSecondOfDay--

@ptomato
Copy link
Collaborator

ptomato commented Jul 7, 2020

Filed under "feedback", thanks again! Here the same question, can you say anything more about the use case where you missed this method?

@justingrant
Copy link
Collaborator

Welcome, @thojanssens! Thanks so much for taking the time to review Temporal and give feedback. It's very helpful.

ECMAScript built-in objects tend to have fewer methods than OSS libraries built on top of those built-ins. The general philosophy is to expose few methods that can flexibly be applied to many different use cases. Therefore it's unlikely that Temporal would build a dedicated method for a use case like "how many seconds have elapsed so far today?".

However, the more general use case of "[unit] of [larger unit]" is an interesting one. There are already dayOfWeek, dayOfYear, weekOfYear properties on Date, DateTime, and (if #700 is accepted) LocalDateTime but I wonder if there is a more generalized "X of Y" solution possible... and if possible would it be needed?

For the specific case of "second of day", here's one way to do it.

function toSecondOfDay(timeLike) {
  const time = Temporal.Time.from(timeLike);
  const midnight = Temporal.Time.from('00:00');
  const diff = time.difference(midnight, {largestUnit: 'seconds'});
  return diff.seconds;
}
toSecondOfDay({hour: 12})
// => 43200
toSecondOfDay(Temporal.DateTime.from('2020-07-07T10:34:56'))
// => 38096
toSecondOfDay(Temporal.Time.from('12:00'))
// => 43200 (although currently 0. See note below.)

BTW, while writing this sample I found bug #735 in Temporal.Time.from which will make the third call above return 0 until #736 is merged.

@thojanssens
Copy link
Author

@ptomato For example, get the difference between two times in total of seconds. AFAIK Duration doesn't give a value like "total of X".

@justingrant or

function toSecondOfDay(timeLike) {
  const time = Temporal.Time.from(timeLike);
  return time.hour * 60 * 60 + time.minute * 60 + time.second;
}

@ptomato
Copy link
Collaborator

ptomato commented Jul 7, 2020

To get the difference between two times in total of seconds, you can do time1.difference(time2, { largestUnit: 'seconds' }), and we are also intending to provide some way to round Temporal.Durations to the nearest unit (#337). (Not implying that this method shouldn't also be considered, but I'm mentioning them as alternatives.)

@justingrant
Copy link
Collaborator

AFAIK Duration doesn't give a value like "total of X".

Getting totals from an existing duration is discussed here: #584

@justingrant
Copy link
Collaborator

This use case has gotten a little easier from recent changes. Here's what this code will look like in a few days after #1064 lands:

image

@ptomato ptomato added the v2? Candidate for being moved to a Temporal v2 proposal label Jan 14, 2021
@ptomato
Copy link
Collaborator

ptomato commented Feb 12, 2021

I believe these use cases are covered well enough now.

// toSecondOfDay as Justin mentioned above:
plainTime.since('00:00').total({ unit: 'seconds' })
// get the difference between two times in seconds:
time1.until(time2).total({ unit: 'seconds' })

I'm not sure there's anything to propose at https://github.com/js-temporal/proposal-temporal-v2/issues at this point, but please do if you think there is.

@ptomato ptomato closed this as completed Feb 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback v2? Candidate for being moved to a Temporal v2 proposal
Projects
None yet
Development

No branches or pull requests

3 participants