time_machine icon indicating copy to clipboard operation
time_machine copied to clipboard

How to get First day of Week

Open Solido opened this issue 7 years ago • 11 comments

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

Solido avatar Aug 17 '18 14:08 Solido

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 avatar Aug 17 '18 14:08 Dana-Ferguson

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.

Dana-Ferguson avatar Aug 17 '18 14:08 Dana-Ferguson

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 avatar Aug 17 '18 14:08 Solido

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 !

Solido avatar Sep 28 '18 08:09 Solido

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

Dana-Ferguson avatar Oct 01 '18 11:10 Dana-Ferguson

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

Solido avatar Oct 01 '18 11:10 Solido

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

Dana-Ferguson avatar Oct 01 '18 12:10 Dana-Ferguson

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 !

Solido avatar Oct 01 '18 12:10 Solido

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.

Dana-Ferguson avatar Oct 04 '18 16:10 Dana-Ferguson

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

Solido avatar Oct 08 '18 22:10 Solido

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

Dana-Ferguson avatar Dec 20 '18 03:12 Dana-Ferguson