Qtimefx.com Review What You Should Know About QtimeFX

Best Binary Options Brokers 2020:
  • Binarium

    The Best Binary Options Broker 2020!
    Perfect For Beginners!
    Free Demo Account!
    Free Trading Education!

  • Binomo

    Good choice for experienced traders!

What every Salesforce developer should know about Dates and Times in Apex

Dates, times and computers have never been the best of friends. Just look at the list of famous bugs caused by miscalculations in Dates and Times: the famous y2k bugs which cost over $300 billion worldwide to address, the Deep Impact spacecraft which was lost because the time was stored in too small a variable, or the deadly Patriot Missile Failure which resulted in 28 deaths because the calculation was off by a small fraction of a second. It’s not just huge disasters, simple miscalculations can cause bugs like the ones reported recently in the Apple Watch causing crashes and reboots when Daylight Saving Time came into effect.

While Salesforce developers like you and me are unlikely to be faced with intercepting missiles or keeping track of spacecraft, we are likely to face angry users if we don’t handle dates and times properly. In this article, we’re going to list principles and practices when dealing with Dates, Times and DateTimes in Apex.

Rule 1: Know your time zones

Time zones aren’t fun. They’re even less fun when Daylight Saving Time (DST) is involved. Just ask the students in Ohio U. who went rioting after losing an hour of drinking time to DST. One of those Ohio U. students may be your Director of Sales today, so it’s best you have your time zones sorted out.

The two kinds of time zones

Time zones come in two varieties — region-based and offset-based. When you set your org’s default time zone to America/Chicago, for example, you’re referring to the time zone of a region. The time in this region would either be GMT-06:00 or GMT-05:00 depending on whether DST is in effect or not.

Then we have the offset varieties, such as Central Standard Time (GMT-06:00) and Central Daylight Time (GMT-05:00). These kind of time zones represent an offset from the Greenwich Mean Time, but don’t necessarily represent any particular region. The America/Chicago (or Central) region, for example, follows CST or CDT depending on the time of the year.

As you might guess, region-based time zones come with a problem. On November 3, 2020 at 02:00 AM America/Chicago, clocks “fall back” one hour. This means that November 3, 2020 at 01:30 AM in the America/Chicago region comes twice. Also, March 10, 2020 at 02:00 AM, the clocks are to “spring forward” one hour. This means that in the America/Chicago time zone, March 10, 2020 02:30 AM simply doesn’t exist.

Time zones in Salesforce

In Salesforce, we see region-based time zones very often. When you create a user, for example, you get to pick a region-based timezone for the user. You set your org’s default timezone to a region-based timezone as well. The timezone in my personal org, for example, is America/Los_Angeles. This is better than setting an offset-based time zone because Salesforce takes care of displaying the correct date and time to me whether Daylight Saving Time is in effect or not.

But we do still see offset-based time zones. If I build a DateTime object and call the format() method on it and specifically ask it to display the time zone, I would see an offset-based time zone. For example, if I run the following code in the console:

I would see 01/17/2020 05:00 PST in the debug logs.

Using an offset-based time zone for display makes sense because it removes the ambiguity associated with region-based time zones. November 3, 2020 at 01:30 AM appears twice in America/Los_Angeles but when displaying the two instances, adding PST or PDT can resolve that ambiguity.

Best Binary Options Brokers 2020:
  • Binarium

    The Best Binary Options Broker 2020!
    Perfect For Beginners!
    Free Demo Account!
    Free Trading Education!

  • Binomo

    Good choice for experienced traders!

And using a region-based time zone when setting a user or org’s time zone or when constructing an instance of DateTime makes sense because DST gets handled internally.

What time zones are available in Salesforce?

If you go to your Company Information page on setup, you would see a list of time zones. America/Chicago and America/Los_Angeles are examples of time zones available. The list of region-based time zones available in Apex, however, is far greater.

