Best practices with saving datetime & timezone info in database when data is dependant on datetime

Hugo’s answer is mostly correct, but I’ll add a few key points: When you’re storing the customer’s time zone, do NOT store a numerical offset. As others have pointed out, the offset from UTC is only for a single point in time, and can easily change for DST and for other reasons. Instead, you should … Read more

Using DateTime properties in Code-First Entity Framework and SQL Server

You can specify the type in Fluent API: modelBuilder.Entity<Book>() .Property(f => f.DateTimeAdded) .HasColumnType(“datetime2”); This creates a datetime2(7) column in the database. If you want to finetune the precision you can use: modelBuilder.Entity<Book>() .Property(f => f.DateTimeAdded) .HasColumnType(“datetime2”) .HasPrecision(0); … for a datetime2(0) column in the DB. However, the code you have shown in your question works … Read more

Calculate the number of weekdays between 2 dates in R

Date1 <- as.Date(“2011-01-30”) Date2 <- as.Date(“2011-02-04”) sum(!weekdays(seq(Date1, Date2, “days”)) %in% c(“Saturday”, “Sunday”)) EDIT: And Zach said, let there be Vectorize 🙂 Dates1 <- as.Date(“2011-01-30”) + rep(0, 10) Dates2 <- as.Date(“2011-02-04”) + seq(0, 9) Nweekdays <- Vectorize(function(a, b) sum(!weekdays(seq(a, b, “days”)) %in% c(“Saturday”, “Sunday”))) Nweekdays(Dates1, Dates2)

Aggregate Daily Data to Month/Year intervals

I’d do it with lubridate and plyr, rounding dates down to the nearest month to make them easier to plot: library(lubridate) df <- data.frame( date = today() + days(1:300), x = runif(300) ) df$my <- floor_date(df$date, “month”) library(plyr) ddply(df, “my”, summarise, x = mean(x))

Parsing of Ordered Timestamps in Local Time (to UTC) While Observing Daylight Saving Time

In C#: // Define the input values. string[] input = { “2013-11-03 00:45:00”, “2013-11-03 01:00:00”, “2013-11-03 01:15:00”, “2013-11-03 01:30:00”, “2013-11-03 01:45:00”, “2013-11-03 01:00:00”, “2013-11-03 01:15:00”, “2013-11-03 01:30:00”, “2013-11-03 01:45:00”, “2013-11-03 02:00:00”, }; // Get the time zone the input is meant to be interpreted in. TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(“Eastern Standard Time”); // Create an array … Read more