4
\$\begingroup\$

I was refactoring some code and found this:

DateTime CurrentDateTime = System.now();
Datetime ESTDate = Datetime.newInstance(CurrentDateTime.year(),CurrentDateTime.month(),CurrentDateTime.day(),CurrentDateTime.hour(),CurrentDateTime.minute(),CurrentDateTime.second());
String myDateFormat = ESTDate.format('yyyy-MM-dd hh:mm:ss');
String myDate = myDateFormat.replace(' ', 'T');
object1.birthDate = myDate;

And I changed it to

object1.birthDate = datetime.now().format('yyyy-MM-dd\'T\'hh:mm:ss');

CurrentDateTime isn't used anywhere just in those four lines. How a programmer can even do that? Or WHY?

\$\endgroup\$
8
  • 3
    \$\begingroup\$ What's language is that? \$\endgroup\$
    – Cyrille Ka
    Commented Feb 11, 2013 at 22:30
  • \$\begingroup\$ @CyrilleKarmann - Probably C# (or other .NET); DateTime is the standard 'timestamp' type, and System.Now is the 'current timestamp' property. Personally, though, I'm a little suspicious of anything named object1, and storing the time for a birthdate (while technically people are born at a particular 'instant', nobody ever thinks of them that ways - it tends to be the 'local calendar day'). That, and outputting it as a formatted string. It looks like there may have been an attempt to deal with timezones, but I don't see anything that actually references them, so... \$\endgroup\$ Commented Feb 11, 2013 at 23:32
  • \$\begingroup\$ @Clockwork-Muse no, this is not C#. apex-code is a programming language for Salesforce. \$\endgroup\$
    – Adam
    Commented Feb 13, 2013 at 9:38
  • \$\begingroup\$ @Clockwork-Muse imagine that instead birthDate there is a field named f1 -> object1.f1 = ... \$\endgroup\$ Commented Feb 18, 2013 at 11:22
  • \$\begingroup\$ @Chiz - Ouch. Is there nothing you can do to get better names for things? I'd be "Spindle, Fold, and Mutilate"-ing someone who tried that on regular production code... \$\endgroup\$ Commented Feb 18, 2013 at 16:25

1 Answer 1

5
\$\begingroup\$

The programmer probably didn't understand how quoting works in DateTime.format() - just couldn't get the 'T' to appear in the string, so bailed, put the space there and replaced it. Creating ESTDate from CurrentDateTime is particularly weird, though!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.