The official documentation says that Apex supports all time zones returned by TimeZone.getAvailableIDs method in Java. Where does Java get this list from? Currently, Java gets this from the tz database which is an ICANN-backed collabarative effort to maintain a list of all time zones. Since governments can redefine time zones any time, the database is constantly updated.

Rule 2: GMT is the model, everything else is a representation

Imagine you have an object that represents distance and stores distances as millimeters. You can create a new Distance instance by perhaps calling Distance.newInstance(2000) which creates a distance object representing a distance of 2000 millimeters, or 2 meters. When you call the getDistance() method on the distance record, it returns 2000 . If you call format() on the distance record, while your locale is somewhere in the UK, it prints out 2m . If you call format() while your locale is somewhere in the US, it prints out 6 ft, 6.7402 in . What’s important to remember here is that millimeters is the model we have chosen, and 2m and 6 ft, 6.7402 in are representations.

What do you say when someone comes to you and says that they wanted the distance object to represent distance in meters instead of millimeters? and so they just did Distance.newInstance(2) ? Now, when they call getDistance() , it returns 2 which is what they wanted.

You would rightfully be flabbergasted. That’s not how it works. The Distance objects uses millimeters as the model and if you’re working with meters, be sure to multiply it by 1000 before constructing the object. Or maybe add a newInstanceMeters(Integer meters) function to your Distance class that multiplies the passed meters by 1000 and returns a Distance object representing 2 meters as 2000 millimeters.

The GMT Model

With DateTime objects, the logic is the same. GMT is the model, and dates in all other time zones are representations. Apex actually makes this pretty easy for us — all DateTime objects we create are GMT DateTimes, but provide us a way to display it in any time zone. Just like our Distance class above which stores distance in millimeters, but can display it in any many different units.

Let’s do an experiment. My user in the org I’m using right now has a time zone of America/Los_Angeles, which is 8 hours behind GMT. What happens if I construct a date for February 10, 2020 12:00 PM? I’ll put the following code in my anonymous apex window:

The DateTime instance we constructed represents a moment in time. The moment in time when it’s February 10, 2020 12:00 PM in Los Angeles, California; February 10, 2020 02:00 PM in Chicago, Illinois and February 10, 2020 08:00 PM in the GMT time zone.

When we construct this date, Salesforce assumes that we’re talking about a moment in time in the current user’s selected time zone, which in our case is America/Los_Angeles. What do we see in the console when we print out the date using System.debug ?

Let me add some more emphasis to that: EIGHT PEE EM.

Even though we constructed a DateTime for 12:00 PM.

What is the reason for this? Internally, Apex uses GMT as a model to store the date and time. Similar to how it doesn’t matter when we stored our distance as millimeters when we wanted to work with meters, it doesn’t matter that we store our DateTime as GMT, because all we want is to store a specific moment in time. It’s actually better that we use a consistent model to store our DateTime records.

And we can represent this moment in time in multiple ways, just like we could represent our distance as meters or feet and inches. Let’s run the following code:

This is the result in the debug log:

Additionally, do remember that the result would have been different if my user’s selected time zone was America/Central, because February 10, 12PM in Central Standard Time would translate to 6PM in GMT:

Rule 3: Always capture the correct moment in time

This rules is implied by the previous rule, but it’s so easy to get this wrong that I feel it deserves a special mention.

The Salesforce world is filled with people asking how they can construct a DateTime in their local time zone. Most people who ask this question end up adjusting the DateTime object to look like their local time. This is a piece of code I picked up from the developer forums — and it’s not uncommon to see something like this in the dev forums or on stack overflow:

The following is the result of this code:

The intent was to create a DateTime object that represents February 10, 2020 12:00 PM as a local DateTime. Do you see the problem? local does not represent the correct moment in time. Sure, local evaluates to February 10, 2020 12:00 PM, and that is what we intended to construct, but it represents 12PM GMT, not 12PM PST. This is the same mistake is doing Distance.newInstance(2) because you wanted to capture a distance of 2 meters.

