Dana-Ferguson / time_machine

A date and time API for Dart
https://pub.dartlang.org/packages/time_machine/
Apache License 2.0
126 stars 37 forks source link

How to get First day of Week #6

Open Solido opened 6 years ago

Solido commented 6 years ago

In the US the first day of the week is Sunday. I've been playing with SimpleWeekYearRule but I can't get to find the right API call. Thanks

Dana-Ferguson commented 6 years ago

Right off the cuff, this is what comes to mind:

print(LocalDate.today().adjust(firstDayOfWeek));

LocalDate firstDayOfWeek(LocalDate date) {
  var dayOfWeek = date.dayOfWeek.value;
  if (dayOfWeek == 7) return date;
  return date.subtractDays(dayOfWeek);
}

I'll look at it more closely later, we can probably do this in a culture-sensitive way. (I'm on vacation at the moment -- this whole week -- you can tell by the lack of commits -- It's been awesome. 🌞 !

Dana-Ferguson commented 6 years ago

Looking through the codebase, SimpleWeekRule is marked internal. It's not part of the public API. Did you gain access to it with

import 'package:time_machine/time_machine.dart';
import 'package:time_machine/time_machine_text_patterns.dart';

?

We may have another bug somewhere.

Solido commented 6 years ago

I was trying to access it with the plural form SimpleWeekRules No pressure on this ! I started too by exploring Culture then moved to Noda time for some more discovery. Enjoy your vacation ;)

Solido commented 6 years ago

Hi !

I'm back on it, this is what I used

var weekday = on.dayOfWeek.value; var startDay = on.subtractDays(weekday - 1);

But my question related to how is the first day selected ? Is it by Local or can I configure it somewhere ?

I'd like to offer the user the option to manually choose the first day of week.

Thank you !

Dana-Ferguson commented 6 years ago

That works because it's an enumeration's value. see: https://github.com/Dana-Ferguson/time_machine/blob/master/lib/src/dayofweek.dart

If we subtract the value of the day, which is just an integer, it puts us on the first day of the week.

In order to choose their first day of their week, you'd just vary your constant (-1).

p.s. -- sorry for the late response ~ life is happenning

Solido commented 6 years ago

Thanks ! But can I force the first day of week with an option ?

Dana-Ferguson commented 6 years ago

So, do you mean, How can the user decide if Sunday or Monday is their first day of their week?

Off the top of my head,

var weekday = on.dayOfWeek.value;
var startDay = on.subtractDays(weekday - 1); // your first day of the week is Monday

// You'll need to use modulus to constrain the range to [0, 6)
var startDay = on.subtractDays((weekday - DayOfWeek.value)%7); // your first day of the week is DayOfWeek
Solido commented 6 years ago

I was a little confuse on how to deal with this feature.

Some calendars start with Monday others with Sunday. I was looking for setting Locale or a field thinking that others calculation may rely on this field.

Thank you for the clarification !

Dana-Ferguson commented 6 years ago

I think I was definitely not understanding your question.

Doing a little more research:

Based on {1, 2} , it looks like to me that it is traditional, but the ISO recommendation is Monday. But... I found the CLDR has the recommended first day of the week tagged!

https://github.com/tc39/ecma402/issues/6 https://github.com/unicode-cldr/cldr-core/blob/master/supplemental/weekData.json#L61

We'll need to do a little more investigating to see how we can correlate the information.

Solido commented 6 years ago

This is the app I'm working on https://twitter.com/BlueAquilae/status/1049315328835182592

At first I was looking at the Locale but it's not enough, a US user can working in Europe may decide to override the locale so i'll offer a switch on the settings panel to start on monday or sunday.

Thanks

Dana-Ferguson commented 5 years ago

That is a super cool looking app. :trophy: !