So what is the right answer to the question of constructing a DateTime in your local time zone? The answer is that you can’t. And in most cases, you don’t need to anyway. What you need from DateTime is to capture a moment in time, and every moment can be captured in GMT. Make sure that your DateTimes represent the correct moment in time, and you can always display it in any time zone.

Rule 4: Store Date as Date, and Time as Time

It is a good rule of thumb to never use DateTime if you only need to represent a particular time or a particular date.

Let’s take an example. What happens when you, a user in the America/Chicago time zone, try to save Feb 10, 2020 in a DateTime field instead of a Date field? Maybe you’d save Feb 10, 2020 00:00 as a DateTime. Which gets saved as Feb 10, 2020 06:00 GMT. Which is all good. And what happens when your boss, who is in the America/Pacific Time Zone, loads up your custom page which displays this very important date to him? He sees February 9, 2020. Because Feb 10, 2020 06:00 GMT is February 9, 2020 22:00 PST. What happens next? Your boss, who graduated from Ohio U., goes rioting.

Because DateTime is translated to the user’s time zone and stored as GMT, it’s never a good idea to use it to only store only a Date or a Time. Always be sure to use the Date and Time types in Apex and the Date and Time fields on sObjects if you only need to store a date or a time.

Another case where you would want to use separate Date and Time objects or fields is when representing an event that takes place at a certain local time in more than one place. For example, if you want to store a deadline to submit documentation on February 1, 2020 at 5:00 PM no matter where you are (your team in New York submits at 5:00 PM Eastern Time and you team in Los Angeles submits at 5:00 PM Pacific Time), you would want to use separate date and time fields.

Remember, DateTime represents a specific moment in time that is automatically adjusted across different time zones. To store values that don’t adjust based on time zone, use separate Date and Time fields.

Rule 5: Be prepared if time zones are redefined

In 2005, the US passed a bill which would extend daylight saving time by one hour effective 2007. Instead of the first Sunday of April, DST would start coming into effect on the second Sunday of March. And it wasn’t pretty for us programmers.

Do you see why that’s a problem for us? This is a case where the GMT model doesn’t work. Suppose that before the 2005 announcement was made, maybe some time in 2004, you had constructed a DateTime representing March 20, 2008 09:00 AM in America/Chicago and saved it in a field. It would have been saved as March 20, 2008 03:00 PM GMT.

But in 2005, the US announces that DST in 2007 would begin on March 11 instead of April 1. Suddenly, March 20, 2008 03:00 PM GMT, which is the GMT date you have saved, doesn’t translate to March 20, 2008 09:00 AM in America/Chicago anymore.

A good solution to deal with situations like these would be to persist future DateTimes representing a particular local time in the local time zone, but unfortunately Salesforce only allows us to save DateTimes in GMT. We could talk about saving a local DateTime as a Date, Time and a String Timezone field, but the added complication and the lack of ability in Apex to directly construct a DateTime from data in any given timezone (I intend to write a blog on how to do this later), it might not be worth it.

Depending on the situation, what we can do is keep an eye out for changes in time zone rules and be ready to either inform users or update the saved dates in case the rules do change.


Almost everything discussed above becomes obvious if we follow just one principle: DateTime always has GMT as the model. You can construct a DateTime based on the local time zone (the time zone of the user) but it’s always saved as GMT. Another important fact to remember is that time zones are region-based and adjust automatically for DST. Just keep these basics in mind and you won’t go wrong working across time zones.

Are there any other rules or practices you use when dealing with dates and times in Apex? I would be happy to hear your thoughts.

Error 404 Not Found

Oops! We couldn’t find this Page.

Please check your URL or use the search form below.

Checking your browser before accessing scamrecovery.net.

This process is automatic. Your browser will redirect to your requested content shortly.

11 Days After Fuming About a Coughing Passenger, a Bus Driver Died From the Coronavirus

Jason Hargrove, a bus driver in Detroit, posted a widely viewed Facebook video that underscored the dangers blue-collar workers face as they find themselves on the front lines of the pandemic.

    April 4, 2020

Jason Hargrove was furious.

A woman on his bus had just coughed four or five times, unguarded. He went on Facebook and vented his anger, cursing at times. In a video posted on March 21, he railed against the disrespect that he said passengers like her had shown workers like him who were trying to make Detroit run in the midst of a pandemic.

“We’re out here as public workers, doing our job, trying to make an honest living to take care of our families,” he said. “But for you to get on the bus, and stand on the bus, and cough several times without covering up your mouth, and you know that we’re in the middle of a pandemic, that lets me know that some folks don’t care.”

On Wednesday, a week and a half after recording the video outside his bus, Mr. Hargrove, a 50-year-old, married father of six, died from complications of Covid-19, the disease caused by the coronavirus, according to his union and city officials.

Mr. Hargrove’s video has since been viewed more than half a million times, and has resonated as a reminder of the dangers that transit employees and other blue-collar workers face as they suddenly find themselves on the front lines of the coronavirus.

“He knew his life was being put in jeopardy — even though he was going to work for the citizens of Detroit every day — by somebody who just didn’t care, somebody who didn’t take this seriously,” Mayor Mike Duggan told reporters on Thursday. “And now he’s gone.”

Mayor Duggan said he thought of Mr. Hargrove and other workers who put themselves at risk whenever he saw people violating social distancing rules.

“You’re putting really good people like Jason Hargrove’s lives on the line,” he said. “I hope the people in this city and in this country will watch his video and listen to his words, because it’s a message this country needs to hear.”

Detroit, which has endured population loss and the largest municipal bankruptcy in the nation, has become a coronavirus hot spot, with more than 3,900 cases and nearly 130 deaths as of Saturday afternoon, according to the City Health Department.

City officials said they were not sure when Mr. Hargrove contracted the virus.

But even before his death, Detroit’s bus drivers had complained that the city was not doing enough to protect them, said Glenn Tolbert, the president of the local transit workers’ union, who himself tested positive for the coronavirus on Thursday. Mr. Tolbert said on Saturday that 200 of the union’s 525 members were in quarantine.

“The fear is real, and this virus is real, and the closer it hits to home, it just makes it more real,” Mr. Tolbert said.

On March 17, Detroit’s bus drivers refused to go to work until the city acted.

The same day, Mayor Duggan announced several changes to protect the drivers, including waiving fares; requiring passengers to use only the rear doors; and making sure drivers had gloves, masks and disinfecting wipes. The city also stepped up the cleaning and sanitizing of buses, according to John Roach, a city spokesman.

Detroit has also reached an agreement with the Greektown Casino-Hotel to provide rooms for bus drivers who are worried about infecting family members at home, Mr. Roach said.

In his video, Mr. Hargrove said the woman who coughed on his bus without covering her mouth showed that some people were not taking the virus seriously.

“It’s folks dying out here,” he said, adding that he felt violated both for himself and for other passengers who were on the bus.

Mr. Hargrove had been a bus driver since October 2020 and was an active member of the union, Mr. Tolbert said. On his Facebook page, Mr. Hargrove had posted a photo of himself wearing a mask on his bus with the captions “#ICannotStayHome” and “I’mOnTheRoad4U.”

“He was frustrated, but he was a professional guy to the end,” Mr. Tolbert said.

“I just don’t want his death to be in vain,” he added. “I want us to get through this thing, and I want the transit operators around the world to be recognized as first responders and given the necessary equipment.”

Best Binary Options Brokers 2020:
  • Binarium

    The Best Binary Options Broker 2020!
    Perfect For Beginners!
    Free Demo Account!
    Free Trading Education!

  • Binomo

    Good choice for experienced traders!

Like this post? Please share to your friends:
How To Make Money on Binary Options Trading
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: